Skip to content

Commit fc70295

Browse files
committed
Merge branch 'bugfix/queue_arith_overflow_v3.4' into 'release/v3.4'
freertos: Add queue init overflow check (backport v3.4) See merge request sdk/ESP8266_RTOS_SDK!1613
2 parents 2abe921 + 91163a8 commit fc70295

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

components/freertos/freertos/queue.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ Queue_t * const pxQueue = ( Queue_t * ) xQueue;
360360
Queue_t *pxNewQueue;
361361
size_t xQueueSizeInBytes;
362362
uint8_t *pucQueueStorage;
363+
BaseType_t overflow;
363364

364365
configASSERT( uxQueueLength > ( UBaseType_t ) 0 );
365366

@@ -375,7 +376,29 @@ Queue_t * const pxQueue = ( Queue_t * ) xQueue;
375376
xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
376377
}
377378

378-
pxNewQueue = ( Queue_t * ) pvPortMalloc( sizeof( Queue_t ) + xQueueSizeInBytes );
379+
/* Check for multiplication overflow. */
380+
overflow = ( uxItemSize != 0 ) && ( uxQueueLength != ( xQueueSizeInBytes / uxItemSize ) );
381+
382+
/* Check for addition overflow. */
383+
overflow = overflow || ( ( sizeof( Queue_t ) + xQueueSizeInBytes ) < xQueueSizeInBytes );
384+
385+
if ( overflow == (BaseType_t) 0 )
386+
{
387+
/* Allocate the queue and storage area. Justification for MISRA
388+
deviation as follows: pvPortMalloc() always ensures returned memory
389+
blocks are aligned per the requirements of the MCU stack. In this case
390+
pvPortMalloc() must return a pointer that is guaranteed to meet the
391+
alignment requirements of the Queue_t structure - which in this case
392+
is an int8_t *. Therefore, whenever the stack alignment requirements
393+
are greater than or equal to the pointer to char requirements the cast
394+
is safe. In other cases alignment requirements are not strict (one or
395+
two bytes). */
396+
pxNewQueue = ( Queue_t * ) pvPortMalloc( sizeof( Queue_t ) + xQueueSizeInBytes ); /*lint !e9087 !e9079 see comment above. */
397+
}
398+
else
399+
{
400+
pxNewQueue = NULL;
401+
}
379402

380403
if( pxNewQueue != NULL )
381404
{

components/freertos/freertos/stream_buffer.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,15 @@ static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,
242242
this is a quirk of the implementation that means otherwise the free
243243
space would be reported as one byte smaller than would be logically
244244
expected. */
245-
xBufferSizeBytes++;
246-
pucAllocatedMemory = ( uint8_t * ) pvPortMalloc( xBufferSizeBytes + sizeof( StreamBuffer_t ) ); /*lint !e9079 malloc() only returns void*. */
245+
if( xBufferSizeBytes < ( xBufferSizeBytes + 1 + sizeof( StreamBuffer_t ) ) )
246+
{
247+
xBufferSizeBytes++;
248+
pucAllocatedMemory = ( uint8_t * ) pvPortMalloc( xBufferSizeBytes + sizeof( StreamBuffer_t ) ); /*lint !e9079 malloc() only returns void*. */
249+
}
250+
else
251+
{
252+
pucAllocatedMemory = NULL;
253+
}
247254

248255
if( pucAllocatedMemory != NULL )
249256
{

0 commit comments

Comments
 (0)