]> git.ozlabs.org Git - ccan/blobdiff - ccan/failtest/failtest.c
failtest: use 64 bit offsets if available.
[ccan] / ccan / failtest / failtest.c
index d9a0849daf600d1518514df57b74c71be1b78709..224898f9cb75ad8ad782c0dc983ade12c544cc06 100644 (file)
@@ -1,5 +1,5 @@
 /* Licensed under LGPL - see LICENSE file for details */
-#include "config.h"
+#include <ccan/failtest/failtest.h>
 #include <stdarg.h>
 #include <string.h>
 #include <stdio.h>
@@ -17,7 +17,6 @@
 #include <assert.h>
 #include <ccan/read_write_all/read_write_all.h>
 #include <ccan/failtest/failtest_proto.h>
-#include <ccan/failtest/failtest.h>
 #include <ccan/build_assert/build_assert.h>
 
 enum failtest_result (*failtest_hook)(struct failtest_call *, unsigned);
@@ -310,6 +309,16 @@ static void restore_files(struct saved_file *s)
        }
 }
 
+static void free_files(struct saved_file *s)
+{
+       while (s) {
+               struct saved_file *next = s->next;
+               free(s->contents);
+               free(s);
+               s = next;
+       }
+}
+
 /* Free up memory, so valgrind doesn't report leaks. */
 static void free_everything(void)
 {
@@ -362,7 +371,7 @@ static bool should_fail(struct failtest_call *call)
        struct saved_file *files;
 
        /* Are we probing? */
-       if (probe_count && --probe_count == 0)
+       if (probe_count && --probe_count == 0 && control_fd != -1)
                failtest_cleanup(true, 0);
 
        if (call == &unrecorded_call)
@@ -412,17 +421,22 @@ static bool should_fail(struct failtest_call *call)
                switch (failtest_hook(history, history_num)) {
                case FAIL_OK:
                        break;
+               case FAIL_PROBE:
+                       /* Already down probe path?  Stop now. */
+                       if (!probe_count) {
+                               /* FIXME: We should run *parent* and
+                                * run probe until calls match up again. */
+                               probe_count = 3;
+                               break;
+                       } else {
+                               /* Child should give up now. */
+                               if (control_fd != -1)
+                                       failtest_cleanup(true, 0);
+                               /* Parent, don't fail again. */
+                       }
                case FAIL_DONT_FAIL:
                        call->fail = false;
                        return false;
-               case FAIL_PROBE:
-                       /* Already down probe path?  Stop now. */
-                       if (probe_count)
-                               failtest_cleanup(true, 0);
-                       /* FIXME: We should run *parent* and run probe until
-                        * calls match up again. */
-                       probe_count = 3;
-                       break;
                default:
                        abort();
                }
@@ -470,6 +484,8 @@ static bool should_fail(struct failtest_call *call)
                if (output[1] != STDOUT_FILENO && output[1] != STDERR_FILENO)
                        close(output[1]);
                control_fd = control[1];
+               /* Valgrind spots the leak if we don't free these. */
+               free_files(files);
                return true;
        }