From 9d5d22c458e7454559166eacfad6d1f919d1d834 Mon Sep 17 00:00:00 2001 From: Felipe Neves Date: Fri, 27 Mar 2020 14:30:27 -0300 Subject: [PATCH] multi_heap: ensure that malloc(0) return NULL pointer in any poisoning configuration --- components/heap/multi_heap_poisoning.c | 4 ++++ components/heap/test/test_malloc.c | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/components/heap/multi_heap_poisoning.c b/components/heap/multi_heap_poisoning.c index a896043ab..ae4cfd44d 100644 --- a/components/heap/multi_heap_poisoning.c +++ b/components/heap/multi_heap_poisoning.c @@ -184,6 +184,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 e2916e2bc..56006173b 100644 --- a/components/heap/test/test_malloc.c +++ b/components/heap/test/test_malloc.c @@ -128,3 +128,9 @@ TEST_CASE("unreasonable allocs should all fail", "[heap]") TEST_ASSERT_NULL(test_malloc_wrapper(xPortGetFreeHeapSize() - 1)); } +TEST_CASE("malloc(0) should return a NULL pointer", "[heap]") +{ + void *p; + p = malloc(0); + TEST_ASSERT(p == NULL); +} \ No newline at end of file