From b990dbb87160a38b5e6ca9cda758ed580dd1d9f9 Mon Sep 17 00:00:00 2001 From: Aniruddha Kanhere <60444055+AniruddhaKanhere@users.noreply.github.com> Date: Fri, 23 Feb 2024 05:53:00 +0000 Subject: [PATCH 1/2] Fix MISRA violations --- .github/workflows/ci.yml | 1 + source/fleet_provisioning.c | 6 +-- test/CMakeLists.txt | 90 ++++++++++++++++++++----------------- tools/coverity/README.md | 6 +-- tools/coverity/misra.config | 28 ++++++------ 5 files changed, 70 insertions(+), 61 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 370a67a..ec109fc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,6 +18,7 @@ jobs: -G "Unix Makefiles" \ -DCMAKE_BUILD_TYPE=Debug \ -DBUILD_CLONE_SUBMODULES=ON \ + -DUNITTEST=1 \ -DCMAKE_C_FLAGS='--coverage -Wall -Wextra -Werror -DNDEBUG' make -C build/ all - name: Test diff --git a/source/fleet_provisioning.c b/source/fleet_provisioning.c index 9b80647..d0e568e 100644 --- a/source/fleet_provisioning.c +++ b/source/fleet_provisioning.c @@ -272,7 +272,7 @@ static void writeTopicFragmentAndAdvance( char ** pBufferCursor, ( const void * ) fragment, ( size_t ) length ); - *pBufferCursor += length; + *pBufferCursor = &( ( *pBufferCursor )[ length ] ); } /*-----------------------------------------------------------*/ @@ -597,7 +597,7 @@ static FleetProvisioningStatus_t consumeIfMatch( const char ** pBufferCursor, else { status = FleetProvisioningSuccess; - *pBufferCursor += matchLength; + *pBufferCursor = &( ( *pBufferCursor )[ matchLength ] ); *pRemainingLength -= matchLength; } } @@ -629,7 +629,7 @@ static FleetProvisioningStatus_t consumeTemplateName( const char ** pTopicCursor if( i > 0U ) { ret = FleetProvisioningSuccess; - *pTopicCursor += i; + *pTopicCursor = &( ( *pTopicCursor )[ i ] ); *pRemainingLength -= i; } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 164254d..7436f0e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,6 +1,6 @@ -cmake_minimum_required ( VERSION 3.13.0 ) +cmake_minimum_required ( VERSION 3.22.0 ) project ( "Fleet Provisioning unit test" - VERSION 1.0.0 + VERSION 1.1.0 LANGUAGES C ) # Allow the project to be organized into folders. @@ -10,6 +10,12 @@ 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 ) + set( COV_ANALYSIS TRUE ) + set( UNITTEST TRUE ) +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." ) @@ -33,55 +39,59 @@ set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib ) # ====================== Coverity Analysis Configuration ====================== -# Include filepaths for source and include. -include( ${MODULE_ROOT_DIR}/fleetprovisioningFilePaths.cmake ) +if( COV_ANALYSIS ) + # Include filepaths for source and include. + include( ${MODULE_ROOT_DIR}/fleetprovisioningFilePaths.cmake ) -# Target for Coverity analysis that builds the library. -add_library( coverity_analysis - ${FLEET_PROVISIONING_SOURCES} ) + # Target for Coverity analysis that builds the library. + add_library( coverity_analysis + ${FLEET_PROVISIONING_SOURCES} ) -# Fleet Provisioning public include path and test config file. -target_include_directories( coverity_analysis PUBLIC - ${FLEET_PROVISIONING_INCLUDE_PUBLIC_DIRS} - "${CMAKE_CURRENT_LIST_DIR}/include" ) + # Fleet Provisioning public include path and test config file. + target_include_directories( coverity_analysis PUBLIC + ${FLEET_PROVISIONING_INCLUDE_PUBLIC_DIRS} + "${CMAKE_CURRENT_LIST_DIR}/include" ) -# Build without debug enabled when performing static analysis -target_compile_options(coverity_analysis PUBLIC -DNDEBUG -DDISABLE_LOGGING) + # Build without debug enabled when performing static analysis + target_compile_options(coverity_analysis PUBLIC -DNDEBUG -DDISABLE_LOGGING) +endif() # ============================ Test Configuration ============================ -# Include Unity build configuration. -include( unit-test/unity_build.cmake ) - -# Check if the Unity source directory exists. If it does not exist and the -# BUILD_CLONE_SUBMODULES configuration is enabled, clone the Unity submodule. -if( NOT EXISTS ${UNITY_DIR}/src ) - # Attempt to clone Unity. - if( ${BUILD_CLONE_SUBMODULES} ) - clone_unity() - else() - message( FATAL_ERROR "The required submodule Unity does not exist. Either clone it manually, or set BUILD_CLONE_SUBMODULES to 1 to automatically clone it during build." ) +if( UNITTEST ) + # Include Unity build configuration. + include( unit-test/unity_build.cmake ) + + # Check if the Unity source directory exists. If it does not exist and the + # BUILD_CLONE_SUBMODULES configuration is enabled, clone the Unity submodule. + if( NOT EXISTS ${UNITY_DIR}/src ) + # Attempt to clone Unity. + if( ${BUILD_CLONE_SUBMODULES} ) + clone_unity() + else() + message( FATAL_ERROR "The required submodule Unity does not exist. Either clone it manually, or set BUILD_CLONE_SUBMODULES to 1 to automatically clone it during build." ) + endif() endif() -endif() -# Use CTest utility for managing test runs. -enable_testing() + # Use CTest utility for managing test runs. + enable_testing() -# Add build target for Unity, required for unit testing. -add_unity_target() + # Add build target for Unity, required for unit testing. + add_unity_target() -# Add functions to enable Unity based tests and coverage. -include( ${MODULE_ROOT_DIR}/tools/unity/create_test.cmake ) + # Add functions to enable Unity based tests and coverage. + include( ${MODULE_ROOT_DIR}/tools/unity/create_test.cmake ) -# Include build configuration for unit tests. -add_subdirectory( unit-test ) + # Include build configuration for unit tests. + add_subdirectory( unit-test ) -# ====================== Coverage Analysis configuration ====================== + # ====================== Coverage Analysis configuration ====================== -# Add a target for running coverage on tests. -add_custom_target( coverage - COMMAND ${CMAKE_COMMAND} -DUNITY_DIR=${UNITY_DIR} - -P ${MODULE_ROOT_DIR}/tools/unity/coverage.cmake - DEPENDS unity fleet_provisioning_utest - WORKING_DIRECTORY ${CMAKE_BINARY_DIR} ) + # Add a target for running coverage on tests. + add_custom_target( coverage + COMMAND ${CMAKE_COMMAND} -DUNITY_DIR=${UNITY_DIR} + -P ${MODULE_ROOT_DIR}/tools/unity/coverage.cmake + DEPENDS unity fleet_provisioning_utest + WORKING_DIRECTORY ${CMAKE_BINARY_DIR} ) +endif() diff --git a/tools/coverity/README.md b/tools/coverity/README.md index 7a17334..727bdee 100644 --- a/tools/coverity/README.md +++ b/tools/coverity/README.md @@ -5,7 +5,7 @@ To that end, this directory provides a [configuration file](https://github.com/a building a binary for the tool to analyze. > **Note** -For generating the report as outlined below, we have used Coverity version 2018.09. +For generating the report as outlined below, we have used Coverity version 2023.6.1. For details regarding the suppressed violations in the report (which can be generated using the instructions described below), please see the [MISRA.md](https://github.com/aws/Fleet-Provisioning-for-AWS-IoT-embedded-sdk/blob/main/MISRA.md) file. @@ -31,7 +31,7 @@ Go to the root directory of the library and run the following commands in termin ~~~ 2. Create the build files using CMake in a `build` directory ~~~ - cmake -B build -S test + cmake -B build -S test -DCOV_ANALYSIS=1 ~~~ 3. Go to the build directory and copy the coverity configuration file ~~~ @@ -62,7 +62,7 @@ Go to the root directory of the library and run the following commands in termin For your convenience the commands above are below to be copy/pasted into a UNIX command friendly terminal. ~~~ cov-configure --force --compiler cc --comptype gcc; - cmake -B build -S test; + cmake -B build -S test -DCOV_ANALYSIS=1; cd build/; cov-build --emit-complementary-info --dir cov-out make coverity_analysis; cd cov-out/ diff --git a/tools/coverity/misra.config b/tools/coverity/misra.config index fdf69bf..7aa6f8f 100644 --- a/tools/coverity/misra.config +++ b/tools/coverity/misra.config @@ -1,25 +1,23 @@ -// 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, logging, and topic string macros use function like macros." + "deviation": "Directive 4.9", + "reason": "Allow inclusion of function like macros. Asserts, logging, and topic string macros use function like macros." }, { - deviation: "Rule 2.5", - reason: "Allow unused macros. Macros defined for topic strings are not used by the library, but are part of the API." + "deviation": "Rule 2.5", + "reason": "Allow unused macros. Macros defined for topic strings are not used by the library, but are part of the API." }, { - deviation: "Rule 3.1", - reason: "Allow nested comments. C++ style `//` comments are used in example code within Doxygen documentation blocks." + "deviation": "Rule 3.1", + "reason": "Allow nested comments. C++ style `//` comments are used in example code within Doxygen documentation blocks." }, { - 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 ccbffa0355cfee92703582a572d625c5d841591b Mon Sep 17 00:00:00 2001 From: Aniruddha Kanhere <60444055+AniruddhaKanhere@users.noreply.github.com> Date: Fri, 23 Feb 2024 05:59:43 +0000 Subject: [PATCH 2/2] Fix spell check --- .github/.cSpellWords.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/.cSpellWords.txt b/.github/.cSpellWords.txt index 8be59db..d13638d 100644 --- a/.github/.cSpellWords.txt +++ b/.github/.cSpellWords.txt @@ -12,6 +12,7 @@ Coverity CSDK ctest DCMOCK +DCOV DDisable decihours Decihours