From d5d752a9b6a79c2b5cf2f7953c4bf80e3f35433b Mon Sep 17 00:00:00 2001 From: Archit Gupta Date: Fri, 12 Jun 2026 18:55:13 +0000 Subject: [PATCH] Fix spurious heap_5 assert when allocation search reaches end marker (#1425) When configENABLE_HEAP_PROTECTOR is 1 and a pvPortMalloc() request cannot be satisfied by any free block, the free list search advances onto the end marker (pxEnd) and validates it with heapVALIDATE_BLOCK_POINTER(). pxEnd is located at pucHeapHighAddress and is not part of the usable heap, so the macro's "( pxBlock ) < pucHeapHighAddress" bounds check fails and the configASSERT() fires on what is a normal out-of-memory condition. The code should instead fall through and call the malloc failed hook. Exclude the end marker from validation in the search loop so an allocation that cannot be satisfied returns NULL (and invokes vApplicationMallocFailedHook when configUSE_MALLOC_FAILED_HOOK is 1) without asserting. --- portable/MemMang/heap_5.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/portable/MemMang/heap_5.c b/portable/MemMang/heap_5.c index bf321304f..a54381b0c 100644 --- a/portable/MemMang/heap_5.c +++ b/portable/MemMang/heap_5.c @@ -284,7 +284,14 @@ void * pvPortMalloc( size_t xWantedSize ) { pxPreviousBlock = pxBlock; pxBlock = heapPROTECT_BLOCK_POINTER( pxBlock->pxNextFreeBlock ); - heapVALIDATE_BLOCK_POINTER( pxBlock ); + + /* pxEnd is the end marker of the free list. It is located at + * pucHeapHighAddress and is not part of the usable heap, so it + * must be excluded from the heap block pointer validation. */ + if( pxBlock != pxEnd ) + { + heapVALIDATE_BLOCK_POINTER( pxBlock ); + } } /* If the end marker was reached then a block of adequate size