From: Branden Archer Date: Sun, 19 Jun 2016 20:52:26 +0000 (-0400) Subject: Disallow a test case from being added to a suite twice X-Git-Tag: 0.11.0~21^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a4bd0e16a8b22b64b39051ca8b2b239470e83674;p=check Disallow a test case from being added to a suite twice It was possible to add a test case to a suite more than once. When the suite is freed later on the test case is freed twice, causing a double free error. Although is likely a bug from the test writer, this change allows check to disallow double additions to prevent double freeing a test case. https://github.com/libcheck/check/issues/50 --- diff --git a/src/check.c b/src/check.c index d0bb7ee..020fdfc 100644 --- a/src/check.c +++ b/src/check.c @@ -172,8 +172,11 @@ static void tcase_free(TCase * tc) void suite_add_tcase(Suite * s, TCase * tc) { - if(s == NULL || tc == NULL) + if(s == NULL || tc == NULL || check_list_contains(s->tclst, tc)) + { return; + } + check_list_add_end(s->tclst, tc); } diff --git a/src/check.h.in b/src/check.h.in index d096b2a..b956cf9 100644 --- a/src/check.h.in +++ b/src/check.h.in @@ -145,7 +145,10 @@ CK_DLL_EXP Suite *CK_EXPORT suite_create(const char *name); CK_DLL_EXP int CK_EXPORT suite_tcase(Suite * s, const char *tcname); /** - * Add a test case to a suite + * Add a test case to a suite. + * + * Note that if the TCase has already been added attempting + * to add it again will be ignored. * * @param s suite to add test case to * @param tc test case to add to suite diff --git a/tests/check_check_selective.c b/tests/check_check_selective.c index d07a093..31d5083 100644 --- a/tests/check_check_selective.c +++ b/tests/check_check_selective.c @@ -72,6 +72,12 @@ static void selective_setup (void) suite_add_tcase (s1, tc11); suite_add_tcase (s1, tc12); + /* This line intentionally attempts to add an already + * added test case twice, to ensure it is not added + * again. If it was added again, when the test cases + * are freed a double-free failure will occur. */ + suite_add_tcase (s1, tc12); + /* * Create a test suite 'suite2' with one test case 'test21' * containing two tests.