From ed546797d0892e8322c526e52b75e430fbb35398 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Wed, 29 Aug 2018 12:52:03 +0800 Subject: [PATCH] =?UTF-8?q?heap:=20test:=20don=E2=80=99t=20warn=20about=20?= =?UTF-8?q?oversized=20mallocs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/heap/test/test_malloc.c | 35 +++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/components/heap/test/test_malloc.c b/components/heap/test/test_malloc.c index 1e13ce3603..52dc5d171a 100644 --- a/components/heap/test/test_malloc.c +++ b/components/heap/test/test_malloc.c @@ -88,23 +88,42 @@ TEST_CASE("Check if reserved DMA pool still can allocate even when malloc()'ed m #endif + +/* As you see, we are desperately trying to outsmart the compiler, so that it + * doesn't warn about oversized allocations in the next two unit tests. + * To be removed when we switch to GCC 8.2 and add + * -Wno-alloc-size-larger-than=PTRDIFF_MAX to CFLAGS for this file. + */ +void* (*g_test_malloc_ptr)(size_t) = &malloc; +void* (*g_test_calloc_ptr)(size_t, size_t) = &calloc; + +void* test_malloc_wrapper(size_t size) +{ + return (*g_test_malloc_ptr)(size); +} + +void* test_calloc_wrapper(size_t count, size_t size) +{ + return (*g_test_calloc_ptr)(count, size); +} + TEST_CASE("alloc overflows should all fail", "[heap]") { /* allocates 8 bytes */ - TEST_ASSERT_NULL(calloc(SIZE_MAX / 2 + 4, 2)); + TEST_ASSERT_NULL(test_calloc_wrapper(SIZE_MAX / 2 + 4, 2)); /* will overflow if any poisoning is enabled (should fail for sensible OOM reasons, otherwise) */ - TEST_ASSERT_NULL(malloc(SIZE_MAX - 1)); - TEST_ASSERT_NULL(calloc(SIZE_MAX - 1, 1)); + TEST_ASSERT_NULL(test_malloc_wrapper(SIZE_MAX - 1)); + TEST_ASSERT_NULL(test_calloc_wrapper(SIZE_MAX - 1, 1)); } TEST_CASE("unreasonable allocs should all fail", "[heap]") { - TEST_ASSERT_NULL(calloc(16, 1024*1024)); - TEST_ASSERT_NULL(malloc(16*1024*1024)); - TEST_ASSERT_NULL(malloc(SIZE_MAX / 2)); - TEST_ASSERT_NULL(malloc(SIZE_MAX - 256)); - TEST_ASSERT_NULL(malloc(xPortGetFreeHeapSize() - 1)); + TEST_ASSERT_NULL(test_calloc_wrapper(16, 1024*1024)); + TEST_ASSERT_NULL(test_malloc_wrapper(16*1024*1024)); + TEST_ASSERT_NULL(test_malloc_wrapper(SIZE_MAX / 2)); + TEST_ASSERT_NULL(test_malloc_wrapper(SIZE_MAX - 256)); + TEST_ASSERT_NULL(test_malloc_wrapper(xPortGetFreeHeapSize() - 1)); }