From a7bda37fce6d6a1065ba2cc42da82847966f8397 Mon Sep 17 00:00:00 2001 From: wangfei_chen Date: Tue, 20 Aug 2024 11:26:33 +0800 Subject: [PATCH] Add heap protector to allocted heap blocks When validate those allocated heap block structure, the canary is not used. Do xor with canary when allocating a new block. Signed-off-by: wangfei_chen --- portable/MemMang/heap_4.c | 6 +++--- portable/MemMang/heap_5.c | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/portable/MemMang/heap_4.c b/portable/MemMang/heap_4.c index d1021ea09a..f3cab000d0 100644 --- a/portable/MemMang/heap_4.c +++ b/portable/MemMang/heap_4.c @@ -308,7 +308,7 @@ void * pvPortMalloc( size_t xWantedSize ) /* The block is being returned - it is allocated and owned * by the application and has no "next" block. */ heapALLOCATE_BLOCK( pxBlock ); - pxBlock->pxNextFreeBlock = NULL; + pxBlock->pxNextFreeBlock = heapPROTECT_BLOCK_POINTER( NULL ); xNumberOfSuccessfulAllocations++; } else @@ -367,11 +367,11 @@ void vPortFree( void * pv ) heapVALIDATE_BLOCK_POINTER( pxLink ); configASSERT( heapBLOCK_IS_ALLOCATED( pxLink ) != 0 ); - configASSERT( pxLink->pxNextFreeBlock == NULL ); + configASSERT( pxLink->pxNextFreeBlock == heapPROTECT_BLOCK_POINTER( NULL ) ); if( heapBLOCK_IS_ALLOCATED( pxLink ) != 0 ) { - if( pxLink->pxNextFreeBlock == NULL ) + if( pxLink->pxNextFreeBlock == heapPROTECT_BLOCK_POINTER( NULL ) ) { /* The block is being returned to the heap - it is no longer * allocated. */ diff --git a/portable/MemMang/heap_5.c b/portable/MemMang/heap_5.c index e0c5666409..fccf798256 100644 --- a/portable/MemMang/heap_5.c +++ b/portable/MemMang/heap_5.c @@ -336,7 +336,7 @@ void * pvPortMalloc( size_t xWantedSize ) /* The block is being returned - it is allocated and owned * by the application and has no "next" block. */ heapALLOCATE_BLOCK( pxBlock ); - pxBlock->pxNextFreeBlock = NULL; + pxBlock->pxNextFreeBlock = heapPROTECT_BLOCK_POINTER( NULL ); xNumberOfSuccessfulAllocations++; } else @@ -395,11 +395,11 @@ void vPortFree( void * pv ) heapVALIDATE_BLOCK_POINTER( pxLink ); configASSERT( heapBLOCK_IS_ALLOCATED( pxLink ) != 0 ); - configASSERT( pxLink->pxNextFreeBlock == NULL ); + configASSERT( pxLink->pxNextFreeBlock == heapPROTECT_BLOCK_POINTER( NULL ) ); if( heapBLOCK_IS_ALLOCATED( pxLink ) != 0 ) { - if( pxLink->pxNextFreeBlock == NULL ) + if( pxLink->pxNextFreeBlock == heapPROTECT_BLOCK_POINTER( NULL ) ) { /* The block is being returned to the heap - it is no longer * allocated. */