From 94d1aac92a86cc08e117037e876f507e45757cc4 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Wed, 21 Feb 2024 08:52:20 +0000 Subject: [PATCH] Fix MISRA C 2012 deviations * Fix rule 18.6 deviations. Not to operaters on pointer. * Fix rule 9.1 deviations. Initialize the local variable to prevent use of uninitialized variable. --- source/core_sntp_client.c | 2 +- source/core_sntp_serializer.c | 18 ++++++++++-------- tools/coverity/misra.config | 28 ++++++++++++++-------------- 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/source/core_sntp_client.c b/source/core_sntp_client.c index b84d45f..240ead1 100644 --- a/source/core_sntp_client.c +++ b/source/core_sntp_client.c @@ -675,7 +675,7 @@ static SntpStatus_t processServerResponse( SntpContext_t * pContext, if( status == SntpSuccess ) { - SntpResponseData_t parsedResponse; + SntpResponseData_t parsedResponse = { 0 }; /* De-serialize response packet to determine whether the server accepted or rejected * the request for time. Also, calculate the system clock offset if the server responded diff --git a/source/core_sntp_serializer.c b/source/core_sntp_serializer.c index 14fffed..a0fd94c 100644 --- a/source/core_sntp_serializer.c +++ b/source/core_sntp_serializer.c @@ -221,12 +221,14 @@ typedef struct SntpPacket static void fillWordMemoryInNetworkOrder( uint32_t * pWordMemory, uint32_t data ) { + uint8_t * pByteMemory = ( uint8_t * ) pWordMemory; + assert( pWordMemory != NULL ); - *( ( uint8_t * ) pWordMemory ) = ( uint8_t ) ( data >> 24 ); - *( ( uint8_t * ) pWordMemory + 1 ) = ( uint8_t ) ( ( data >> 16 ) & 0x000000FFU ); - *( ( uint8_t * ) pWordMemory + 2 ) = ( uint8_t ) ( ( data >> 8 ) & 0x000000FFU ); - *( ( uint8_t * ) pWordMemory + 3 ) = ( uint8_t ) ( ( data ) & 0x000000FFU ); + pByteMemory[ 0 ] = ( uint8_t ) ( data >> 24 ); + pByteMemory[ 1 ] = ( uint8_t ) ( ( data >> 16 ) & 0x000000FFU ); + pByteMemory[ 2 ] = ( uint8_t ) ( ( data >> 8 ) & 0x000000FFU ); + pByteMemory[ 3 ] = ( uint8_t ) ( ( data ) & 0x000000FFU ); } /** @@ -244,10 +246,10 @@ static uint32_t readWordFromNetworkByteOrderMemory( const uint32_t * ptr ) assert( ptr != NULL ); - return ( uint32_t ) ( ( ( uint32_t ) *( pMemStartByte ) << 24 ) | - ( 0x00FF0000U & ( ( uint32_t ) *( pMemStartByte + 1 ) << 16 ) ) | - ( 0x0000FF00U & ( ( uint32_t ) *( pMemStartByte + 2 ) << 8 ) ) | - ( ( uint32_t ) *( pMemStartByte + 3 ) ) ); + return ( uint32_t ) ( ( ( uint32_t ) pMemStartByte[ 0 ] << 24 ) | + ( 0x00FF0000U & ( ( uint32_t ) pMemStartByte[ 1 ] << 16 ) ) | + ( 0x0000FF00U & ( ( uint32_t ) pMemStartByte[ 2 ] << 8 ) ) | + ( ( uint32_t ) pMemStartByte[ 3 ] ) ); } /** diff --git a/tools/coverity/misra.config b/tools/coverity/misra.config index a672ba6..290489a 100644 --- a/tools/coverity/misra.config +++ b/tools/coverity/misra.config @@ -1,30 +1,30 @@ // MISRA C-2012 Rules { - version : "2.0", - standard : "c2012", - title: "Coverity MISRA Configuration", - deviations : [ + "version" : "2.0", + "standard" : "c2012", + "title" : "Coverity MISRA Configuration", + "deviations" : [ // Disable the following rules. { - deviation: "Directive 4.9", - reason: "Allow inclusion of function like macros. Asserts and logging are done using function like macros." + "deviation": "Directive 4.9", + "reason": "Allow inclusion of function like macros. Asserts and logging are done using function like macros." }, { - deviation: "Rule 2.4", - reason: "Allow unused tags. Some compilers warn if types are not tagged." + "deviation": "Rule 2.4", + "reason": "Allow unused tags. Some compilers warn if types are not tagged." }, { - deviation: "Rule 2.5", - reason: "Allow unused macros. coreSNTP Library headers define macros intended for the application's use, but are not used by the agent." + "deviation": "Rule 2.5", + "reason": "Allow unused macros. coreSNTP Library headers define macros intended for the application's use, but are not used by the agent." }, { - deviation: "Rule 3.1", - reason: "Allow nested comments. Documentation blocks contain comments for example code." + "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 library. They must be externally visible in order to be used by the application." + "deviation": "Rule 8.7", + "reason": "API functions are not used by library. They must be externally visible in order to be used by the application." }, ] }