diff --git a/components/heap/multi_heap_poisoning.c b/components/heap/multi_heap_poisoning.c index 8705ac9eb..7e43be96d 100644 --- a/components/heap/multi_heap_poisoning.c +++ b/components/heap/multi_heap_poisoning.c @@ -175,6 +175,10 @@ static bool verify_fill_pattern(void *data, size_t size, bool print_errors, bool void *multi_heap_malloc(multi_heap_handle_t heap, size_t size) { + if (!size) { + return NULL; + } + if(size > SIZE_MAX - POISON_OVERHEAD) { return NULL; } diff --git a/components/heap/test/test_malloc.c b/components/heap/test/test_malloc.c index 3b329e386..7247ce757 100644 --- a/components/heap/test/test_malloc.c +++ b/components/heap/test/test_malloc.c @@ -113,3 +113,9 @@ TEST_CASE("unreasonable allocs should all fail", "[heap]") TEST_ASSERT_NULL(malloc(xPortGetFreeHeapSize() - 1)); } +TEST_CASE("malloc(0) should return a NULL pointer", "[heap]") +{ + void *p; + p = malloc(0); + TEST_ASSERT(p == NULL); +}