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

Fix MISRA violations #110

Merged
merged 8 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading