From 9368a80760f52698faa94e62b5dc4b59d9d8cbb7 Mon Sep 17 00:00:00 2001 From: embeddedt <42941056+embeddedt@users.noreply.github.com> Date: Sat, 14 May 2022 16:04:13 -0400 Subject: [PATCH] fix(mem): fix TLSF returning the wrong pointer when the requested size is too large (#3325) * test(mem) add test for #3324 * fix(tlsf) don't return the same pointer if the requested size is too large --- src/misc/lv_tlsf.c | 4 ++++ tests/src/test_cases/test_mem.c | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 tests/src/test_cases/test_mem.c diff --git a/src/misc/lv_tlsf.c b/src/misc/lv_tlsf.c index 069318aadead..27e0a46cfe0b 100644 --- a/src/misc/lv_tlsf.c +++ b/src/misc/lv_tlsf.c @@ -1208,6 +1208,10 @@ void * lv_tlsf_realloc(lv_tlsf_t tlsf, void * ptr, size_t size) const size_t cursize = block_size(block); const size_t combined = cursize + block_size(next) + block_header_overhead; const size_t adjust = adjust_request_size(size, ALIGN_SIZE); + if(size > cursize && adjust == 0) { + /* The request is probably too large, fail */ + return NULL; + } tlsf_assert(!block_is_free(block) && "block already marked as free"); diff --git a/tests/src/test_cases/test_mem.c b/tests/src/test_cases/test_mem.c new file mode 100644 index 000000000000..c97e2719fd20 --- /dev/null +++ b/tests/src/test_cases/test_mem.c @@ -0,0 +1,26 @@ +#if LV_BUILD_TEST +#include "../lvgl.h" + +#include "unity/unity.h" + +void setUp(void) +{ + /* Function run before every test */ +} + +void tearDown(void) +{ + /* Function run after every test */ +} + +/* #3324 */ +void test_mem_buf_realloc(void) +{ +#if LV_MEM_CUSTOM == 0 + void * buf1 = lv_mem_alloc(20); + void * buf2 = lv_mem_realloc(buf1, LV_MEM_SIZE + 16384); + TEST_ASSERT_NULL(buf2); +#endif +} + +#endif