Skip to content

Commit

Permalink
fix(mem): fix TLSF returning the wrong pointer when the requested siz…
Browse files Browse the repository at this point in the history
…e is too large (lvgl#3325)

* test(mem) add test for lvgl#3324

* fix(tlsf) don't return the same pointer if the requested size is too large
  • Loading branch information
embeddedt authored and somebodyLi committed Nov 8, 2022
1 parent d171f69 commit 9368a80
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/misc/lv_tlsf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
26 changes: 26 additions & 0 deletions tests/src/test_cases/test_mem.c
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9368a80

Please sign in to comment.