From 0c8ce8f470da02282b2c39759b07e5c5b0651cea Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Mon, 11 Mar 2024 12:25:38 -0700 Subject: [PATCH] Fix assertion error due to zero length allocation The original code missed a * to check the value of size for non-zero. This corrects that error and adds an additional check after the first alignment. --- tlsf.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tlsf.c b/tlsf.c index 7c363ce..b548e23 100644 --- a/tlsf.c +++ b/tlsf.c @@ -284,8 +284,8 @@ static inline __attribute__((always_inline)) void mapping_search(control_t* cont { if (*size >= control->small_block_size) { - const size_t round = (1 << (tlsf_fls_sizet(*size) - control->sl_index_count_log2)) - 1; - *size = (*size + round) & ~round; + const size_t round = (1 << (tlsf_fls_sizet(*size) - control->sl_index_count_log2)); + *size = align_up(*size, round); } mapping_insert(control, *size, fli, sli); } @@ -545,7 +545,7 @@ static inline __attribute__((always_inline)) block_header_t* block_locate_free(c int fl = 0, sl = 0; block_header_t* block = 0; - if (size) + if (*size) { mapping_search(control, size, &fl, &sl); @@ -1001,6 +1001,11 @@ void* tlsf_malloc(tlsf_t tlsf, size_t size) { control_t* control = tlsf_cast(control_t*, tlsf); size_t adjust = adjust_request_size(tlsf, size, ALIGN_SIZE); + // Returned size is 0 when the requested size is larger than the max block + // size. + if (adjust == 0) { + return NULL; + } // block_locate_free() may adjust our allocated size further. block_header_t* block = block_locate_free(control, &adjust); return block_prepare_used(control, block, adjust);