diff --git a/portable/MemMang/heap_2.c b/portable/MemMang/heap_2.c index 1240a6b53..1eed436ce 100644 --- a/portable/MemMang/heap_2.c +++ b/portable/MemMang/heap_2.c @@ -138,7 +138,7 @@ void * pvPortMalloc( size_t xWantedSize ) { /* Ensure xWantedSize will never wrap after adjustment, even if we need * an alignment adjustment */ - if ( xWantedSize < ( SIZE_MAX - heapSTRUCT_SIZE - portBYTE_ALIGNMENT - 1) ) + if( ( xWantedSize > 0 ) && ( ( xWantedSize + xHeapStructSize ) > xWantedSize ) ) { xWantedSize += heapSTRUCT_SIZE; @@ -154,7 +154,7 @@ void * pvPortMalloc( size_t xWantedSize ) } } - if( xWantedSize > 0 ) + if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) ) { /* Blocks are stored in byte order - traverse the list from the start * (smallest) block until one of adequate size is found. */ diff --git a/portable/MemMang/heap_4.c b/portable/MemMang/heap_4.c index 8bdedf767..a86052c20 100644 --- a/portable/MemMang/heap_4.c +++ b/portable/MemMang/heap_4.c @@ -176,7 +176,7 @@ void * pvPortMalloc( size_t xWantedSize ) mtCOVERAGE_TEST_MARKER(); } - if( ( xWantedSize > 0 ) && ( xWantedSize < xFreeBytesRemaining ) ) + if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) ) { /* Traverse the list from the start (lowest address) block until * one of adequate size is found. */ diff --git a/portable/MemMang/heap_5.c b/portable/MemMang/heap_5.c index 4953237f0..c4d96cb44 100644 --- a/portable/MemMang/heap_5.c +++ b/portable/MemMang/heap_5.c @@ -179,7 +179,7 @@ void * pvPortMalloc( size_t xWantedSize ) xWantedSize = 0; } - if( ( xWantedSize > 0 ) && ( xWantedSize < xFreeBytesRemaining ) ) + if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) ) { /* Traverse the list from the start (lowest address) block until * one of adequate size is found. */