Skip to content

Commit cf7c846

Browse files
committed
Eliminate integer overflow in block_find_suitable
This commit fixes undefined behavior when (*fl + 1 >= 32) in bitmap operations.
1 parent 069112d commit cf7c846

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

tlsf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ INLINE tlsf_block_t *block_find_suitable(tlsf_t *t, uint32_t *fl, uint32_t *sl)
238238
uint32_t sl_map = t->sl[*fl] & (~0U << *sl);
239239
if (!sl_map) {
240240
/* No block exists. Search in the next largest first-level list. */
241-
uint32_t fl_map = t->fl & (uint32_t) (~(uint64_t) 0 << (*fl + 1));
241+
uint32_t fl_map = t->fl & ((*fl + 1 >= 32) ? 0U : (~0U << (*fl + 1)));
242242

243243
/* No free blocks available, memory has been exhausted. */
244244
if (UNLIKELY(!fl_map))
@@ -475,7 +475,7 @@ INLINE tlsf_block_t *block_find_free(tlsf_t *t, size_t *size)
475475
block = block_find_suitable(t, &fl, &sl);
476476
ASSERT(block, "no block found");
477477
}
478-
ASSERT(block_size(block) >= size, "insufficient block size");
478+
ASSERT(block_size(block) >= *size, "insufficient block size");
479479
remove_free_block(t, block, fl, sl);
480480
return block;
481481
}

tlsf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extern "C" {
2121
#define _TLSF_FL_MAX 30
2222
#endif
2323
#define TLSF_MAX_SIZE (((size_t) 1 << (_TLSF_FL_MAX - 1)) - sizeof(size_t))
24-
#define TLSF_INIT ((tlsf_t){.size = 0})
24+
#define TLSF_INIT ((tlsf_t) {.size = 0})
2525

2626
typedef struct {
2727
uint32_t fl, sl[_TLSF_FL_COUNT];

0 commit comments

Comments
 (0)