]> git.codecow.com Git - Monocypher.git/commitdiff
Don't free() NULL pointers
authorLoup Vaillant <loup@loup-vaillant.fr>
Sun, 24 Jun 2018 13:58:55 +0000 (15:58 +0200)
committerLoup Vaillant <loup@loup-vaillant.fr>
Sun, 24 Jun 2018 13:58:55 +0000 (15:58 +0200)
The alloc() function in the test suite unconditionally succeeds when
trying to allocate zero bytes.  It does so by returning NULL right away,
without exiting the program.  This was for portability for platforms
that refuse to allocate zero bytes.

Unfortunately, this meant that the test suite later called free() on
those NULL pointers, which is undefined.  Wrapping free() in a dealloc()
function avoids this error.

tests/test.c

index 0f50d1376f3d98fd042cb2165592da1d9ffc931b..7bb5769b997949cc2aab79ce4b05a0a17d0fc22f 100644 (file)
@@ -34,6 +34,13 @@ static void* alloc(size_t size)
     return buf;
 }
 
+static void dealloc(void *ptr)
+{
+    if (ptr != 0) {
+        free(ptr);
+    }
+}
+
 typedef struct {
     u8     *buf;
     size_t  size;
@@ -73,11 +80,11 @@ static int test(void (*f)(const vector[], vector*),
         if (out.size != 0) {
             status |= memcmp(out.buf, expected.buf, out.size);
         }
-        free(out.buf);
+        dealloc(out.buf);
         idx += nb_inputs + 1;
         nb_tests++;
     }
-    free(in);
+    dealloc(in);
     printf("%s %4d tests: %s\n",
            status != 0 ? "FAILED" : "OK", nb_tests, name);
     return status;
@@ -163,7 +170,7 @@ static void argon2i(const vector in[], vector *out)
                            salt    ->buf, salt    ->size,
                            key     ->buf, key     ->size,
                            ad      ->buf, ad      ->size);
-    free(work_area);
+    dealloc(work_area);
 }
 
 static void x25519(const vector in[], vector *out)
@@ -548,7 +555,7 @@ static int p_argon2i_easy()
                            password, 32, salt, 16, 0, 0, 0, 0);
     crypto_argon2i(hash_easy, 32, work_area, 8, 1, password, 32, salt, 16);
     status |= memcmp(hash_general, hash_easy, 32);
-    free(work_area);
+    dealloc(work_area);
     printf("%s: Argon2i (easy interface)\n", status != 0 ? "FAILED" : "OK");
     return status;
 }
@@ -580,8 +587,8 @@ static int p_argon2i_overlap()
                                work_area +   ad_offset, 32);
         status |= memcmp(hash1, hash2, 32);
     }
-    free(work_area);
-    free(clean_work_area);
+    dealloc(work_area);
+    dealloc(clean_work_area);
     printf("%s: Argon2i (overlaping i/o)\n", status != 0 ? "FAILED" : "OK");
     return status;
 }