Skip to content

Commit 3fd7f17

Browse files
Fix MISRA violations for Kernel release V11.2.0 (#1251)
* Fix MISRA violations for Kernel release V11.2.0 * Fix formatting * Remove redundant configASSERT in timers.c
1 parent df0aa5a commit 3fd7f17

File tree

5 files changed

+115
-105
lines changed

5 files changed

+115
-105
lines changed

MISRA.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ _Ref 11.5.5_
120120
MISRA C-2012 Rule 14.3: Controlling expressions shall not be invariant.
121121

122122
_Ref 14.3_
123-
- The `configMAX_TASK_NAME_LEN` and `taskRESERVED_TASK_NAME_LENGTH` are
124-
evaluated to constants at compile time and may vary based on the build
123+
- The `configMAX_TASK_NAME_LEN` , `taskRESERVED_TASK_NAME_LENGTH` and `SIZE_MAX`
124+
are evaluated to constants at compile time and may vary based on the build
125125
configuration.
126126

127127
#### Rule 18.1

examples/coverity/coverity_misra.config

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
"standard" : "c2012",
44
"title": "Coverity MISRA Configuration",
55
"deviations" : [
6+
{
7+
"deviation": "Rule 1.2",
8+
"reason": "Allow use of __attribute__ for necessary functions placement in specific memory regions."
9+
},
610
{
711
"deviation": "Rule 3.1",
812
"reason": "We post HTTP links in code comments which contain // inside comments blocks."

queue.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,10 @@ BaseType_t xQueueGenericReset( QueueHandle_t xQueue,
513513
/* Check for multiplication overflow. */
514514
( ( SIZE_MAX / uxQueueLength ) >= uxItemSize ) &&
515515
/* Check for addition overflow. */
516-
( ( SIZE_MAX - sizeof( Queue_t ) ) >= ( size_t ) ( uxQueueLength * uxItemSize ) ) )
516+
/* MISRA Ref 14.3.1 [Configuration dependent invariant] */
517+
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-143. */
518+
/* coverity[misra_c_2012_rule_14_3_violation] */
519+
( ( SIZE_MAX - sizeof( Queue_t ) ) >= ( size_t ) ( ( size_t ) uxQueueLength * ( size_t ) uxItemSize ) ) )
517520
{
518521
/* Allocate enough space to hold the maximum number of items that
519522
* can be in the queue at any time. It is valid for uxItemSize to be

tasks.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -2016,7 +2016,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
20162016
pxNewTCB->xTaskRunState = taskTASK_NOT_RUNNING;
20172017

20182018
/* Is this an idle task? */
2019-
if( ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) prvIdleTask ) || ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) prvPassiveIdleTask ) )
2019+
if( ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) ( &prvIdleTask ) ) || ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) ( &prvPassiveIdleTask ) ) )
20202020
{
20212021
pxNewTCB->uxTaskAttributes |= taskATTRIBUTE_IS_IDLE;
20222022
}
@@ -3573,7 +3573,7 @@ static BaseType_t prvCreateIdleTasks( void )
35733573
{
35743574
#if ( configNUMBER_OF_CORES == 1 )
35753575
{
3576-
pxIdleTaskFunction = prvIdleTask;
3576+
pxIdleTaskFunction = &prvIdleTask;
35773577
}
35783578
#else /* #if ( configNUMBER_OF_CORES == 1 ) */
35793579
{
@@ -3582,11 +3582,11 @@ static BaseType_t prvCreateIdleTasks( void )
35823582
* run when no other task is available to run. */
35833583
if( xCoreID == 0 )
35843584
{
3585-
pxIdleTaskFunction = prvIdleTask;
3585+
pxIdleTaskFunction = &prvIdleTask;
35863586
}
35873587
else
35883588
{
3589-
pxIdleTaskFunction = prvPassiveIdleTask;
3589+
pxIdleTaskFunction = &prvPassiveIdleTask;
35903590
}
35913591
}
35923592
#endif /* #if ( configNUMBER_OF_CORES == 1 ) */
@@ -3603,7 +3603,7 @@ static BaseType_t prvCreateIdleTasks( void )
36033603
* name will contain an incorrect ASCII character. This is
36043604
* acceptable as the task name is used mainly for debugging. */
36053605
cIdleName[ xIdleTaskNameIndex ] = ( char ) ( xCoreID + '0' );
3606-
cIdleName[ xIdleTaskNameIndex + 1 ] = '\0';
3606+
cIdleName[ xIdleTaskNameIndex + 1U ] = '\0';
36073607
}
36083608
#endif /* if ( configNUMBER_OF_CORES > 1 ) */
36093609

timers.c

+100-97
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@
257257
configSTACK_DEPTH_TYPE uxTimerTaskStackSize;
258258

259259
vApplicationGetTimerTaskMemory( &pxTimerTaskTCBBuffer, &pxTimerTaskStackBuffer, &uxTimerTaskStackSize );
260-
xTimerTaskHandle = xTaskCreateStaticAffinitySet( prvTimerTask,
260+
xTimerTaskHandle = xTaskCreateStaticAffinitySet( &prvTimerTask,
261261
configTIMER_SERVICE_TASK_NAME,
262262
uxTimerTaskStackSize,
263263
NULL,
@@ -273,7 +273,7 @@
273273
}
274274
#else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
275275
{
276-
xReturn = xTaskCreateAffinitySet( prvTimerTask,
276+
xReturn = xTaskCreateAffinitySet( &prvTimerTask,
277277
configTIMER_SERVICE_TASK_NAME,
278278
configTIMER_TASK_STACK_DEPTH,
279279
NULL,
@@ -292,7 +292,7 @@
292292
configSTACK_DEPTH_TYPE uxTimerTaskStackSize;
293293

294294
vApplicationGetTimerTaskMemory( &pxTimerTaskTCBBuffer, &pxTimerTaskStackBuffer, &uxTimerTaskStackSize );
295-
xTimerTaskHandle = xTaskCreateStatic( prvTimerTask,
295+
xTimerTaskHandle = xTaskCreateStatic( &prvTimerTask,
296296
configTIMER_SERVICE_TASK_NAME,
297297
uxTimerTaskStackSize,
298298
NULL,
@@ -307,7 +307,7 @@
307307
}
308308
#else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
309309
{
310-
xReturn = xTaskCreate( prvTimerTask,
310+
xReturn = xTaskCreate( &prvTimerTask,
311311
configTIMER_SERVICE_TASK_NAME,
312312
configTIMER_TASK_STACK_DEPTH,
313313
NULL,
@@ -458,11 +458,9 @@
458458

459459
traceENTER_xTimerGenericCommandFromTask( xTimer, xCommandID, xOptionalValue, pxHigherPriorityTaskWoken, xTicksToWait );
460460

461-
configASSERT( xTimer );
462-
463461
/* Send a message to the timer service task to perform a particular action
464462
* on a particular timer definition. */
465-
if( xTimerQueue != NULL )
463+
if( ( xTimerQueue != NULL ) && ( xTimer != NULL ) )
466464
{
467465
/* Send a command to the timer service task to start the xTimer timer. */
468466
xMessage.xMessageID = xCommandID;
@@ -509,11 +507,9 @@
509507

510508
traceENTER_xTimerGenericCommandFromISR( xTimer, xCommandID, xOptionalValue, pxHigherPriorityTaskWoken, xTicksToWait );
511509

512-
configASSERT( xTimer );
513-
514510
/* Send a message to the timer service task to perform a particular action
515511
* on a particular timer definition. */
516-
if( xTimerQueue != NULL )
512+
if( ( xTimerQueue != NULL ) && ( xTimer != NULL ) )
517513
{
518514
/* Send a command to the timer service task to start the xTimer timer. */
519515
xMessage.xMessageID = xCommandID;
@@ -974,109 +970,116 @@
974970
* software timer. */
975971
pxTimer = xMessage.u.xTimerParameters.pxTimer;
976972

977-
if( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) == pdFALSE )
973+
if( pxTimer != NULL )
978974
{
979-
/* The timer is in a list, remove it. */
980-
( void ) uxListRemove( &( pxTimer->xTimerListItem ) );
981-
}
982-
else
983-
{
984-
mtCOVERAGE_TEST_MARKER();
985-
}
975+
if( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) == pdFALSE )
976+
{
977+
/* The timer is in a list, remove it. */
978+
( void ) uxListRemove( &( pxTimer->xTimerListItem ) );
979+
}
980+
else
981+
{
982+
mtCOVERAGE_TEST_MARKER();
983+
}
986984

987-
traceTIMER_COMMAND_RECEIVED( pxTimer, xMessage.xMessageID, xMessage.u.xTimerParameters.xMessageValue );
985+
traceTIMER_COMMAND_RECEIVED( pxTimer, xMessage.xMessageID, xMessage.u.xTimerParameters.xMessageValue );
988986

989-
/* In this case the xTimerListsWereSwitched parameter is not used, but
990-
* it must be present in the function call. prvSampleTimeNow() must be
991-
* called after the message is received from xTimerQueue so there is no
992-
* possibility of a higher priority task adding a message to the message
993-
* queue with a time that is ahead of the timer daemon task (because it
994-
* pre-empted the timer daemon task after the xTimeNow value was set). */
995-
xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );
987+
/* In this case the xTimerListsWereSwitched parameter is not used, but
988+
* it must be present in the function call. prvSampleTimeNow() must be
989+
* called after the message is received from xTimerQueue so there is no
990+
* possibility of a higher priority task adding a message to the message
991+
* queue with a time that is ahead of the timer daemon task (because it
992+
* pre-empted the timer daemon task after the xTimeNow value was set). */
993+
xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );
996994

997-
switch( xMessage.xMessageID )
998-
{
999-
case tmrCOMMAND_START:
1000-
case tmrCOMMAND_START_FROM_ISR:
1001-
case tmrCOMMAND_RESET:
1002-
case tmrCOMMAND_RESET_FROM_ISR:
1003-
/* Start or restart a timer. */
1004-
pxTimer->ucStatus |= ( uint8_t ) tmrSTATUS_IS_ACTIVE;
1005-
1006-
if( prvInsertTimerInActiveList( pxTimer, xMessage.u.xTimerParameters.xMessageValue + pxTimer->xTimerPeriodInTicks, xTimeNow, xMessage.u.xTimerParameters.xMessageValue ) != pdFALSE )
1007-
{
1008-
/* The timer expired before it was added to the active
1009-
* timer list. Process it now. */
1010-
if( ( pxTimer->ucStatus & tmrSTATUS_IS_AUTORELOAD ) != 0U )
995+
switch( xMessage.xMessageID )
996+
{
997+
case tmrCOMMAND_START:
998+
case tmrCOMMAND_START_FROM_ISR:
999+
case tmrCOMMAND_RESET:
1000+
case tmrCOMMAND_RESET_FROM_ISR:
1001+
/* Start or restart a timer. */
1002+
pxTimer->ucStatus |= ( uint8_t ) tmrSTATUS_IS_ACTIVE;
1003+
1004+
if( prvInsertTimerInActiveList( pxTimer, xMessage.u.xTimerParameters.xMessageValue + pxTimer->xTimerPeriodInTicks, xTimeNow, xMessage.u.xTimerParameters.xMessageValue ) != pdFALSE )
10111005
{
1012-
prvReloadTimer( pxTimer, xMessage.u.xTimerParameters.xMessageValue + pxTimer->xTimerPeriodInTicks, xTimeNow );
1006+
/* The timer expired before it was added to the active
1007+
* timer list. Process it now. */
1008+
if( ( pxTimer->ucStatus & tmrSTATUS_IS_AUTORELOAD ) != 0U )
1009+
{
1010+
prvReloadTimer( pxTimer, xMessage.u.xTimerParameters.xMessageValue + pxTimer->xTimerPeriodInTicks, xTimeNow );
1011+
}
1012+
else
1013+
{
1014+
pxTimer->ucStatus &= ( ( uint8_t ) ~tmrSTATUS_IS_ACTIVE );
1015+
}
1016+
1017+
/* Call the timer callback. */
1018+
traceTIMER_EXPIRED( pxTimer );
1019+
pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );
10131020
}
10141021
else
10151022
{
1016-
pxTimer->ucStatus &= ( ( uint8_t ) ~tmrSTATUS_IS_ACTIVE );
1023+
mtCOVERAGE_TEST_MARKER();
10171024
}
10181025

1019-
/* Call the timer callback. */
1020-
traceTIMER_EXPIRED( pxTimer );
1021-
pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );
1022-
}
1023-
else
1024-
{
1025-
mtCOVERAGE_TEST_MARKER();
1026-
}
1027-
1028-
break;
1029-
1030-
case tmrCOMMAND_STOP:
1031-
case tmrCOMMAND_STOP_FROM_ISR:
1032-
/* The timer has already been removed from the active list. */
1033-
pxTimer->ucStatus &= ( ( uint8_t ) ~tmrSTATUS_IS_ACTIVE );
1034-
break;
1035-
1036-
case tmrCOMMAND_CHANGE_PERIOD:
1037-
case tmrCOMMAND_CHANGE_PERIOD_FROM_ISR:
1038-
pxTimer->ucStatus |= ( uint8_t ) tmrSTATUS_IS_ACTIVE;
1039-
pxTimer->xTimerPeriodInTicks = xMessage.u.xTimerParameters.xMessageValue;
1040-
configASSERT( ( pxTimer->xTimerPeriodInTicks > 0 ) );
1041-
1042-
/* The new period does not really have a reference, and can
1043-
* be longer or shorter than the old one. The command time is
1044-
* therefore set to the current time, and as the period cannot
1045-
* be zero the next expiry time can only be in the future,
1046-
* meaning (unlike for the xTimerStart() case above) there is
1047-
* no fail case that needs to be handled here. */
1048-
( void ) prvInsertTimerInActiveList( pxTimer, ( xTimeNow + pxTimer->xTimerPeriodInTicks ), xTimeNow, xTimeNow );
1049-
break;
1050-
1051-
case tmrCOMMAND_DELETE:
1052-
#if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
1053-
{
1054-
/* The timer has already been removed from the active list,
1055-
* just free up the memory if the memory was dynamically
1056-
* allocated. */
1057-
if( ( pxTimer->ucStatus & tmrSTATUS_IS_STATICALLY_ALLOCATED ) == ( uint8_t ) 0 )
1026+
break;
1027+
1028+
case tmrCOMMAND_STOP:
1029+
case tmrCOMMAND_STOP_FROM_ISR:
1030+
/* The timer has already been removed from the active list. */
1031+
pxTimer->ucStatus &= ( ( uint8_t ) ~tmrSTATUS_IS_ACTIVE );
1032+
break;
1033+
1034+
case tmrCOMMAND_CHANGE_PERIOD:
1035+
case tmrCOMMAND_CHANGE_PERIOD_FROM_ISR:
1036+
pxTimer->ucStatus |= ( uint8_t ) tmrSTATUS_IS_ACTIVE;
1037+
pxTimer->xTimerPeriodInTicks = xMessage.u.xTimerParameters.xMessageValue;
1038+
configASSERT( ( pxTimer->xTimerPeriodInTicks > 0 ) );
1039+
1040+
/* The new period does not really have a reference, and can
1041+
* be longer or shorter than the old one. The command time is
1042+
* therefore set to the current time, and as the period cannot
1043+
* be zero the next expiry time can only be in the future,
1044+
* meaning (unlike for the xTimerStart() case above) there is
1045+
* no fail case that needs to be handled here. */
1046+
( void ) prvInsertTimerInActiveList( pxTimer, ( xTimeNow + pxTimer->xTimerPeriodInTicks ), xTimeNow, xTimeNow );
1047+
break;
1048+
1049+
case tmrCOMMAND_DELETE:
1050+
#if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
10581051
{
1059-
vPortFree( pxTimer );
1052+
/* The timer has already been removed from the active list,
1053+
* just free up the memory if the memory was dynamically
1054+
* allocated. */
1055+
if( ( pxTimer->ucStatus & tmrSTATUS_IS_STATICALLY_ALLOCATED ) == ( uint8_t ) 0 )
1056+
{
1057+
vPortFree( pxTimer );
1058+
}
1059+
else
1060+
{
1061+
pxTimer->ucStatus &= ( ( uint8_t ) ~tmrSTATUS_IS_ACTIVE );
1062+
}
10601063
}
1061-
else
1064+
#else /* if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) */
10621065
{
1066+
/* If dynamic allocation is not enabled, the memory
1067+
* could not have been dynamically allocated. So there is
1068+
* no need to free the memory - just mark the timer as
1069+
* "not active". */
10631070
pxTimer->ucStatus &= ( ( uint8_t ) ~tmrSTATUS_IS_ACTIVE );
10641071
}
1065-
}
1066-
#else /* if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) */
1067-
{
1068-
/* If dynamic allocation is not enabled, the memory
1069-
* could not have been dynamically allocated. So there is
1070-
* no need to free the memory - just mark the timer as
1071-
* "not active". */
1072-
pxTimer->ucStatus &= ( ( uint8_t ) ~tmrSTATUS_IS_ACTIVE );
1073-
}
1074-
#endif /* configSUPPORT_DYNAMIC_ALLOCATION */
1075-
break;
1072+
#endif /* configSUPPORT_DYNAMIC_ALLOCATION */
1073+
break;
10761074

1077-
default:
1078-
/* Don't expect to get here. */
1079-
break;
1075+
default:
1076+
/* Don't expect to get here. */
1077+
break;
1078+
}
1079+
}
1080+
else
1081+
{
1082+
mtCOVERAGE_TEST_MARKER();
10801083
}
10811084
}
10821085
}

0 commit comments

Comments
 (0)