Skip to content

Commit 8314c29

Browse files
authored
Fix MISRA violations (#110)
* Fix MISRA C-2012 Directive 4.6 * Suppress MISRA C-2012 Rule 11.3 * Suppress MISRA C-2012 Rule 8.7 * Suppress MISRA C-2012 Rule 8.9 * Suppress MISRA C-2012 Rule 8.13 * Fix MISRA C-2012 Rule 8.9 * Fix formatting
1 parent 955b9a2 commit 8314c29

File tree

3 files changed

+39
-18
lines changed

3 files changed

+39
-18
lines changed

MISRA.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,17 @@ Deviations from the MISRA standard are listed below:
2323
| Rule 8.13 | Advisory | Functions that are passed as pointers to coreMQTT or the agent must exactly match function signatures with the pointer type definition, so `const` modifiers cannot be added even if a specific function implementation does not modify a given parameter. |
2424

2525
### Suppressed with Coverity Comments
26-
*None.*
26+
To find the deviation references in the source files run grep on the source code
27+
with ( Assuming rule 11.3 violation; with justification in point 1 ):
28+
```
29+
grep 'MISRA Ref 11.3.1' . -rI
30+
```
31+
#### Rule 11.3
32+
33+
_Ref 11.3.1_
34+
35+
- MISRA C-2012 Rule 11.3 states that a cast shall not be performed between a pointer to
36+
to object type and a pointer to a different object type. In this library, the MQTT stack
37+
processes data as byte stream, requiring casting to specific data structure. However this
38+
casting is safe because the buffers are aligned to a 4-byte boundaries, ensuring that no
39+
unaligned memory access occurs.

source/core_mqtt_agent.c

+17-17
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,6 @@
5151

5252
/*-----------------------------------------------------------*/
5353

54-
#if ( MQTT_AGENT_USE_QOS_1_2_PUBLISH != 0 )
55-
56-
/**
57-
* @brief Array used to maintain the outgoing publish records and their
58-
* state by the coreMQTT library.
59-
*/
60-
static MQTTPubAckInfo_t pOutgoingPublishRecords[ MQTT_AGENT_MAX_OUTSTANDING_ACKS ];
61-
62-
/**
63-
* @brief Array used to maintain the incoming publish records and their
64-
* state by the coreMQTT library.
65-
*/
66-
static MQTTPubAckInfo_t pIncomingPublishRecords[ MQTT_AGENT_MAX_OUTSTANDING_ACKS ];
67-
#endif
68-
6954
/**
7055
* @brief Track an operation by adding it to a list, indicating it is anticipating
7156
* an acknowledgment.
@@ -562,9 +547,9 @@ static MQTTStatus_t processCommand( MQTTAgentContext_t * pMqttAgentContext,
562547

563548
if( pCommand != NULL )
564549
{
565-
assert( ( unsigned int ) pCommand->commandType < ( unsigned int ) NUM_COMMANDS );
550+
assert( ( uint32_t ) pCommand->commandType < ( uint32_t ) NUM_COMMANDS );
566551

567-
if( ( unsigned int ) pCommand->commandType < ( unsigned int ) NUM_COMMANDS )
552+
if( ( uint32_t ) pCommand->commandType < ( uint32_t ) NUM_COMMANDS )
568553
{
569554
commandFunction = pCommandFunctionTable[ pCommand->commandType ];
570555
pCommandArgs = pCommand->pArgs;
@@ -657,6 +642,9 @@ static MQTTAgentContext_t * getAgentFromMQTTContext( MQTTContext_t * pMQTTContex
657642
MQTTAgentContext_t ctx = { 0 };
658643
ptrdiff_t offset = ( ( uint8_t * ) &( ctx.mqttContext ) ) - ( ( uint8_t * ) &ctx );
659644

645+
/* MISRA Ref 11.3.1 [Misaligned access] */
646+
/* More details at: https://github.com/FreeRTOS/coreMQTT-Agent/blob/main/MISRA.md#rule-113 */
647+
/* coverity[misra_c_2012_rule_11_3_violation] */
660648
return ( MQTTAgentContext_t * ) &( ( ( uint8_t * ) pMQTTContext )[ 0 - offset ] );
661649
}
662650

@@ -987,6 +975,18 @@ MQTTStatus_t MQTTAgent_Init( MQTTAgentContext_t * pMqttAgentContext,
987975
{
988976
MQTTStatus_t returnStatus;
989977

978+
/**
979+
* @brief Array used to maintain the outgoing publish records and their
980+
* state by the coreMQTT library.
981+
*/
982+
static MQTTPubAckInfo_t pIncomingPublishRecords[ MQTT_AGENT_MAX_OUTSTANDING_ACKS ];
983+
984+
/**
985+
* @brief Array used to maintain the outgoing publish records and their
986+
* state by the coreMQTT library.
987+
*/
988+
static MQTTPubAckInfo_t pOutgoingPublishRecords[ MQTT_AGENT_MAX_OUTSTANDING_ACKS ];
989+
990990
if( ( pMqttAgentContext == NULL ) ||
991991
( pMsgInterface == NULL ) ||
992992
( pTransportInterface == NULL ) ||

tools/coverity/misra.config

+8
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@
3232
"deviation": "Rule 3.1",
3333
"reason": "Allow nested comments. Documentation blocks contain comments for example code."
3434
},
35+
{
36+
"deviation": "Rule 8.7",
37+
"reason": "API functions are not used by the library outside of the files they are defined; however, they must be externally visible in order to be used by an application."
38+
},
39+
{
40+
"deviation": "Rule 8.13",
41+
"reason": "Allow to not to use const-qualified type for callback function."
42+
},
3543
{
3644
"deviation": "Rule 11.5",
3745
"reason": "Allow casts from void *. Contexts are passed as void * and must be cast to the correct data type before use."

0 commit comments

Comments
 (0)