diff --git a/components/heap/multi_heap_poisoning.c b/components/heap/multi_heap_poisoning.c index 119017d00..ec620c079 100644 --- a/components/heap/multi_heap_poisoning.c +++ b/components/heap/multi_heap_poisoning.c @@ -187,6 +187,10 @@ static bool verify_fill_pattern(void *data, size_t size, bool print_errors, bool void *multi_heap_aligned_alloc(multi_heap_handle_t heap, size_t size, size_t alignment) { + if (!size) { + return NULL; + } + if (size > SIZE_MAX - POISON_OVERHEAD) { return NULL; } @@ -213,9 +217,14 @@ void *multi_heap_aligned_alloc(multi_heap_handle_t heap, size_t size, size_t ali void *multi_heap_malloc(multi_heap_handle_t heap, size_t size) { + if (!size) { + return NULL; + } + if(size > SIZE_MAX - POISON_OVERHEAD) { return NULL; } + multi_heap_internal_lock(heap); poison_head_t *head = multi_heap_malloc_impl(heap, size + POISON_OVERHEAD); uint8_t *data = NULL; diff --git a/components/heap/test/test_aligned_alloc_caps.c b/components/heap/test/test_aligned_alloc_caps.c index 845d7cf44..67226b29a 100644 --- a/components/heap/test/test_aligned_alloc_caps.c +++ b/components/heap/test/test_aligned_alloc_caps.c @@ -159,4 +159,11 @@ TEST_CASE("Capabilities aligned calloc test", "[heap]") } #endif -} \ No newline at end of file +} + +TEST_CASE("aligned_alloc(0) should return a NULL pointer", "[heap]") +{ + void *p; + p = heap_caps_aligned_alloc(32, 0, MALLOC_CAP_DEFAULT); + TEST_ASSERT(p == NULL); +} diff --git a/components/heap/test/test_malloc.c b/components/heap/test/test_malloc.c index cfc634bad..11bb3d9c9 100644 --- a/components/heap/test/test_malloc.c +++ b/components/heap/test/test_malloc.c @@ -127,3 +127,10 @@ 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); +} +