mirror of
https://github.com/cryb-to/cryb-to.git
synced 2024-11-15 02:05:40 +00:00
Move the decision of whether to check for memory leaks from the malloc()
code to the driver and allow an environment variable to override the default (on unless coverage is enabled).
This commit is contained in:
parent
56eccb99fe
commit
82e32027af
2 changed files with 20 additions and 13 deletions
17
t/t_main.c
17
t/t_main.c
|
@ -175,6 +175,7 @@ int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
unsigned int n, pass, fail;
|
unsigned int n, pass, fail;
|
||||||
|
size_t nt;
|
||||||
int opt;
|
int opt;
|
||||||
|
|
||||||
/* make all unintentional allocation failures fatal */
|
/* make all unintentional allocation failures fatal */
|
||||||
|
@ -210,8 +211,18 @@ main(int argc, char *argv[])
|
||||||
if (t_plan_len == 0)
|
if (t_plan_len == 0)
|
||||||
errx(1, "no plan\n");
|
errx(1, "no plan\n");
|
||||||
|
|
||||||
|
/* do not check for leaks when doing coverage analysis */
|
||||||
|
nt = t_plan_len;
|
||||||
|
#if CRYB_COVERAGE
|
||||||
|
nt = t_str_is_true(getenv("CRYB_LEAKTEST")) ?
|
||||||
|
t_plan_len + 1 : t_plan_len;
|
||||||
|
#else
|
||||||
|
nt = t_str_is_false(getenv("CRYB_LEAKTEST")) ?
|
||||||
|
t_plan_len : t_plan_len + 1;
|
||||||
|
#endif
|
||||||
|
|
||||||
/* run the tests */
|
/* run the tests */
|
||||||
printf("1..%zu\n", t_plan_len + 1);
|
printf("1..%zu\n", nt);
|
||||||
for (n = pass = fail = 0; n < t_plan_len; ++n)
|
for (n = pass = fail = 0; n < t_plan_len; ++n)
|
||||||
t_run_test(t_plan[n], n + 1) ? ++pass : ++fail;
|
t_run_test(t_plan[n], n + 1) ? ++pass : ++fail;
|
||||||
|
|
||||||
|
@ -224,8 +235,10 @@ main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
free(t_plan);
|
free(t_plan);
|
||||||
setvbuf(stdout, NULL, _IONBF, 0);
|
setvbuf(stdout, NULL, _IONBF, 0);
|
||||||
|
if (nt > t_plan_len) {
|
||||||
if (verbose)
|
if (verbose)
|
||||||
t_malloc_printstats(stderr);
|
t_malloc_printstats(stderr);
|
||||||
t_run_test(&t_memory_leak, t_plan_len + 1) ? ++pass : ++fail;
|
t_run_test(&t_memory_leak, nt) ? ++pass : ++fail;
|
||||||
|
}
|
||||||
exit(fail > 0 ? 1 : 0);
|
exit(fail > 0 ? 1 : 0);
|
||||||
}
|
}
|
||||||
|
|
12
t/t_malloc.c
12
t/t_malloc.c
|
@ -478,7 +478,6 @@ t_malloc_printstats(FILE *f)
|
||||||
static int
|
static int
|
||||||
t_malloc_leaked(char **desc, void *arg CRYB_UNUSED)
|
t_malloc_leaked(char **desc, void *arg CRYB_UNUSED)
|
||||||
{
|
{
|
||||||
#if !CRYB_COVERAGE
|
|
||||||
struct bucket *b;
|
struct bucket *b;
|
||||||
unsigned int shift;
|
unsigned int shift;
|
||||||
unsigned long nleaked;
|
unsigned long nleaked;
|
||||||
|
@ -487,16 +486,11 @@ t_malloc_leaked(char **desc, void *arg CRYB_UNUSED)
|
||||||
b = &buckets[shift];
|
b = &buckets[shift];
|
||||||
nleaked += b->nalloc - b->nfree;
|
nleaked += b->nalloc - b->nfree;
|
||||||
}
|
}
|
||||||
if (nleaked > 0) {
|
if (nleaked > 0)
|
||||||
asprintf(desc, "%lu allocation(s) leaked", nleaked);
|
asprintf(desc, "%lu allocation(s) leaked", nleaked);
|
||||||
return (0);
|
else
|
||||||
} else {
|
|
||||||
asprintf(desc, "%s", "no memory leaked");
|
asprintf(desc, "%s", "no memory leaked");
|
||||||
return (1);
|
return (nleaked == 0);
|
||||||
}
|
|
||||||
#else
|
|
||||||
return (-1);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct t_test t_memory_leak = {
|
struct t_test t_memory_leak = {
|
||||||
|
|
Loading…
Reference in a new issue