correct memory leaks
authorLaurent Mazet <mazet@softndesign.org>
Fri, 22 Dec 2023 09:41:59 +0000 (10:41 +0100)
committerLaurent Mazet <mazet@softndesign.org>
Fri, 22 Dec 2023 09:41:59 +0000 (10:41 +0100)
bf.c

diff --git a/bf.c b/bf.c
index 41882a8cd92a08995a34e018fb7a940d72aea832..e06041b8cb2949553336b80af19af951b3b6ac50 100644 (file)
--- a/bf.c
+++ b/bf.c
@@ -2,7 +2,7 @@
 /* cflags: */
 /* linker: debug.o */
 
-#include <assert.h>
+#include <errno.h>
 #include <getopt.h>
 #include <malloc.h>
 #include <stdio.h>
@@ -248,6 +248,7 @@ int main (int argc, char *argv[])
         fid = fopen (output, "w");
         if (fid == NULL) {
             VERBOSE (ERROR, fprintf (stderr, "%s: can't open file '%s' for writing\n", progname, output));
+            if (buffer) free (buffer);
             return 1;
         }
     } else {
@@ -262,6 +263,11 @@ int main (int argc, char *argv[])
         fclose (fid);
     }
 
+    /* free buffer */
+    if (buffer) {
+        free (buffer);
+    }
+
     VERBOSE (INFO, fprintf (stdout, "\nmemory:"); int _i; for (_i = 0; _i < MEMSIZE; _i++) fprintf (stdout," %d", mem[_i]); fprintf (stdout,"\n"));
 
     return rc;