fix memory leaks
authorLaurent Mazet <mazet@softndesign.org>
Tue, 25 Jun 2024 21:25:04 +0000 (23:25 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Tue, 25 Jun 2024 21:25:04 +0000 (23:25 +0200)
function.c
function.h
gameoflife.c

index 2ce081676945b8ea611b3c06758269f257d0f733..43e61a6285c171de26d9a33d0bbf9a47a70ab250 100644 (file)
@@ -200,7 +200,7 @@ board_t *loadboard (char *str, char **pname)
         board = initboard (xsize, ysize);
         memcpy (board->tab, tab, xsize * ysize);
     }
-    if ((board != NULL) && (*pname != NULL)) {
+    if ((board != NULL) && (pname != NULL)) {
         *pname = strdup (name);
     }
 
@@ -356,4 +356,69 @@ board_t *putelement (board_t *board, board_t *element, int x, int y, int mode)
     return board;
 }
 
+board_t *processfile (char *filename, char **pname)
+{
+    char *buffer = readdata (filename);
+    if (buffer == NULL) {
+        VERBOSE (ERROR, fprintf (stderr, "can't read file (%s)\n", filename));
+        return NULL;
+    }
+
+    board_t *board = loadboard (buffer, pname);
+    if (board == NULL) {
+        VERBOSE (ERROR, fprintf (stderr, "incorrect file (%s)\n", filename));
+    }
+
+    free (buffer);
+
+    return board;
+}
+
+library_t *processlibrary (char *libname)
+{
+    int n = 0;
+    library_t *lib = (library_t *) malloc ((n + 2) * sizeof (library_t));
+    CHECKALLOC (lib);
+
+    (lib + n)->board = initboard (3, 3);
+    (lib + n)->board->tab[4] = 'X';
+    (lib + n)->name = strdup ("Cell");
+    memset (lib + n + 1, 0, sizeof (library_t));
+    n++;
+
+    if (libname) {
+        char *saveptr;
+        char *filename = strtok_r (libname, ":", &saveptr);
+        while (filename) {
+            char *name;
+            lib = (library_t *) realloc (lib, (n + 2) * sizeof (library_t));
+            CHECKALLOC (lib);
+
+            (lib + n)->board = processfile (filename, &name);
+            if ((lib + n)->board != NULL) {
+                (lib + n)->name = name;
+                memset (lib + n + 1, 0, sizeof (library_t));
+                n++;
+            }
+
+            filename = strtok_r (NULL, ":", &saveptr);
+        }
+    }
+
+    return lib;
+}
+
+void freelibrary (library_t *lib)
+{
+    int n = 0;
+
+    while ((lib + n)->board) {
+        freeboard ((lib + n)->board);
+        free ((lib + n)->name);
+        n++;
+    }
+
+    free (lib);
+}
+
 /* vim: set ts=4 sw=4 et: */
index 5438c17394f63f4105737d81ff76a7440da46cbd..7179b71e6127bd0d56f1888d345763ed676da532 100644 (file)
@@ -39,6 +39,12 @@ board_t *mirrorelement (board_t *element, int mode);
 
 board_t *putelement (board_t *board, board_t *element, int x, int y, int mode);
 
+board_t *processfile (char *filename, char **pname);
+
+library_t *processlibrary (char *libname);
+
+void freelibrary (library_t *lib);
+
 #endif /* __FUNCTION_H__ */
 
 /* vim: set ts=4 sw=4 et: */
index 7452e6e34ef36bd3a719c5fa6cf9605819c10ec5..bd7b4d87668a77433866774710023d946fb4c037 100644 (file)
@@ -66,58 +66,6 @@ int usage (int ret)
     return ret;
 }
 
-board_t *processfile (char *filename, char **pname)
-{
-    char *buffer = readdata (filename);
-    if (buffer == NULL) {
-        VERBOSE (ERROR, fprintf (stderr, "can't read file (%s)\n", filename));
-        return NULL;
-    }
-
-    board_t *board = loadboard (buffer, pname);
-    if (board == NULL) {
-        VERBOSE (ERROR, fprintf (stderr, "incorrect file (%s)\n", filename));
-    }
-
-    free (buffer);
-
-    return board;
-}
-
-library_t *processlibrary (char *libname)
-{
-    int n = 0;
-    library_t *lib = (library_t *) malloc ((n + 2) * sizeof (library_t));
-    CHECKALLOC (lib);
-
-    (lib + n)->board = initboard (3, 3);
-    (lib + n)->board->tab[4] = 'X';
-    (lib + n)->name = strdup ("Cell");
-    memset (lib + n + 1, 0, sizeof (library_t));
-    n++;
-
-    if (libname) {
-        char *saveptr;
-        char *filename = strtok_r (libname, ":", &saveptr);
-        while (filename) {
-            char *name;
-            lib = (library_t *) realloc (lib, (n + 2) * sizeof (library_t));
-            CHECKALLOC (lib);
-        
-            (lib + n)->board = processfile (filename, &name);
-            if ((lib + n)->board != NULL) {
-                (lib + n)->name = name;
-                memset (lib + n + 1, 0, sizeof (library_t));
-                n++;
-            }
-
-            filename = strtok_r (NULL, ":", &saveptr);
-        }
-    }
-
-    return lib;
-}
-
 /* main function */
 int main (int argc, char *argv[])
 {
@@ -201,11 +149,6 @@ int main (int argc, char *argv[])
         return 1;
     }
 
-    /* load library */
-    library_t *lib = processlibrary (listoffilename);
-    int n = 0;
-    board_t *element = (lib + n)->board;
-
     /* load playground */
     board_t *playground = NULL;
     if (playname) {
@@ -215,6 +158,11 @@ int main (int argc, char *argv[])
         }
     }
 
+    /* load library */
+    library_t *lib = processlibrary (listoffilename);
+    int n = 0;
+    board_t *element = (lib + n)->board;
+
     /* init curses window */
     initscr ();
     noecho ();
@@ -373,20 +321,15 @@ int main (int argc, char *argv[])
 
     endwin ();
 
-    n = 0;
-    while ((lib + n)->board) {
-        freeboard ((lib + n)->board);
-        free ((lib + n)->name);
-    }
-    free (lib);
+    freelibrary (lib);
     freeboard (board);
 
     return 0;
 }
 
 /* test: gameoflife.exe -e 2>&1 | grep 'no board' */
-/* test: gameoflife.exe -e nofile.gol 2>&1 | grep "can't read file" */
-/* test: gameoflife.exe -e bogus.gol 2>&1 | grep 'incorrect file' */
+/* test: echo -n q | gameoflife.exe -e nofile.gol 2>&1 | grep "can't read file" */
+/* test: echo -n q | gameoflife.exe -e bogus.gol 2>&1 | grep 'incorrect file' */
 /* test: gameoflife.exe -h | grep usage */
 /* test: gameoflife.exe -p 2>&1 | grep 'no playground' */
 /* test: gameoflife.exe -p nofile.gol 2>&1 | grep "can't read file" */
@@ -397,10 +340,10 @@ int main (int argc, char *argv[])
 /* test: gameoflife.exe -x 2>&1 | grep 'no width' */
 /* test: gameoflife.exe -y 2>&1 | grep 'no height' */
 /* test: gameoflife.exe _ 2>&1 | grep invalid */
-/* test: echo -n q | gameoflife.exe -e glider.gol */
-/* test: { echo clllldckkkrcjjjjcxfcilililecs; sleep 5; echo -n q; } | gameoflife.exe -e glider.gol -s 2 */
+/* test: echo -n q | gameoflife.exe */
+/* test: { echo oclllldckkkrcjjjjcxfcilililecs; sleep 5; echo -n q; } | gameoflife.exe -e glider.gol -s 2 */
 /* test: { echo icklckcjcjckjs; sleep 2; echo; sleep 1; echo -n q; } | gameoflife.exe -x 10 -y 10 -v 2 */
 /* test: { sleep 4; echo -n q; } | gameoflife.exe -p board.gol -s 1 */
-/* test: { echo cllllrckkkkjjd; sleep 3; echo -n q; } | gameoflife.exe -e beehive.gol -s 1 */
+/* test: { echo uucllllrckkkkjjd; sleep 3; echo -n q; } | gameoflife.exe -e beehive.gol:block.gol -s 1 */
 
 /* vim: set ts=4 sw=4 et: */