From: Loup Vaillant Date: Sun, 24 Jun 2018 13:58:55 +0000 (+0200) Subject: Don't free() NULL pointers X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=79f8285ea6cae0db2e906bf3eb65c8fcadabdaf8;p=Monocypher.git Don't free() NULL pointers 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. --- diff --git a/tests/test.c b/tests/test.c index 0f50d13..7bb5769 100644 --- a/tests/test.c +++ b/tests/test.c @@ -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; }