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
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);
}
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
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.