[misc] Fix a memory leak in vsc_error_cleanup().
authorMatthias Bolte <matthias.bolte@googlemail.com>
Sat, 20 Jun 2009 14:15:47 +0000 (16:15 +0200)
committerMatthias Bolte <matthias.bolte@googlemail.com>
Sat, 20 Jun 2009 14:15:47 +0000 (16:15 +0200)
Signed-off-by: Matthias Bolte <matthias.bolte@googlemail.com>

misc/error.c
tests/manual/error.test
tests/manual/error_test.c

index f43c117..df80de7 100644 (file)
@@ -207,10 +207,6 @@ vsc_error_cleanup (struct VscError *error)
                return;
        }
 
-       if (error->message != _error_default_message) {
-               vsc_free (&error->message);
-       }
-
        /*
         * Free the error list messages if they are allocated (not the default
         * error message) and free the error structs too, if they are allocated
@@ -219,29 +215,35 @@ vsc_error_cleanup (struct VscError *error)
         *
         * Cleanup and free branches, because they must always be allocated.
         */
-       current = error->next;
+       current = error;
 
        while (current != NULL) {
                next = current->next;
 
                for (i = 0; i < current->num_branches; ++i) {
-                       vsc_error_free (&current->branches[i]);
+                       if (current->branches[i] != _error_default_pointer) {
+                               vsc_error_free (&current->branches[i]);
+                       }
                }
 
                for (i = 0; i < current->num_more_branches_used; ++i) {
-                       vsc_error_free (&current->more_branches[i]);
+                       if (current->more_branches[i] != _error_default_pointer) {
+                               vsc_error_free (&current->more_branches[i]);
+                       }
                }
 
                if (current->message != _error_default_message) {
                        vsc_free (&current->message);
                }
 
-               if (current != _error_default_pointer) {
+               if (current != error && current != _error_default_pointer) {
                        vsc_free (&current);
                }
 
                current = next;
        }
+
+       vsc_free (&error->more_branches);
 }
 
 
index ea66db2..d1bcb4e 100755 (executable)
Binary files a/tests/manual/error.test and b/tests/manual/error.test differ
index 67305e8..b4ee252 100644 (file)
@@ -109,6 +109,20 @@ main (void)
        VSC__APPEND_ERROR0 (&error, VSC__ERROR_CODE__TRACE);
 
        vsc_error_fprint (&error, stdout);
+       printf ("---------------------------------------------\n");
+
+       for (int i = 0; i < 150; ++i) {
+               branch1 = vsc_error_new();
+
+               VSC__ERROR2 (branch1, VSC__ERROR_CODE__INTERNAL_ERROR,
+                            "Branch %d", 3 + i);
+               vsc_error_append_branch (&error, branch1);
+
+               VSC__APPEND_ERROR0 (branch1, VSC__ERROR_CODE__TRACE);
+               VSC__APPEND_ERROR0 (branch1, VSC__ERROR_CODE__TRACE);
+       }
+
+       vsc_error_fprint (&error, stdout);
 
        vsc_error_cleanup (&error);