changeset 300:85d92bcbebf3

Be more diligent about not doing structure reuse, easy to have either uninitialized fields or dangling fields/pointers in them. Fix the case of PSFStackItem and get rid of double free()s and such nastyness.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 09 Jan 2020 22:44:20 +0200
parents b3d867a0b92c
children 86f5c9ce26a8
files sidinfo.c
diffstat 1 files changed, 15 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/sidinfo.c	Thu Jan 09 18:36:37 2020 +0200
+++ b/sidinfo.c	Thu Jan 09 22:44:20 2020 +0200
@@ -342,11 +342,14 @@
         {
             for (int n = 0; n < stack->nitems; n++)
             {
-                if (stack->items[n].cmd == -1)
-                    th_free(stack->items[n].str);
+                PSFStackItem *item = &stack->items[n];
+                th_free(item->str);
+                th_free(item->fmt);
             }
             th_free(stack->items);
         }
+
+        // Clear the stack data
         memset(stack, 0, sizeof(PSFStack));
     }
 }
@@ -359,7 +362,6 @@
 
     while (*start)
     {
-        PSFStackItem item;
         const char *end = strchr(start, ',');
         char *field = (end != NULL) ?
             th_strndup_trim(start, end - start, TH_TRIM_BOTH) :
@@ -367,16 +369,15 @@
 
         if (field != NULL)
         {
+            PSFStackItem item;
             int found = argMatchPSFieldError(field);
             th_free(field);
 
             if (found < 0)
                 return FALSE;
 
+            memset(&item, 0, sizeof(item));
             item.cmd = found;
-            item.flags = 0;
-            item.fmt = NULL;
-            item.str = NULL;
 
             if (!siStackAddItem(stack, &item))
                 return FALSE;
@@ -734,7 +735,6 @@
 //
 static BOOL argParsePSFormatStr(PSFStack *stack, const char *fmt)
 {
-    PSFStackItem item;
     const char *start = NULL;
     int mode = 0;
     BOOL rval = TRUE;
@@ -770,9 +770,11 @@
             if (fmt - start == 0)
             {
                 // "@@" sequence, just print out @
+                PSFStackItem item;
+                memset(&item, 0, sizeof(item));
                 item.cmd = -2;
-                item.str = NULL;
                 item.chr = '@';
+
                 if (!siStackAddItem(stack, &item))
                     return FALSE;
             }
@@ -788,10 +790,9 @@
                 int ret = argMatchPSFieldError(field);
                 if (ret >= 0)
                 {
+                    PSFStackItem item;
+                    memset(&item, 0, sizeof(item));
                     item.cmd = ret;
-                    item.flags = 0;
-                    item.fmt = NULL;
-                    item.str = NULL;
 
                     if (fopt != NULL)
                     {
@@ -824,8 +825,11 @@
         case 2:
             if (*fmt == 0 || *fmt == '@')
             {
+                PSFStackItem item;
+                memset(&item, 0, sizeof(item));
                 item.cmd = -1;
                 item.str = th_strndup(start, fmt - start);
+
                 if (!siStackAddItem(stack, &item))
                     return FALSE;