Skip to content

Commit

Permalink
Fix MISRA violations (#110)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
moninom1 authored Aug 13, 2024
1 parent 955b9a2 commit 8314c29
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
15 changes: 14 additions & 1 deletion MISRA.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,17 @@ Deviations from the MISRA standard are listed below:
| 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. |

### Suppressed with Coverity Comments
*None.*
To find the deviation references in the source files run grep on the source code
with ( Assuming rule 11.3 violation; with justification in point 1 ):
```
grep 'MISRA Ref 11.3.1' . -rI
```
#### Rule 11.3

_Ref 11.3.1_

- MISRA C-2012 Rule 11.3 states that a cast shall not be performed between a pointer to
to object type and a pointer to a different object type. In this library, the MQTT stack
processes data as byte stream, requiring casting to specific data structure. However this
casting is safe because the buffers are aligned to a 4-byte boundaries, ensuring that no
unaligned memory access occurs.
34 changes: 17 additions & 17 deletions source/core_mqtt_agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,6 @@

/*-----------------------------------------------------------*/

#if ( MQTT_AGENT_USE_QOS_1_2_PUBLISH != 0 )

/**
* @brief Array used to maintain the outgoing publish records and their
* state by the coreMQTT library.
*/
static MQTTPubAckInfo_t pOutgoingPublishRecords[ MQTT_AGENT_MAX_OUTSTANDING_ACKS ];

/**
* @brief Array used to maintain the incoming publish records and their
* state by the coreMQTT library.
*/
static MQTTPubAckInfo_t pIncomingPublishRecords[ MQTT_AGENT_MAX_OUTSTANDING_ACKS ];
#endif

/**
* @brief Track an operation by adding it to a list, indicating it is anticipating
* an acknowledgment.
Expand Down Expand Up @@ -562,9 +547,9 @@ static MQTTStatus_t processCommand( MQTTAgentContext_t * pMqttAgentContext,

if( pCommand != NULL )
{
assert( ( unsigned int ) pCommand->commandType < ( unsigned int ) NUM_COMMANDS );
assert( ( uint32_t ) pCommand->commandType < ( uint32_t ) NUM_COMMANDS );

if( ( unsigned int ) pCommand->commandType < ( unsigned int ) NUM_COMMANDS )
if( ( uint32_t ) pCommand->commandType < ( uint32_t ) NUM_COMMANDS )
{
commandFunction = pCommandFunctionTable[ pCommand->commandType ];
pCommandArgs = pCommand->pArgs;
Expand Down Expand Up @@ -657,6 +642,9 @@ static MQTTAgentContext_t * getAgentFromMQTTContext( MQTTContext_t * pMQTTContex
MQTTAgentContext_t ctx = { 0 };
ptrdiff_t offset = ( ( uint8_t * ) &( ctx.mqttContext ) ) - ( ( uint8_t * ) &ctx );

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

Expand Down Expand Up @@ -987,6 +975,18 @@ MQTTStatus_t MQTTAgent_Init( MQTTAgentContext_t * pMqttAgentContext,
{
MQTTStatus_t returnStatus;

/**
* @brief Array used to maintain the outgoing publish records and their
* state by the coreMQTT library.
*/
static MQTTPubAckInfo_t pIncomingPublishRecords[ MQTT_AGENT_MAX_OUTSTANDING_ACKS ];

/**
* @brief Array used to maintain the outgoing publish records and their
* state by the coreMQTT library.
*/
static MQTTPubAckInfo_t pOutgoingPublishRecords[ MQTT_AGENT_MAX_OUTSTANDING_ACKS ];

if( ( pMqttAgentContext == NULL ) ||
( pMsgInterface == NULL ) ||
( pTransportInterface == NULL ) ||
Expand Down
8 changes: 8 additions & 0 deletions tools/coverity/misra.config
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@
"deviation": "Rule 3.1",
"reason": "Allow nested comments. Documentation blocks contain comments for example code."
},
{
"deviation": "Rule 8.7",
"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."
},
{
"deviation": "Rule 8.13",
"reason": "Allow to not to use const-qualified type for callback function."
},
{
"deviation": "Rule 11.5",
"reason": "Allow casts from void *. Contexts are passed as void * and must be cast to the correct data type before use."
Expand Down

0 comments on commit 8314c29

Please sign in to comment.