From b8090d0f5bee66ceb12cee2b63425efd74ad4610 Mon Sep 17 00:00:00 2001 From: Alan Coopersmith Date: Sat, 9 Apr 2022 16:11:45 -0700 Subject: atobm: close memory leaks Fix leaks reported by Oracle Parfait (though they don't last long, since the program exits immediately after leaking): Error: Memory leak Memory leak [memory-leak] (CWE 401): Memory leak of pointer pointer allocated with _new_scan_list(bytes_per_scanline) at line 260 of atobm.c in function 'doit'. calloc called at line 180 in function '_new_scan_list' Allocated value returned to caller at line 193 pointer allocated at line 250 in function 'doit' with _new_scan_list(bytes_per_scanline) pointer leaks when (i + 1) >= at line 275 and buf[0] != 0 at line 225. Error: Memory leak Memory leak [memory-leak] (CWE 401): Memory leak of pointer pointer allocated with _new_scan_list(bytes_per_scanline) at line 316 of atobm.c in function 'doit'. calloc called at line 180 in function '_new_scan_list' Allocated value returned to caller at line 193 pointer allocated at line 250 in function 'doit' with _new_scan_list(bytes_per_scanline) pointer leaks when slist != NULL at line 252 and (i + 1) >= at line 275 and (i + 1) >= at line 303. Signed-off-by: Alan Coopersmith --- atobm.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/atobm.c b/atobm.c index 90440ac..7d25906 100644 --- a/atobm.c +++ b/atobm.c @@ -243,7 +243,7 @@ doit (FILE *fp, *newline = '\0'; len = strlen (cp); - if (width == 0) { + if (head == NULL) { width = len; padded = ((width & 7) != 0); bytes_per_scanline = (len + 7) / 8; @@ -257,7 +257,7 @@ doit (FILE *fp, fprintf (stderr, "%s: line %d is %d characters wide instead of %d\n", ProgramName, lineno, len, width); - return; + goto bail; } if (slist->used + 1 >= slist->allocated) { @@ -266,8 +266,7 @@ doit (FILE *fp, if (!slist) { fprintf (stderr, "%s: unable to allocate scan list\n", ProgramName); - free(old); - return; + goto bail; } } @@ -308,5 +307,11 @@ doit (FILE *fp, } } printf (" };\n"); + bail: + for (slist = head; slist != NULL; slist = head) { + head = slist->next; + free(slist->scanlines); + free(slist); + } return; } -- cgit v1.2.3