From bd1e1a9720ce4617f7d97be75cffc45e905bb791 Mon Sep 17 00:00:00 2001 From: Phil Carmody Date: Tue, 9 Aug 2016 18:29:19 +0300 Subject: [PATCH] lib-test: test-exit helper to stop valgrind complaining about fork()s fork()s that want to _exit() will cause valgrind's full memory leak checker to complain a lot - this performs a quick cleanup first. Before: phil@phil:~/repos/dovecot-core$ valgrind --trace-children=yes --leak-check=full --show-leak-kinds=all src/lib/test-lib --match unix ==19576== total heap usage: 4 allocs, 0 frees, 17,858 bytes allocated istream unix ......................................................... : ok 0 / 1 tests failed ==19575== total heap usage: 7 allocs, 7 frees, 19,327 bytes allocated After: phil@phil:~/repos/dovecot-core$ valgrind --trace-children=yes --leak-check=full --show-leak-kinds=all src/lib/test-lib --match unix istream unix ......................................................... : ok ==4993== total heap usage: 4 allocs, 4 frees, 17,858 bytes allocated 0 / 1 tests failed ==4992== total heap usage: 7 allocs, 7 frees, 19,327 bytes allocated Signed-off-by: Phil Carmody --- src/lib-test/test-common.c | 12 +++++++++++- src/lib-test/test-common.h | 3 +++ src/lib/test-istream-unix.c | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/lib-test/test-common.c b/src/lib-test/test-common.c index e2b0a25880..02493b2fb4 100644 --- a/src/lib-test/test-common.c +++ b/src/lib-test/test-common.c @@ -5,7 +5,7 @@ #include "test-common.h" #include - +#include /* _exit() */ #include /* for fatal tests */ /* To test the firing of i_assert, we need non-local jumps, i.e. setjmp */ @@ -448,3 +448,13 @@ int test_run_named_with_fatals(const char *match, struct named_test tests[], test_run_named_fatals(fatals, match); return test_deinit(); } + +void ATTR_NORETURN +test_exit(int status) +{ + i_free_and_null(expected_error_str); + i_free_and_null(test_prefix); + (void)t_pop(); /* as we were within a T_BEGIN { tests[i].func(); } T_END */ + lib_deinit(); + _exit(status); +} diff --git a/src/lib-test/test-common.h b/src/lib-test/test-common.h index b956e7d78a..2afddbcc3f 100644 --- a/src/lib-test/test-common.h +++ b/src/lib-test/test-common.h @@ -73,4 +73,7 @@ int test_run_named_with_fatals(const char *match, struct named_test tests[], #define FATAL_NAMELESS(x) x, /* Were you to want to use the X trick but not name the tests */ #define FATAL_NAMED(x) { .name = #x , .func = x }, +/* If a fork() wants to exit(), then this will avoid valgrind leak errors */ +void test_exit(int status) ATTR_NORETURN; + #endif diff --git a/src/lib/test-istream-unix.c b/src/lib/test-istream-unix.c index a0890bf50d..dadf42c919 100644 --- a/src/lib/test-istream-unix.c +++ b/src/lib/test-istream-unix.c @@ -176,7 +176,7 @@ void test_istream_unix(void) case 0: i_close_fd(&fd[0]); test_istream_unix_client(fd[1]); - _exit(0); + test_exit(0); default: i_close_fd(&fd[1]); test_istream_unix_server(fd[0]);