From 624e9c664aa9043168c24170531f2a364ac5a94a Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Mon, 22 May 2023 12:53:10 +0200 Subject: [PATCH] tests/sys/malloc: fix test with LLVM --- tests/sys/malloc/main.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/tests/sys/malloc/main.c b/tests/sys/malloc/main.c index 9728efa30d..0195efbb57 100644 --- a/tests/sys/malloc/main.c +++ b/tests/sys/malloc/main.c @@ -109,10 +109,8 @@ static void free_memory(struct node *head) printf("Free count: %"PRIu32"\n", freed); } -int main(void) +static void check_calloc_returns_null_on_overflow(void) { - uint32_t allocations = 0; - /* modern compilers warn about nonsense calls to calloc, but this is exactly what we want to * test */ #pragma GCC diagnostic push @@ -121,8 +119,24 @@ int main(void) #endif /* test if an overflow is correctly detected by calloc(): the size below overflows by 1 byte */ /* cppcheck-suppress leakReturnValNotUsed; (should return NULL, so nothing to free anyway) */ - expect(NULL == calloc(SIZE_MAX / 16 + 1, 16)); + size_t nmemb = SIZE_MAX / 16 + 1; + size_t size = 16; + void *p = calloc(nmemb, size); + + /* When clang detects that the memory allocated is not actually used, it + * will optimize out the call to `calloc()` and just assume that the + * allocation succeeded. It then optimized out the test `NULL == p` and + * assumes it to always be false. We just print the address to prevent + * that from happening */ + printf("calloc(%zu, %zu) = %p\n", nmemb, size, p); + expect(NULL == p); #pragma GCC diagnostic pop +} + +int main(void) +{ + uint32_t allocations = 0; + check_calloc_returns_null_on_overflow(); printf("CHUNK_SIZE: %"PRIu32"\n", (uint32_t)CHUNK_SIZE); printf("NUMBER_OF_TESTS: %d\n", NUMBER_OF_TESTS);