]> granicus.if.org Git - check/commitdiff
Disallow a test case from being added to a suite twice
authorBranden Archer <b.m.archer4@gmail.com>
Sun, 19 Jun 2016 20:52:26 +0000 (16:52 -0400)
committerBranden Archer <b.m.archer4@gmail.com>
Sun, 19 Jun 2016 20:52:26 +0000 (16:52 -0400)
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

src/check.c
src/check.h.in
tests/check_check_selective.c

index d0bb7ee353ed5dea06a2098063025858fc218b00..020fdfcad4d5cf8d48035717f3f503010b22b9c4 100644 (file)
@@ -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);
 }
 
index d096b2ad45c362309ade0f591f21904471f642f9..b956cf9740b0d05db66b5fe8ba40659daa00eb08 100644 (file)
@@ -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
index d07a0937fbd83549d0f5bc9d851c96d6e08042ff..31d5083a09dd12d0c63c86a1b58b135233d46785 100644 (file)
@@ -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.