Mercurial > hg > sidinfo
comparison sidinfo.c @ 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 | c9cc62e7f221 |
children | 86f5c9ce26a8 |
comparison
equal
deleted
inserted
replaced
299:b3d867a0b92c | 300:85d92bcbebf3 |
---|---|
340 { | 340 { |
341 if (stack->nitems > 0 && stack->items != NULL) | 341 if (stack->nitems > 0 && stack->items != NULL) |
342 { | 342 { |
343 for (int n = 0; n < stack->nitems; n++) | 343 for (int n = 0; n < stack->nitems; n++) |
344 { | 344 { |
345 if (stack->items[n].cmd == -1) | 345 PSFStackItem *item = &stack->items[n]; |
346 th_free(stack->items[n].str); | 346 th_free(item->str); |
347 th_free(item->fmt); | |
347 } | 348 } |
348 th_free(stack->items); | 349 th_free(stack->items); |
349 } | 350 } |
351 | |
352 // Clear the stack data | |
350 memset(stack, 0, sizeof(PSFStack)); | 353 memset(stack, 0, sizeof(PSFStack)); |
351 } | 354 } |
352 } | 355 } |
353 | 356 |
354 | 357 |
357 const char *start = fmt; | 360 const char *start = fmt; |
358 siClearStack(stack); | 361 siClearStack(stack); |
359 | 362 |
360 while (*start) | 363 while (*start) |
361 { | 364 { |
362 PSFStackItem item; | |
363 const char *end = strchr(start, ','); | 365 const char *end = strchr(start, ','); |
364 char *field = (end != NULL) ? | 366 char *field = (end != NULL) ? |
365 th_strndup_trim(start, end - start, TH_TRIM_BOTH) : | 367 th_strndup_trim(start, end - start, TH_TRIM_BOTH) : |
366 th_strdup_trim(start, TH_TRIM_BOTH); | 368 th_strdup_trim(start, TH_TRIM_BOTH); |
367 | 369 |
368 if (field != NULL) | 370 if (field != NULL) |
369 { | 371 { |
372 PSFStackItem item; | |
370 int found = argMatchPSFieldError(field); | 373 int found = argMatchPSFieldError(field); |
371 th_free(field); | 374 th_free(field); |
372 | 375 |
373 if (found < 0) | 376 if (found < 0) |
374 return FALSE; | 377 return FALSE; |
375 | 378 |
379 memset(&item, 0, sizeof(item)); | |
376 item.cmd = found; | 380 item.cmd = found; |
377 item.flags = 0; | |
378 item.fmt = NULL; | |
379 item.str = NULL; | |
380 | 381 |
381 if (!siStackAddItem(stack, &item)) | 382 if (!siStackAddItem(stack, &item)) |
382 return FALSE; | 383 return FALSE; |
383 } | 384 } |
384 | 385 |
732 // | 733 // |
733 // Parse a format string into a PSFStack structure | 734 // Parse a format string into a PSFStack structure |
734 // | 735 // |
735 static BOOL argParsePSFormatStr(PSFStack *stack, const char *fmt) | 736 static BOOL argParsePSFormatStr(PSFStack *stack, const char *fmt) |
736 { | 737 { |
737 PSFStackItem item; | |
738 const char *start = NULL; | 738 const char *start = NULL; |
739 int mode = 0; | 739 int mode = 0; |
740 BOOL rval = TRUE; | 740 BOOL rval = TRUE; |
741 | 741 |
742 siClearStack(stack); | 742 siClearStack(stack); |
768 } | 768 } |
769 | 769 |
770 if (fmt - start == 0) | 770 if (fmt - start == 0) |
771 { | 771 { |
772 // "@@" sequence, just print out @ | 772 // "@@" sequence, just print out @ |
773 PSFStackItem item; | |
774 memset(&item, 0, sizeof(item)); | |
773 item.cmd = -2; | 775 item.cmd = -2; |
774 item.str = NULL; | |
775 item.chr = '@'; | 776 item.chr = '@'; |
777 | |
776 if (!siStackAddItem(stack, &item)) | 778 if (!siStackAddItem(stack, &item)) |
777 return FALSE; | 779 return FALSE; |
778 } | 780 } |
779 else | 781 else |
780 { | 782 { |
786 } | 788 } |
787 | 789 |
788 int ret = argMatchPSFieldError(field); | 790 int ret = argMatchPSFieldError(field); |
789 if (ret >= 0) | 791 if (ret >= 0) |
790 { | 792 { |
793 PSFStackItem item; | |
794 memset(&item, 0, sizeof(item)); | |
791 item.cmd = ret; | 795 item.cmd = ret; |
792 item.flags = 0; | |
793 item.fmt = NULL; | |
794 item.str = NULL; | |
795 | 796 |
796 if (fopt != NULL) | 797 if (fopt != NULL) |
797 { | 798 { |
798 if (siItemFormatStrCheck(fopt, &optPSOptions[item.cmd])) | 799 if (siItemFormatStrCheck(fopt, &optPSOptions[item.cmd])) |
799 { | 800 { |
822 break; | 823 break; |
823 | 824 |
824 case 2: | 825 case 2: |
825 if (*fmt == 0 || *fmt == '@') | 826 if (*fmt == 0 || *fmt == '@') |
826 { | 827 { |
828 PSFStackItem item; | |
829 memset(&item, 0, sizeof(item)); | |
827 item.cmd = -1; | 830 item.cmd = -1; |
828 item.str = th_strndup(start, fmt - start); | 831 item.str = th_strndup(start, fmt - start); |
832 | |
829 if (!siStackAddItem(stack, &item)) | 833 if (!siStackAddItem(stack, &item)) |
830 return FALSE; | 834 return FALSE; |
831 | 835 |
832 mode = (*fmt == 0) ? -1 : 0; | 836 mode = (*fmt == 0) ? -1 : 0; |
833 } | 837 } |