From f0e2286362eddcc2db773a96633b7eaf293341a1 Mon Sep 17 00:00:00 2001 From: Jacob Whitaker Abrams Date: Sat, 29 Nov 2025 17:19:47 -0800 Subject: [PATCH] Catch calls to initialize an oversize heap --- src/umm_malloc.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/umm_malloc.c b/src/umm_malloc.c index 3dd913b..91b225d 100644 --- a/src/umm_malloc.c +++ b/src/umm_malloc.c @@ -278,6 +278,21 @@ static uint16_t umm_assimilate_down(umm_heap *heap, uint16_t c, uint16_t freemas /* ------------------------------------------------------------------------- */ void umm_multi_init_heap(umm_heap *heap, void *ptr, size_t size) { + /* Guard against too many blocks for 15-bit indices */ + if ((size / UMM_BLOCKSIZE) > UMM_BLOCKNO_MASK) + { + /* Try increasing UMM_BLOCKSIZE if this hits */ + DBGLOG_CRITICAL("Heap too large: %u blocks (max %u)\n", + (unsigned) UMM_NUMBLOCKS, (unsigned) UMM_BLOCKNO_MASK); + + /* Mark this heap as unusable */ + heap->pheap = NULL; + UMM_HEAPSIZE = 0; + UMM_NUMBLOCKS = 0; + + return; + } + /* init heap pointer and size, and memset it to 0 */ heap->pheap = ptr; UMM_HEAPSIZE = size; @@ -799,4 +814,4 @@ void *umm_realloc(void *ptr, size_t size){ void umm_free(void *ptr){ umm_multi_free(&umm_heap_current, ptr); -} \ No newline at end of file +}