failtest: free up everything on exit.
authorRusty Russell <rusty@rustcorp.com.au>
Tue, 18 Jan 2011 00:35:37 +0000 (11:05 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Tue, 18 Jan 2011 00:35:37 +0000 (11:05 +1030)
Otherwise valgrind will report that tests leak memory.

ccan/failtest/failtest.c

index 1127e4fdd7c385973e4d91b3050507c4086968fa..5d7a54f187f1f627ed6c950e9c7f06dfef82b1e1 100644 (file)
@@ -522,12 +522,33 @@ void failtest_init(int argc, char *argv[])
        }
 }
 
        }
 }
 
+/* Free up memory, so valgrind doesn't report leaks. */
+static void free_everything(void)
+{
+       unsigned int i;
+
+       for (i = 0; i < writes_num; i++) {
+               free(writes[i].data);
+               if (writes[i].hdr.offset != (off_t)-1)
+                       free(writes[i].olddata);
+       }
+       free(writes);
+       free(fd_orig);
+       for (i = 0; i < history_num; i++) {
+               if (history[i].type == FAILTEST_OPEN)
+                       free((char *)history[i].u.open.pathname);
+       }
+       free(history);
+}
+
 void failtest_exit(int status)
 {
        unsigned int i;
 
 void failtest_exit(int status)
 {
        unsigned int i;
 
-       if (control_fd == -1)
+       if (control_fd == -1) {
+               free_everything();
                exit(status);
                exit(status);
+       }
 
        if (failtest_exit_check) {
                if (!failtest_exit_check(history, history_num))
 
        if (failtest_exit_check) {
                if (!failtest_exit_check(history, history_num))
@@ -555,6 +576,7 @@ void failtest_exit(int status)
                        close(fd_orig[i].fd);
        }
 
                        close(fd_orig[i].fd);
        }
 
+       free_everything();
        tell_parent(SUCCESS);
        exit(0);
 }
        tell_parent(SUCCESS);
        exit(0);
 }