Skip to content

Commit f0f8e9e

Browse files
committed
Fix MISRA violations for Kernel release V11.2.0
1 parent df0aa5a commit f0f8e9e

File tree

5 files changed

+115
-101
lines changed

5 files changed

+115
-101
lines changed

MISRA.md

Lines changed: 2 additions & 2 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 4 additions & 1 deletion
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 100 additions & 93 deletions
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,
@@ -462,7 +462,7 @@
462462

463463
/* Send a message to the timer service task to perform a particular action
464464
* on a particular timer definition. */
465-
if( xTimerQueue != NULL )
465+
if( ( xTimerQueue != NULL ) && ( xTimer != NULL ) )
466466
{
467467
/* Send a command to the timer service task to start the xTimer timer. */
468468
xMessage.xMessageID = xCommandID;
@@ -513,7 +513,7 @@
513513

514514
/* Send a message to the timer service task to perform a particular action
515515
* on a particular timer definition. */
516-
if( xTimerQueue != NULL )
516+
if( ( xTimerQueue != NULL ) && ( xTimer != NULL ) )
517517
{
518518
/* Send a command to the timer service task to start the xTimer timer. */
519519
xMessage.xMessageID = xCommandID;
@@ -974,109 +974,116 @@
974974
* software timer. */
975975
pxTimer = xMessage.u.xTimerParameters.pxTimer;
976976

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

987-
traceTIMER_COMMAND_RECEIVED( pxTimer, xMessage.xMessageID, xMessage.u.xTimerParameters.xMessageValue );
989+
traceTIMER_COMMAND_RECEIVED( pxTimer, xMessage.xMessageID, xMessage.u.xTimerParameters.xMessageValue );
988990

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

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 )
999+
switch( xMessage.xMessageID )
1000+
{
1001+
case tmrCOMMAND_START:
1002+
case tmrCOMMAND_START_FROM_ISR:
1003+
case tmrCOMMAND_RESET:
1004+
case tmrCOMMAND_RESET_FROM_ISR:
1005+
/* Start or restart a timer. */
1006+
pxTimer->ucStatus |= ( uint8_t ) tmrSTATUS_IS_ACTIVE;
1007+
1008+
if( prvInsertTimerInActiveList( pxTimer, xMessage.u.xTimerParameters.xMessageValue + pxTimer->xTimerPeriodInTicks, xTimeNow, xMessage.u.xTimerParameters.xMessageValue ) != pdFALSE )
10111009
{
1012-
prvReloadTimer( pxTimer, xMessage.u.xTimerParameters.xMessageValue + pxTimer->xTimerPeriodInTicks, xTimeNow );
1010+
/* The timer expired before it was added to the active
1011+
* timer list. Process it now. */
1012+
if( ( pxTimer->ucStatus & tmrSTATUS_IS_AUTORELOAD ) != 0U )
1013+
{
1014+
prvReloadTimer( pxTimer, xMessage.u.xTimerParameters.xMessageValue + pxTimer->xTimerPeriodInTicks, xTimeNow );
1015+
}
1016+
else
1017+
{
1018+
pxTimer->ucStatus &= ( ( uint8_t ) ~tmrSTATUS_IS_ACTIVE );
1019+
}
1020+
1021+
/* Call the timer callback. */
1022+
traceTIMER_EXPIRED( pxTimer );
1023+
pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );
10131024
}
10141025
else
10151026
{
1016-
pxTimer->ucStatus &= ( ( uint8_t ) ~tmrSTATUS_IS_ACTIVE );
1027+
mtCOVERAGE_TEST_MARKER();
10171028
}
10181029

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 )
1030+
break;
1031+
1032+
case tmrCOMMAND_STOP:
1033+
case tmrCOMMAND_STOP_FROM_ISR:
1034+
/* The timer has already been removed from the active list. */
1035+
pxTimer->ucStatus &= ( ( uint8_t ) ~tmrSTATUS_IS_ACTIVE );
1036+
break;
1037+
1038+
case tmrCOMMAND_CHANGE_PERIOD:
1039+
case tmrCOMMAND_CHANGE_PERIOD_FROM_ISR:
1040+
pxTimer->ucStatus |= ( uint8_t ) tmrSTATUS_IS_ACTIVE;
1041+
pxTimer->xTimerPeriodInTicks = xMessage.u.xTimerParameters.xMessageValue;
1042+
configASSERT( ( pxTimer->xTimerPeriodInTicks > 0 ) );
1043+
1044+
/* The new period does not really have a reference, and can
1045+
* be longer or shorter than the old one. The command time is
1046+
* therefore set to the current time, and as the period cannot
1047+
* be zero the next expiry time can only be in the future,
1048+
* meaning (unlike for the xTimerStart() case above) there is
1049+
* no fail case that needs to be handled here. */
1050+
( void ) prvInsertTimerInActiveList( pxTimer, ( xTimeNow + pxTimer->xTimerPeriodInTicks ), xTimeNow, xTimeNow );
1051+
break;
1052+
1053+
case tmrCOMMAND_DELETE:
1054+
#if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
10581055
{
1059-
vPortFree( pxTimer );
1056+
/* The timer has already been removed from the active list,
1057+
* just free up the memory if the memory was dynamically
1058+
* allocated. */
1059+
if( ( pxTimer->ucStatus & tmrSTATUS_IS_STATICALLY_ALLOCATED ) == ( uint8_t ) 0 )
1060+
{
1061+
vPortFree( pxTimer );
1062+
}
1063+
else
1064+
{
1065+
pxTimer->ucStatus &= ( ( uint8_t ) ~tmrSTATUS_IS_ACTIVE );
1066+
}
10601067
}
1061-
else
1068+
#else /* if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) */
10621069
{
1070+
/* If dynamic allocation is not enabled, the memory
1071+
* could not have been dynamically allocated. So there is
1072+
* no need to free the memory - just mark the timer as
1073+
* "not active". */
10631074
pxTimer->ucStatus &= ( ( uint8_t ) ~tmrSTATUS_IS_ACTIVE );
10641075
}
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;
1076+
#endif /* configSUPPORT_DYNAMIC_ALLOCATION */
1077+
break;
10761078

1077-
default:
1078-
/* Don't expect to get here. */
1079-
break;
1079+
default:
1080+
/* Don't expect to get here. */
1081+
break;
1082+
}
1083+
}
1084+
else
1085+
{
1086+
mtCOVERAGE_TEST_MARKER();
10801087
}
10811088
}
10821089
}

0 commit comments

Comments
 (0)