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 <davide@uninstall.it>
  • Loading branch information
bettio committed Sep 22, 2024
1 parent 3e42afa commit 2fee31b
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions tests/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,34 @@ struct Test
#define SKIP_STACKTRACES false
#endif

#include <malloc.h>

// 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
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 2fee31b

Please sign in to comment.