Skip to content

Commit

Permalink
tests: add build time option for emulating malloc(0) behavior
Browse files Browse the repository at this point in the history
Allow to override malloc/calloc functions to provide a version that
behaves like esp-idf one, when malloc(0) is called.

Signed-off-by: Davide Bettio <[email protected]>
  • Loading branch information
bettio committed Sep 23, 2024
1 parent be8797b commit e13c743
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions tests/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,35 @@ struct Test
#define SKIP_STACKTRACES false
#endif

// Enabling this will override malloc and calloc weak symbols,
// so we can force an alternative version of malloc that returns
// NULL when size is 0.
// This is useful to find debugging or finding some kind of issues.
#ifdef FORCE_MALLOC_ZERO_RETURNS_NULL
// required for memalign
#include <malloc.h>

void *malloc(size_t size)
{
if (size == 0) {
return 0;
} else {
return memalign(sizeof(void *), size);
}
}

void *calloc(size_t nmemb, size_t size)
{
if (size == 0) {
return 0;
} else {
void *ptr = memalign(sizeof(void *), nmemb * size);
memset(ptr, 0, nmemb * size);
return ptr;
}
}
#endif

struct Test tests[] = {
TEST_CASE_EXPECTED(add, 17),
TEST_CASE_EXPECTED(fact, 120),
Expand Down

0 comments on commit e13c743

Please sign in to comment.