Skip to content

Commit f31787d

Browse files
fix(freertos): Limit idle task name length copy operation and ensure null-termination of the idle task name string (#1203)
* fix(freertos): Limit idle task name copy operation and ensure null termination This commit: - Limits the idle task name length copy operation to prevent Out-of-bounds memory access warnings from static code analyzers. - Fixes a bug where in the idle task name could be non null-terminated string for SMP configuration. Signed-off-by: Sudeep Mohanty <[email protected]>
1 parent cc31510 commit f31787d

File tree

1 file changed

+18
-33
lines changed

1 file changed

+18
-33
lines changed

tasks.c

+18-33
Original file line numberDiff line numberDiff line change
@@ -3524,27 +3524,28 @@ static BaseType_t prvCreateIdleTasks( void )
35243524
{
35253525
BaseType_t xReturn = pdPASS;
35263526
BaseType_t xCoreID;
3527-
char cIdleName[ configMAX_TASK_NAME_LEN ];
3527+
char cIdleName[ configMAX_TASK_NAME_LEN ] = { 0 };
35283528
TaskFunction_t pxIdleTaskFunction = NULL;
35293529
BaseType_t xIdleTaskNameIndex;
3530+
BaseType_t xIdleNameLen;
3531+
BaseType_t xCopyLen;
35303532

3531-
for( xIdleTaskNameIndex = ( BaseType_t ) 0; xIdleTaskNameIndex < ( BaseType_t ) configMAX_TASK_NAME_LEN; xIdleTaskNameIndex++ )
3533+
configASSERT( ( configIDLE_TASK_NAME != NULL ) && ( configMAX_TASK_NAME_LEN > 3 ) );
3534+
3535+
/* The length of the idle task name is limited to the minimum of the length
3536+
* of configIDLE_TASK_NAME and configMAX_TASK_NAME_LEN - 2, keeping space
3537+
* for the core ID suffix and the null-terminator. */
3538+
xIdleNameLen = strlen( configIDLE_TASK_NAME );
3539+
xCopyLen = xIdleNameLen < ( configMAX_TASK_NAME_LEN - 2 ) ? xIdleNameLen : ( configMAX_TASK_NAME_LEN - 2 );
3540+
3541+
for( xIdleTaskNameIndex = ( BaseType_t ) 0; xIdleTaskNameIndex < xCopyLen; xIdleTaskNameIndex++ )
35323542
{
35333543
cIdleName[ xIdleTaskNameIndex ] = configIDLE_TASK_NAME[ xIdleTaskNameIndex ];
3534-
3535-
/* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
3536-
* configMAX_TASK_NAME_LEN characters just in case the memory after the
3537-
* string is not accessible (extremely unlikely). */
3538-
if( cIdleName[ xIdleTaskNameIndex ] == ( char ) 0x00 )
3539-
{
3540-
break;
3541-
}
3542-
else
3543-
{
3544-
mtCOVERAGE_TEST_MARKER();
3545-
}
35463544
}
35473545

3546+
/* Ensure null termination. */
3547+
cIdleName[ xIdleTaskNameIndex ] = '\0';
3548+
35483549
/* Add each idle task at the lowest priority. */
35493550
for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
35503551
{
@@ -3573,25 +3574,9 @@ static BaseType_t prvCreateIdleTasks( void )
35733574
* only one idle task. */
35743575
#if ( configNUMBER_OF_CORES > 1 )
35753576
{
3576-
/* Append the idle task number to the end of the name if there is space. */
3577-
if( xIdleTaskNameIndex < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3578-
{
3579-
cIdleName[ xIdleTaskNameIndex ] = ( char ) ( xCoreID + '0' );
3580-
3581-
/* And append a null character if there is space. */
3582-
if( ( xIdleTaskNameIndex + 1 ) < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3583-
{
3584-
cIdleName[ xIdleTaskNameIndex + 1 ] = '\0';
3585-
}
3586-
else
3587-
{
3588-
mtCOVERAGE_TEST_MARKER();
3589-
}
3590-
}
3591-
else
3592-
{
3593-
mtCOVERAGE_TEST_MARKER();
3594-
}
3577+
/* Append the idle task number to the end of the name. */
3578+
cIdleName[ xIdleTaskNameIndex ] = ( char ) ( xCoreID + '0' );
3579+
cIdleName[ xIdleTaskNameIndex + 1 ] = '\0';
35953580
}
35963581
#endif /* if ( configNUMBER_OF_CORES > 1 ) */
35973582

0 commit comments

Comments
 (0)