diff --git a/MISRA.md b/MISRA.md index 3e8f0ea..0d6871d 100644 --- a/MISRA.md +++ b/MISRA.md @@ -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. diff --git a/source/core_mqtt_agent.c b/source/core_mqtt_agent.c index f9ec26b..45ad94d 100644 --- a/source/core_mqtt_agent.c +++ b/source/core_mqtt_agent.c @@ -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. @@ -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; @@ -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 ] ); } @@ -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 ) || diff --git a/tools/coverity/misra.config b/tools/coverity/misra.config index 8a5031b..12bdc33 100644 --- a/tools/coverity/misra.config +++ b/tools/coverity/misra.config @@ -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."