Commit 460f62ce authored by Rusty Russell's avatar Rusty Russell

failtest: detect leaks in children.

If we need to clean up the children, they didn't exit cleanly.

This takes a bit more care when writing tests, but found a leak in tdb2.
parent c9f915e0
...@@ -317,21 +317,28 @@ static NORETURN void failtest_cleanup(bool forced_cleanup, int status) ...@@ -317,21 +317,28 @@ static NORETURN void failtest_cleanup(bool forced_cleanup, int status)
{ {
int i; int i;
/* For children, we don't care if they "failed" the testing. */
if (control_fd != -1)
status = 0;
if (forced_cleanup) if (forced_cleanup)
history_num--; history_num--;
/* Cleanup everything, in reverse order. */ /* Cleanup everything, in reverse order. */
for (i = history_num - 1; i >= 0; i--) for (i = history_num - 1; i >= 0; i--) {
if (history[i].cleanup) if (!history[i].cleanup)
history[i].cleanup(&history[i].u); continue;
if (!forced_cleanup) {
printf("Leak at %s:%u\n",
history[i].file, history[i].line);
status = 1;
}
history[i].cleanup(&history[i].u);
}
free_everything(); free_everything();
if (control_fd == -1)
exit(status);
tell_parent(SUCCESS); tell_parent(SUCCESS);
exit(0); exit(status);
} }
static bool should_fail(struct failtest_call *call) static bool should_fail(struct failtest_call *call)
......
...@@ -7,15 +7,16 @@ ...@@ -7,15 +7,16 @@
/* Include the C files directly. */ /* Include the C files directly. */
#include <ccan/failtest/failtest.c> #include <ccan/failtest/failtest.c>
int main(void) int main(int argc, char *argv[])
{ {
int fd; int fd;
char *p; char *p;
char buf[] = "Hello world!"; char buf[] = "Hello world!";
plan_tests(5); plan_tests(5);
failtest_init(argc, argv);
fd = failtest_open("run-write-scratchpad", "run-write.c", 1, fd = failtest_open("run-write-scratchpad", __FILE__, __LINE__,
O_RDWR|O_CREAT, 0600); O_RDWR|O_CREAT, 0600);
/* Child will fail, ignore. */ /* Child will fail, ignore. */
if (fd < 0) if (fd < 0)
...@@ -23,14 +24,16 @@ int main(void) ...@@ -23,14 +24,16 @@ int main(void)
write(fd, buf, strlen(buf)); write(fd, buf, strlen(buf));
ok1(lseek(fd, 0, SEEK_CUR) == strlen(buf)); ok1(lseek(fd, 0, SEEK_CUR) == strlen(buf));
p = failtest_malloc(100, "run-write.c", 1); p = failtest_malloc(100, __FILE__, __LINE__);
if (!p) { if (!p) {
/* We are the child. Do a heap of writes. */ /* We are the child. Do a heap of writes. */
unsigned int i; unsigned int i;
for (i = 0; i < strlen(buf)+1; i++) for (i = 0; i < strlen(buf)+1; i++)
if (failtest_write(fd, "x", 1, "run-write.c", 1) == 1) if (failtest_write(fd, "x", 1, __FILE__, __LINE__)
== 1)
break; break;
failtest_close(fd, __FILE__, __LINE__);
failtest_exit(0); failtest_exit(0);
} }
...@@ -41,6 +44,7 @@ int main(void) ...@@ -41,6 +44,7 @@ int main(void)
lseek(fd, 0, SEEK_SET); lseek(fd, 0, SEEK_SET);
ok1(read(fd, buf, strlen(buf)) == strlen("Hello world!")); ok1(read(fd, buf, strlen(buf)) == strlen("Hello world!"));
ok1(strcmp(buf, "Hello world!") == 0); ok1(strcmp(buf, "Hello world!") == 0);
failtest_close(fd, __FILE__, __LINE__);
return exit_status(); return exit_status();
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment