]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
unit-tests: Exclude memory checks after-free from AddressSanitizer
authorMartin Willi <martin@revosec.ch>
Wed, 8 Apr 2015 08:16:13 +0000 (10:16 +0200)
committerTobias Brunner <tobias@strongswan.org>
Thu, 15 Sep 2022 10:16:12 +0000 (12:16 +0200)
We explicitly test the memory we free()d if that got properly wiped, so suppress
the warning from AddressSanitizer.

src/libstrongswan/tests/suites/test_chunk.c

index 3f723815268595c5bfadb1e0a25d0e25601659e3..93753c7398331104c5b2b4d26b925b3e8f1879c3 100644 (file)
@@ -125,12 +125,27 @@ END_TEST
  * clear
  */
 
+ADDRESS_SANITIZER_EXCLUDE
+static bool cleared(u_char *ptr)
+{
+       int i;
+
+       for (i = 0; i < 64; i += 2)
+       {
+               if (ptr[i] != 0 && ptr[i] == i &&
+                       ptr[i+1] != 0 && ptr[i+1] == i+1)
+               {
+                       return FALSE;
+               }
+       }
+       return TRUE;
+}
+
 START_TEST(test_chunk_clear)
 {
        chunk_t chunk;
        u_char *ptr;
        int i;
-       bool cleared = TRUE;
 
        chunk = chunk_empty;
        chunk_clear(&chunk);
@@ -147,17 +162,8 @@ START_TEST(test_chunk_clear)
         * test directly, as it might allocate data at the freed area.  comparing
         * two bytes at once reduces the chances of conflicts if memory got
         * overwritten already */
-       for (i = 0; i < 64; i += 2)
-       {
-               if (ptr[i] != 0 && ptr[i] == i &&
-                       ptr[i+1] != 0 && ptr[i+1] == i+1)
-               {
-                       cleared = FALSE;
-                       break;
-               }
-       }
+       ck_assert(cleared(ptr));
        assert_chunk_empty(chunk);
-       ck_assert(cleared);
 }
 END_TEST
 
@@ -199,6 +205,27 @@ END_TEST
  * chunk_create_cat
  */
 
+ADDRESS_SANITIZER_EXCLUDE
+bool chunk_equals_nosan(chunk_t a, chunk_t b)
+{
+       int i;
+
+       /* cant use memcmp() or any function using it, as that is again
+        * sanitize-checked */
+       if (a.len != b.len)
+       {
+               return FALSE;
+       }
+       for (i = 0; i < b.len; i++)
+       {
+               if (a.ptr[i] != b.ptr[i])
+               {
+                       return FALSE;
+               }
+       }
+       return TRUE;
+}
+
 START_TEST(test_chunk_create_cat)
 {
        chunk_t foo, bar;
@@ -236,8 +263,8 @@ START_TEST(test_chunk_create_cat)
        ck_assert_int_eq(c.len, 6);
        ck_assert(chunk_equals(c, chunk_from_str("foobar")));
        /* check memory area of cleared chunk */
-       ck_assert(!chunk_equals(foo, chunk_create(ptra, 3)));
-       ck_assert(!chunk_equals(bar, chunk_create(ptrb, 3)));
+       ck_assert(!chunk_equals_nosan(foo, chunk_create(ptra, 3)));
+       ck_assert(!chunk_equals_nosan(bar, chunk_create(ptrb, 3)));
 }
 END_TEST