Skip to content

Commit c86c683

Browse files
committed
Fix MISRA C 2012 Rule 20.4: Replace #define static with STATIC macro
Replace `#define static` with `#define STATIC static` to prevent the macro from shadowing the C `static` keyword. This also ensures static variables in `vApplicationGetIdleTaskMemory()` and `vApplicationGetPassiveIdleTaskMemory()` remain static even when portREMOVE_STATIC_QUALIFIER is defined, preventing use-after-free bugs from stack-allocated storage.
1 parent d1f551e commit c86c683

2 files changed

Lines changed: 39 additions & 35 deletions

File tree

croutine.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,26 @@
3838
* than file scope.
3939
*/
4040
#ifdef portREMOVE_STATIC_QUALIFIER
41-
#define static
41+
#define STATIC
42+
#else
43+
#define STATIC static
4244
#endif
4345

4446

4547
/* Lists for ready and blocked co-routines. --------------------*/
46-
static List_t pxReadyCoRoutineLists[ configMAX_CO_ROUTINE_PRIORITIES ]; /**< Prioritised ready co-routines. */
47-
static List_t xDelayedCoRoutineList1; /**< Delayed co-routines. */
48-
static List_t xDelayedCoRoutineList2; /**< Delayed co-routines (two lists are used - one for delays that have overflowed the current tick count. */
49-
static List_t * pxDelayedCoRoutineList = NULL; /**< Points to the delayed co-routine list currently being used. */
50-
static List_t * pxOverflowDelayedCoRoutineList = NULL; /**< Points to the delayed co-routine list currently being used to hold co-routines that have overflowed the current tick count. */
51-
static List_t xPendingReadyCoRoutineList; /**< Holds co-routines that have been readied by an external event. They cannot be added directly to the ready lists as the ready lists cannot be accessed by interrupts. */
48+
STATIC List_t pxReadyCoRoutineLists[ configMAX_CO_ROUTINE_PRIORITIES ]; /**< Prioritised ready co-routines. */
49+
STATIC List_t xDelayedCoRoutineList1; /**< Delayed co-routines. */
50+
STATIC List_t xDelayedCoRoutineList2; /**< Delayed co-routines (two lists are used - one for delays that have overflowed the current tick count. */
51+
STATIC List_t * pxDelayedCoRoutineList = NULL; /**< Points to the delayed co-routine list currently being used. */
52+
STATIC List_t * pxOverflowDelayedCoRoutineList = NULL; /**< Points to the delayed co-routine list currently being used to hold co-routines that have overflowed the current tick count. */
53+
STATIC List_t xPendingReadyCoRoutineList; /**< Holds co-routines that have been readied by an external event. They cannot be added directly to the ready lists as the ready lists cannot be accessed by interrupts. */
5254

5355
/* Other file private variables. --------------------------------*/
5456
CRCB_t * pxCurrentCoRoutine = NULL;
55-
static UBaseType_t uxTopCoRoutineReadyPriority = ( UBaseType_t ) 0U;
56-
static TickType_t xCoRoutineTickCount = ( TickType_t ) 0U;
57-
static TickType_t xLastTickCount = ( TickType_t ) 0U;
58-
static TickType_t xPassedTicks = ( TickType_t ) 0U;
57+
STATIC UBaseType_t uxTopCoRoutineReadyPriority = ( UBaseType_t ) 0U;
58+
STATIC TickType_t xCoRoutineTickCount = ( TickType_t ) 0U;
59+
STATIC TickType_t xLastTickCount = ( TickType_t ) 0U;
60+
STATIC TickType_t xPassedTicks = ( TickType_t ) 0U;
5961

6062
/* The initial state of the co-routine when it is created. */
6163
#define corINITIAL_STATE ( 0 )

tasks.c

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@
148148
* global, rather than file scope.
149149
*/
150150
#ifdef portREMOVE_STATIC_QUALIFIER
151-
#define static
151+
#define STATIC
152+
#else
153+
#define STATIC static
152154
#endif
153155

154156
/* The name allocated to the Idle task. This can be overridden by defining
@@ -473,23 +475,23 @@ typedef tskTCB TCB_t;
473475
* xDelayedTaskList1 and xDelayedTaskList2 could be moved to function scope but
474476
* doing so breaks some kernel aware debuggers and debuggers that rely on removing
475477
* the static qualifier. */
476-
PRIVILEGED_DATA static List_t pxReadyTasksLists[ configMAX_PRIORITIES ]; /**< Prioritised ready tasks. */
477-
PRIVILEGED_DATA static List_t xDelayedTaskList1; /**< Delayed tasks. */
478-
PRIVILEGED_DATA static List_t xDelayedTaskList2; /**< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
479-
PRIVILEGED_DATA static List_t * volatile pxDelayedTaskList; /**< Points to the delayed task list currently being used. */
480-
PRIVILEGED_DATA static List_t * volatile pxOverflowDelayedTaskList; /**< Points to the delayed task list currently being used to hold tasks that have overflowed the current tick count. */
481-
PRIVILEGED_DATA static List_t xPendingReadyList; /**< Tasks that have been readied while the scheduler was suspended. They will be moved to the ready list when the scheduler is resumed. */
478+
PRIVILEGED_DATA STATIC List_t pxReadyTasksLists[ configMAX_PRIORITIES ]; /**< Prioritised ready tasks. */
479+
PRIVILEGED_DATA STATIC List_t xDelayedTaskList1; /**< Delayed tasks. */
480+
PRIVILEGED_DATA STATIC List_t xDelayedTaskList2; /**< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
481+
PRIVILEGED_DATA STATIC List_t * volatile pxDelayedTaskList; /**< Points to the delayed task list currently being used. */
482+
PRIVILEGED_DATA STATIC List_t * volatile pxOverflowDelayedTaskList; /**< Points to the delayed task list currently being used to hold tasks that have overflowed the current tick count. */
483+
PRIVILEGED_DATA STATIC List_t xPendingReadyList; /**< Tasks that have been readied while the scheduler was suspended. They will be moved to the ready list when the scheduler is resumed. */
482484

483485
#if ( INCLUDE_vTaskDelete == 1 )
484486

485-
PRIVILEGED_DATA static List_t xTasksWaitingTermination; /**< Tasks that have been deleted - but their memory not yet freed. */
486-
PRIVILEGED_DATA static volatile UBaseType_t uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
487+
PRIVILEGED_DATA STATIC List_t xTasksWaitingTermination; /**< Tasks that have been deleted - but their memory not yet freed. */
488+
PRIVILEGED_DATA STATIC volatile UBaseType_t uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
487489

488490
#endif
489491

490492
#if ( INCLUDE_vTaskSuspend == 1 )
491493

492-
PRIVILEGED_DATA static List_t xSuspendedTaskList; /**< Tasks that are currently suspended. */
494+
PRIVILEGED_DATA STATIC List_t xSuspendedTaskList; /**< Tasks that are currently suspended. */
493495

494496
#endif
495497

@@ -500,21 +502,21 @@ PRIVILEGED_DATA static List_t xPendingReadyList; /**< Ta
500502
#endif
501503

502504
/* Other file private variables. --------------------------------*/
503-
PRIVILEGED_DATA static volatile UBaseType_t uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
504-
PRIVILEGED_DATA static volatile TickType_t xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
505-
PRIVILEGED_DATA static volatile UBaseType_t uxTopReadyPriority = tskIDLE_PRIORITY;
506-
PRIVILEGED_DATA static volatile BaseType_t xSchedulerRunning = pdFALSE;
507-
PRIVILEGED_DATA static volatile TickType_t xPendedTicks = ( TickType_t ) 0U;
508-
PRIVILEGED_DATA static volatile BaseType_t xYieldPendings[ configNUMBER_OF_CORES ] = { pdFALSE };
509-
PRIVILEGED_DATA static volatile BaseType_t xNumOfOverflows = ( BaseType_t ) 0;
510-
PRIVILEGED_DATA static UBaseType_t uxTaskNumber = ( UBaseType_t ) 0U;
511-
PRIVILEGED_DATA static volatile TickType_t xNextTaskUnblockTime = ( TickType_t ) 0U; /* Initialised to portMAX_DELAY before the scheduler starts. */
512-
PRIVILEGED_DATA static TaskHandle_t xIdleTaskHandles[ configNUMBER_OF_CORES ]; /**< Holds the handles of the idle tasks. The idle tasks are created automatically when the scheduler is started. */
505+
PRIVILEGED_DATA STATIC volatile UBaseType_t uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
506+
PRIVILEGED_DATA STATIC volatile TickType_t xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
507+
PRIVILEGED_DATA STATIC volatile UBaseType_t uxTopReadyPriority = tskIDLE_PRIORITY;
508+
PRIVILEGED_DATA STATIC volatile BaseType_t xSchedulerRunning = pdFALSE;
509+
PRIVILEGED_DATA STATIC volatile TickType_t xPendedTicks = ( TickType_t ) 0U;
510+
PRIVILEGED_DATA STATIC volatile BaseType_t xYieldPendings[ configNUMBER_OF_CORES ] = { pdFALSE };
511+
PRIVILEGED_DATA STATIC volatile BaseType_t xNumOfOverflows = ( BaseType_t ) 0;
512+
PRIVILEGED_DATA STATIC UBaseType_t uxTaskNumber = ( UBaseType_t ) 0U;
513+
PRIVILEGED_DATA STATIC volatile TickType_t xNextTaskUnblockTime = ( TickType_t ) 0U; /* Initialised to portMAX_DELAY before the scheduler starts. */
514+
PRIVILEGED_DATA STATIC TaskHandle_t xIdleTaskHandles[ configNUMBER_OF_CORES ]; /**< Holds the handles of the idle tasks. The idle tasks are created automatically when the scheduler is started. */
513515

514516
/* Improve support for OpenOCD. The kernel tracks Ready tasks via priority lists.
515517
* For tracking the state of remote threads, OpenOCD uses uxTopUsedPriority
516518
* to determine the number of priority lists to read back from the remote target. */
517-
static const volatile UBaseType_t uxTopUsedPriority = configMAX_PRIORITIES - 1U;
519+
STATIC const volatile UBaseType_t uxTopUsedPriority = configMAX_PRIORITIES - 1U;
518520

519521
/* Context switches are held pending while the scheduler is suspended. Also,
520522
* interrupts must not manipulate the xStateListItem of a TCB, or any of the
@@ -528,14 +530,14 @@ static const volatile UBaseType_t uxTopUsedPriority = configMAX_PRIORITIES - 1U;
528530
* Updates to uxSchedulerSuspended must be protected by both the task lock and the ISR lock
529531
* and must not be done from an ISR. Reads must be protected by either lock and may be done
530532
* from either an ISR or a task. */
531-
PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended = ( UBaseType_t ) 0U;
533+
PRIVILEGED_DATA STATIC volatile UBaseType_t uxSchedulerSuspended = ( UBaseType_t ) 0U;
532534

533535
#if ( configGENERATE_RUN_TIME_STATS == 1 )
534536

535537
/* Do not move these variables to function scope as doing so prevents the
536538
* code working with debuggers that need to remove the static qualifier. */
537-
PRIVILEGED_DATA static configRUN_TIME_COUNTER_TYPE ulTaskSwitchedInTime[ configNUMBER_OF_CORES ] = { 0U }; /**< Holds the value of a timer/counter the last time a task was switched in. */
538-
PRIVILEGED_DATA static volatile configRUN_TIME_COUNTER_TYPE ulTotalRunTime[ configNUMBER_OF_CORES ] = { 0U }; /**< Holds the total amount of execution time as defined by the run time counter clock. */
539+
PRIVILEGED_DATA STATIC configRUN_TIME_COUNTER_TYPE ulTaskSwitchedInTime[ configNUMBER_OF_CORES ] = { 0U }; /**< Holds the value of a timer/counter the last time a task was switched in. */
540+
PRIVILEGED_DATA STATIC volatile configRUN_TIME_COUNTER_TYPE ulTotalRunTime[ configNUMBER_OF_CORES ] = { 0U }; /**< Holds the total amount of execution time as defined by the run time counter clock. */
539541

540542
#endif
541543

0 commit comments

Comments
 (0)