From 6aab4389f27e35ae5bdacb7cf01d0a69d067e40e Mon Sep 17 00:00:00 2001
From: PA4WDH
Date: Sun, 25 Aug 2024 17:03:13 +0200
Subject: Small fixes in cpulist.c

---
 cpulist.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/cpulist.c b/cpulist.c
index dfb4733..eb219b5 100644
--- a/cpulist.c
+++ b/cpulist.c
@@ -16,11 +16,15 @@ extern struct s_cpudata cpudata;
 // Add a CPU to the list
 void cpulist_add(char *cpu)
 {
- cpudata.cpulist_len++;
- cpudata.cpulist=reallocarray(cpudata.cpulist,cpudata.cpulist_len,sizeof(char *));
- cpudata.cpulist[cpudata.cpulist_len-1]=malloc(strlen(cpu)+1);
- strcpy(cpudata.cpulist[cpudata.cpulist_len-1],cpu);
- cpudata.cpulist[cpudata.cpulist_len-1][strlen(cpu)]=0;
+ int newlen;
+
+ newlen=cpudata.cpulist_len+1;
+ cpudata.cpulist=reallocarray(cpudata.cpulist,newlen,sizeof(char *));
+
+ cpudata.cpulist[cpudata.cpulist_len]=malloc(strlen(cpu)+1);
+ strcpy(cpudata.cpulist[cpudata.cpulist_len],cpu);
+ cpudata.cpulist[cpudata.cpulist_len][strlen(cpu)]=0;
+ cpudata.cpulist_len=newlen;
 }
 
 // Validate if we found a file named cpu[0-9][0-9][0-9]
@@ -101,10 +105,10 @@ void cpulist_free()
 {
  int count;
  if (cpudata.cpulist==NULL) return;
+ int len=cpudata.cpulist_len;
+ cpudata.cpulist_len=0;
 
- for(count=0;count<cpudata.cpulist_len;count++) free(cpudata.cpulist[count]);
-
+ for(count=0;count<len;count++) free(cpudata.cpulist[count]);
  free(cpudata.cpulist);
  cpudata.cpulist=NULL;
- cpudata.cpulist_len=0;
 }
-- 
cgit v1.2.3