Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress MISRA C rule 11.3 in MISRA.md #857

Merged
merged 9 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion MISRA.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ _Ref 8.4.1_
a declaration in header file is not useful as the assembly code will
still need to declare it separately.


#### Rule 11.3

_Ref 11.3.1_

- MISRA C:2012 Rule 11.3: A cast shall not be performed between a pointer to object
type and a pointer to a different object type.
The rule requires not to cast a pointer to object into a pointer to a
different object to prevent undefined behavior due to incorrectly aligned.
To support static memory allocation, FreeRTOS creates static type kernel
objects which are aliases for kernel object type with prefix "Static" for
data hiding purpose. A static kernel object type is guaranteed to have the
same size and alignment with kernel object, which is checked by configASSERT.
Static kernel object types include StaticEventGroup_t, StaticQueue_t,
StaticStreamBuffer_t, StaticTimer_t and StaticTask_t.


### MISRA configuration

Copy below content to `misra.conf` to run Coverity on FreeRTOS-Kernel.
Expand Down Expand Up @@ -69,4 +86,4 @@ Copy below content to `misra.conf` to run Coverity on FreeRTOS-Kernel.
}
]
}
```
```
11 changes: 10 additions & 1 deletion event_groups.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ static BaseType_t prvTestWaitCondition( const EventBits_t uxCurrentEventBits,
#endif /* configASSERT_DEFINED */

/* The user has provided a statically allocated event group - use it. */
pxEventBits = ( EventGroup_t * ) pxEventGroupBuffer; /*lint !e740 !e9087 EventGroup_t and StaticEventGroup_t are deliberately aliased for data hiding purposes and guaranteed to have the same size and alignment requirement - checked by configASSERT(). */
/* MISRA Ref 11.3.1 [Misaligned access] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
/* coverity[misra_c_2012_rule_11_3_violation] */
pxEventBits = ( EventGroup_t * ) pxEventGroupBuffer;

if( pxEventBits != NULL )
{
Expand Down Expand Up @@ -710,6 +713,9 @@ void vEventGroupDelete( EventGroupHandle_t xEventGroup )
/* Check if the event group was statically allocated. */
if( pxEventBits->ucStaticallyAllocated == ( uint8_t ) pdTRUE )
{
/* MISRA Ref 11.3.1 [Misaligned access] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
/* coverity[misra_c_2012_rule_11_3_violation] */
*ppxEventGroupBuffer = ( StaticEventGroup_t * ) pxEventBits;
xReturn = pdTRUE;
}
Expand All @@ -721,6 +727,9 @@ void vEventGroupDelete( EventGroupHandle_t xEventGroup )
#else /* configSUPPORT_DYNAMIC_ALLOCATION */
{
/* Event group must have been statically allocated. */
/* MISRA Ref 11.3.1 [Misaligned access] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
/* coverity[misra_c_2012_rule_11_3_violation] */
*ppxEventGroupBuffer = ( StaticEventGroup_t * ) pxEventBits;
xReturn = pdTRUE;
}
Expand Down
8 changes: 7 additions & 1 deletion queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,10 @@ BaseType_t xQueueGenericReset( QueueHandle_t xQueue,
/* The address of a statically allocated queue was passed in, use it.
* The address of a statically allocated storage area was also passed in
* but is already set. */
pxNewQueue = ( Queue_t * ) pxStaticQueue; /*lint !e740 !e9087 Unusual cast is ok as the structures are designed to have the same alignment, and the size is checked by an assert. */
/* MISRA Ref 11.3.1 [Misaligned access] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
/* coverity[misra_c_2012_rule_11_3_violation] */
pxNewQueue = ( Queue_t * ) pxStaticQueue;

#if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
{
Expand Down Expand Up @@ -459,6 +462,9 @@ BaseType_t xQueueGenericReset( QueueHandle_t xQueue,
*ppucQueueStorage = ( uint8_t * ) pxQueue->pcHead;
}

/* MISRA Ref 11.3.1 [Misaligned access] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
/* coverity[misra_c_2012_rule_11_3_violation] */
*ppxStaticQueue = ( StaticQueue_t * ) pxQueue;
xReturn = pdTRUE;
}
Expand Down
13 changes: 11 additions & 2 deletions stream_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,10 @@ static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,
StreamBufferCallbackFunction_t pxSendCompletedCallback,
StreamBufferCallbackFunction_t pxReceiveCompletedCallback )
{
StreamBuffer_t * const pxStreamBuffer = ( StreamBuffer_t * ) pxStaticStreamBuffer; /*lint !e740 !e9087 Safe cast as StaticStreamBuffer_t is opaque Streambuffer_t. */
/* MISRA Ref 11.3.1 [Misaligned access] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
/* coverity[misra_c_2012_rule_11_3_violation] */
StreamBuffer_t * const pxStreamBuffer = ( StreamBuffer_t * ) pxStaticStreamBuffer;
StreamBufferHandle_t xReturn;
uint8_t ucFlags;

Expand Down Expand Up @@ -466,7 +469,10 @@ static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,

traceSTREAM_BUFFER_CREATE( pxStreamBuffer, xIsMessageBuffer );

xReturn = ( StreamBufferHandle_t ) pxStaticStreamBuffer; /*lint !e9087 Data hiding requires cast to opaque type. */
/* MISRA Ref 11.3.1 [Misaligned access] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
/* coverity[misra_c_2012_rule_11_3_violation] */
xReturn = ( StreamBufferHandle_t ) pxStaticStreamBuffer;
}
else
{
Expand Down Expand Up @@ -498,6 +504,9 @@ static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,
if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_STATICALLY_ALLOCATED ) != ( uint8_t ) 0 )
{
*ppucStreamBufferStorageArea = pxStreamBuffer->pucBuffer;
/* MISRA Ref 11.3.1 [Misaligned access] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
/* coverity[misra_c_2012_rule_11_3_violation] */
*ppxStaticStreamBuffer = ( StaticStreamBuffer_t * ) pxStreamBuffer;
xReturn = pdTRUE;
}
Expand Down
8 changes: 7 additions & 1 deletion tasks.c
Original file line number Diff line number Diff line change
Expand Up @@ -1271,7 +1271,10 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
{
/* The memory used for the task's TCB and stack are passed into this
* function - use them. */
pxNewTCB = ( TCB_t * ) pxTaskBuffer; /*lint !e740 !e9087 Unusual cast is ok as the structures are designed to have the same alignment, and the size is checked by an assert. */
/* MISRA Ref 11.3.1 [Misaligned access] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
/* coverity[misra_c_2012_rule_11_3_violation] */
pxNewTCB = ( TCB_t * ) pxTaskBuffer;
( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;

Expand Down Expand Up @@ -4319,6 +4322,9 @@ char * pcTaskGetName( TaskHandle_t xTaskToQuery ) /*lint !e971 Unqualified char
if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB )
{
*ppuxStackBuffer = pxTCB->pxStack;
/* MISRA Ref 11.3.1 [Misaligned access] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
/* coverity[misra_c_2012_rule_11_3_violation] */
*ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
xReturn = pdTRUE;
}
Expand Down
8 changes: 7 additions & 1 deletion timers.c
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,10 @@

/* A pointer to a StaticTimer_t structure MUST be provided, use it. */
configASSERT( pxTimerBuffer );
pxNewTimer = ( Timer_t * ) pxTimerBuffer; /*lint !e740 !e9087 StaticTimer_t is a pointer to a Timer_t, so guaranteed to be aligned and sized correctly (checked by an assert()), so this is safe. */
/* MISRA Ref 11.3.1 [Misaligned access] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
/* coverity[misra_c_2012_rule_11_3_violation] */
pxNewTimer = ( Timer_t * ) pxTimerBuffer;

if( pxNewTimer != NULL )
{
Expand Down Expand Up @@ -664,6 +667,9 @@

if( ( pxTimer->ucStatus & tmrSTATUS_IS_STATICALLY_ALLOCATED ) != 0 )
{
/* MISRA Ref 11.3.1 [Misaligned access] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
/* coverity[misra_c_2012_rule_11_3_violation] */
*ppxTimerBuffer = ( StaticTimer_t * ) pxTimer;
xReturn = pdTRUE;
}
Expand Down
Loading