@@ -136,34 +136,42 @@ void * pvPortMalloc( size_t xWantedSize )
136136 * kernel, so it must be free. */
137137 if ( ( xWantedSize & xBlockAllocatedBit ) == 0 )
138138 {
139- /* The wanted size is increased so it can contain a BlockLink_t
139+ /* The wanted size must be increased so it can contain a BlockLink_t
140140 * structure in addition to the requested amount of bytes. */
141- if ( xWantedSize > 0 )
141+ if ( ( xWantedSize > 0 ) &&
142+ ( ( xWantedSize + xHeapStructSize ) > xWantedSize ) ) /* Overflow check */
142143 {
143144 xWantedSize += xHeapStructSize ;
144145
145- /* Ensure that blocks are always aligned to the required number
146- * of bytes. */
146+ /* Ensure that blocks are always aligned. */
147147 if ( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )
148148 {
149- /* Byte alignment required. */
150- xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
151- configASSERT ( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) == 0 );
149+ /* Byte alignment required. Check for overflow. */
150+ if ( ( xWantedSize + ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) ) )
151+ > xWantedSize )
152+ {
153+ xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
154+ configASSERT ( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) == 0 );
155+ }
156+ else
157+ {
158+ xWantedSize = 0 ;
159+ }
152160 }
153161 else
154162 {
155163 mtCOVERAGE_TEST_MARKER ();
156164 }
157- }
158- else
165+ }
166+ else
159167 {
160- mtCOVERAGE_TEST_MARKER () ;
168+ xWantedSize = 0 ;
161169 }
162170
163171 if ( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )
164172 {
165173 /* Traverse the list from the start (lowest address) block until
166- * one of adequate size is found. */
174+ * one of adequate size is found. */
167175 pxPreviousBlock = & xStart ;
168176 pxBlock = xStart .pxNextFreeBlock ;
169177
@@ -174,7 +182,7 @@ void * pvPortMalloc( size_t xWantedSize )
174182 }
175183
176184 /* If the end marker was reached then a block of adequate size
177- * was not found. */
185+ * was not found. */
178186 if ( pxBlock != pxEnd )
179187 {
180188 /* Return the memory space pointed to - jumping over the
0 commit comments