From 3805f5e544d4065deb7c99a9d954a92acde00025 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Wed, 21 Feb 2024 08:52:20 +0000 Subject: [PATCH 1/4] 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 | 33 +++++++++++++++------------------ 3 files changed, 26 insertions(+), 27 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..0249dfe 100644 --- a/tools/coverity/misra.config +++ b/tools/coverity/misra.config @@ -1,30 +1,27 @@ -// MISRA C-2012 Rules - { - version : "2.0", - standard : "c2012", - title: "Coverity MISRA Configuration", - deviations : [ - // Disable the following rules. + "version" : "2.0", + "standard" : "c2012", + "title" : "Coverity MISRA Configuration", + "deviations" : [ { - 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." + } ] } From 1dc86465468c2b830025e071eee638b113bc42ae Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Mon, 26 Feb 2024 06:57:53 +0000 Subject: [PATCH 2/4] Update CMakeList for build target * Rename UNIT_TEST to UNITTEST and COVERITY to COV_ANALYSIS to align FreeRTOS libraries --- .github/workflows/ci.yml | 4 +-- test/CMakeLists.txt | 54 ++++++++++++++++++++++++++-------------- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f03b4ef..e047620 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -61,7 +61,7 @@ jobs: cmake -S test -B build/ \ -G "Unix Makefiles" \ -DCMAKE_BUILD_TYPE=Debug \ - -DBUILD_UNIT_TESTS=ON \ + -DUNITTEST=ON \ -DCMAKE_C_FLAGS="${CFLAGS}" make -C build all -j8 @@ -84,7 +84,7 @@ jobs: cmake -S test -B build/ \ -G "Unix Makefiles" \ -DCMAKE_BUILD_TYPE=Debug \ - -DBUILD_UNIT_TESTS=ON \ + -DUNITTEST=ON \ -DCMAKE_C_FLAGS='--coverage -Wall -Wextra -Werror -DNDEBUG -Wno-error=pedantic -Wno-variadic-macros -DLOGGING_LEVEL_DEBUG=1' make -C build/ all diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d008170..c077cd3 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required( VERSION 3.13.0 ) -project( "coreSNTP unit test" +project( "coreSNTP tests" LANGUAGES C ) # Allow the project to be organized into folders. @@ -9,6 +9,13 @@ set_property( GLOBAL PROPERTY USE_FOLDERS ON ) set( CMAKE_C_STANDARD 90 ) set( CMAKE_C_STANDARD_REQUIRED ON ) +# If no configuration is defined, turn everything on. +if( NOT DEFINED COV_ANALYSIS AND NOT DEFINED UNITTEST AND NOT DEFINED BUILD_CODE_EXAMPLE ) + set( COV_ANALYSIS ON ) + set( UNITTEST ON ) + set( BUILD_CODE_EXAMPLE ON ) +endif() + # Do not allow in-source build. if( ${PROJECT_SOURCE_DIR} STREQUAL ${PROJECT_BINARY_DIR} ) message( FATAL_ERROR "In-source build is not allowed. Please build in a separate directory, such as ${PROJECT_SOURCE_DIR}/build." ) @@ -30,42 +37,51 @@ set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin ) set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib ) set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib ) -# ================================ Coverity Analysis Configuration ================================= - # Include filepaths for source and include. include( ${MODULE_ROOT_DIR}/coreSntpFilePaths.cmake ) -# Target for Coverity analysis that builds the library. -add_library( coverity_analysis - ${CORE_SNTP_SOURCES} ) +# ================================ Coverity Analysis Configuration ================================= + +if( COV_ANALYSIS ) -# Add coreSNTP library public include path. -target_include_directories( coverity_analysis - PUBLIC - ${CORE_SNTP_INCLUDE_PUBLIC_DIRS} ) + # Target for Coverity analysis that builds the library. + add_library( coverity_analysis + ${CORE_SNTP_SOURCES} ) + + # Add coreSNTP library public include path. + target_include_directories( coverity_analysis + PUBLIC + ${CORE_SNTP_INCLUDE_PUBLIC_DIRS} ) -# Build SNTP library target without custom config dependency. -target_compile_definitions( coverity_analysis PUBLIC SNTP_DO_NOT_USE_CUSTOM_CONFIG=1 ) + # Build SNTP library target without custom config dependency. + target_compile_definitions( coverity_analysis PUBLIC SNTP_DO_NOT_USE_CUSTOM_CONFIG=1 ) -# Build without debug enabled when performing static analysis -target_compile_options(coverity_analysis PUBLIC -DNDEBUG ) + # Build without debug enabled when performing static analysis + target_compile_options(coverity_analysis PUBLIC -DNDEBUG ) +endif() # ==================================== Code Example Build ==================================== -if(${BUILD_CODE_EXAMPLE}) +if( BUILD_CODE_EXAMPLE ) # Target for Coverity analysis that builds the library. add_executable( code_example_posix + ${CORE_SNTP_SOURCES} ${MODULE_ROOT_DIR}/docs/doxygen/code_examples/example_sntp_client_posix.c ) - # Add coreSNTP library public include path. - target_link_libraries( code_example_posix - coverity_analysis ) + target_include_directories( code_example_posix + PUBLIC + ${CORE_SNTP_INCLUDE_PUBLIC_DIRS} ) + + # Build SNTP library target without custom config dependency. + target_compile_definitions( code_example_posix PUBLIC SNTP_DO_NOT_USE_CUSTOM_CONFIG=1 ) + # Build without debug enabled when performing static analysis + target_compile_options( code_example_posix PUBLIC -DNDEBUG ) endif() # ==================================== Unit Test Configuration ==================================== -if(${BUILD_UNIT_TESTS}) +if( UNITTEST ) # Include CMock build configuration. include( unit-test/cmock_build.cmake ) From e0056cc3a3d54b6d6d4d4434612e741be58ad80b Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Mon, 26 Feb 2024 07:44:47 +0000 Subject: [PATCH 3/4] Use curly brackets for operator precedence --- source/core_sntp_serializer.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/core_sntp_serializer.c b/source/core_sntp_serializer.c index a0fd94c..abda72a 100644 --- a/source/core_sntp_serializer.c +++ b/source/core_sntp_serializer.c @@ -246,9 +246,9 @@ static uint32_t readWordFromNetworkByteOrderMemory( const uint32_t * ptr ) assert( ptr != NULL ); - return ( uint32_t ) ( ( ( uint32_t ) pMemStartByte[ 0 ] << 24 ) | - ( 0x00FF0000U & ( ( uint32_t ) pMemStartByte[ 1 ] << 16 ) ) | - ( 0x0000FF00U & ( ( uint32_t ) pMemStartByte[ 2 ] << 8 ) ) | + return ( uint32_t ) ( ( ( ( uint32_t ) pMemStartByte[ 0 ] ) << 24 ) | + ( 0x00FF0000U & ( ( ( uint32_t ) pMemStartByte[ 1 ] ) << 16 ) ) | + ( 0x0000FF00U & ( ( ( uint32_t ) pMemStartByte[ 2 ] ) << 8 ) ) | ( ( uint32_t ) pMemStartByte[ 3 ] ) ); } From d58738999055a0cc364857ec587f168e582d9de3 Mon Sep 17 00:00:00 2001 From: chinglee-iot <61685396+chinglee-iot@users.noreply.github.com> Date: Tue, 27 Feb 2024 10:19:25 +0800 Subject: [PATCH 4/4] Update CMakeLists.txt for project version --- test/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index c077cd3..c410157 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,5 +1,6 @@ cmake_minimum_required( VERSION 3.13.0 ) project( "coreSNTP tests" + VERSION 1.2.0 LANGUAGES C ) # Allow the project to be organized into folders.