From 48e78e6e07e7541dd80f51aa4da7df6741da5780 Mon Sep 17 00:00:00 2001 From: Felipe Neves Date: Fri, 27 Mar 2020 14:35:50 -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 703270c9a..e147d9f29 100644 --- a/components/heap/test/test_malloc.c +++ b/components/heap/test/test_malloc.c @@ -132,3 +132,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