Skip to content

Commit

Permalink
lib-test: test-exit helper to stop valgrind complaining about fork()s
Browse files Browse the repository at this point in the history
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 <phil@dovecot.fi>
  • Loading branch information
Phil Carmody authored and GitLab committed Aug 11, 2016
1 parent c281d66 commit bd1e1a9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/lib-test/test-common.c
Expand Up @@ -5,7 +5,7 @@
#include "test-common.h"

#include <stdio.h>

#include <unistd.h> /* _exit() */
#include <setjmp.h> /* for fatal tests */

/* To test the firing of i_assert, we need non-local jumps, i.e. setjmp */
Expand Down Expand Up @@ -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);
}
3 changes: 3 additions & 0 deletions src/lib-test/test-common.h
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/lib/test-istream-unix.c
Expand Up @@ -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]);
Expand Down

0 comments on commit bd1e1a9

Please sign in to comment.