From fdfe5194b4a024eb858f8fc6a9908ca9c5751159 Mon Sep 17 00:00:00 2001 From: Aniruddha Kanhere <60444055+AniruddhaKanhere@users.noreply.github.com> Date: Tue, 20 Feb 2024 00:01:15 +0000 Subject: [PATCH] Fix 2 MISRA violations --- source/core_json.c | 7 ++- test/CMakeLists.txt | 90 ++++++++++++++++++++----------------- tools/coverity/misra.config | 50 ++++++++++----------- 3 files changed, 77 insertions(+), 70 deletions(-) diff --git a/source/core_json.c b/source/core_json.c index 901b2e16..d7513317 100644 --- a/source/core_json.c +++ b/source/core_json.c @@ -688,7 +688,7 @@ static bool skipDigits( const char * buf, { bool ret = false; size_t i = 0U, saveStart = 0U; - int32_t value = 0U; + int32_t value = 0; assert( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) ); @@ -1711,11 +1711,10 @@ JSONStatus_t JSON_SearchT( char * buf, size_t * outValueLength, JSONTypes_t * outType ) { - /* MISRA Ref 11.3.1 [Misaligned access] */ + /* MISRA Ref 11.3.1 [Pointer conversion] */ /* More details at: https://github.com/FreeRTOS/coreJSON/blob/main/MISRA.md#rule-113 */ /* coverity[misra_c_2012_rule_11_3_violation] */ - return JSON_SearchConst( ( const char * ) buf, max, query, queryLength, - ( const char ** ) outValue, outValueLength, outType ); + return JSON_SearchConst( ( const char * ) buf, max, query, queryLength, ( const char ** ) outValue, outValueLength, outType ); } /** @cond DO_NOT_DOCUMENT */ diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 80df584a..d0ac78a2 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -21,6 +21,12 @@ set( MODULE_ROOT_DIR ${__MODULE_ROOT_DIR} CACHE INTERNAL "coreJSON source root." set( UNIT_TEST_DIR ${MODULE_ROOT_DIR}/test/unit-test CACHE INTERNAL "coreJSON unit test directory." ) set( UNITY_DIR ${UNIT_TEST_DIR}/Unity CACHE INTERNAL "Unity library source directory." ) +# 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() + # Configure options to always show in CMake GUI. option( BUILD_CLONE_SUBMODULES "Set this to ON to automatically clone any required Git submodules. When OFF, submodules must be manually cloned." @@ -33,54 +39,58 @@ set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib ) # ================================ Coverity Analysis Configuration ================================= -# Include filepaths for source and include. -include( ${MODULE_ROOT_DIR}/jsonFilePaths.cmake ) -# Target for Coverity analysis that builds the library. -add_library( coverity_analysis - ${JSON_SOURCES} ) -# JSON public include path. -target_include_directories( coverity_analysis PUBLIC ${JSON_INCLUDE_PUBLIC_DIRS} ) - -# When building the coverity analysis target we disable debug -target_compile_options(coverity_analysis PUBLIC -DNDEBUG ) +if( COV_ANALYSIS ) + # Include filepaths for source and include. + include( ${MODULE_ROOT_DIR}/jsonFilePaths.cmake ) + # Target for Coverity analysis that builds the library. + add_library( coverity_analysis + ${JSON_SOURCES} ) + # JSON public include path. + target_include_directories( coverity_analysis PUBLIC ${JSON_INCLUDE_PUBLIC_DIRS} ) + + # When building the coverity analysis target we disable debug + target_compile_options(coverity_analysis PUBLIC -DNDEBUG ) +endif() # ==================================== Test Configuration ======================================== -# Include Unity build configuration. -include( unit-test/unity_build.cmake ) - -# Check if the Unity source directory exists, and if not present, clone the submodule -# if BUILD_CLONE_SUBMODULES configuration is enabled. -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, and if not present, clone the submodule + # if BUILD_CLONE_SUBMODULES configuration is enabled. + 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() -# Add unit test and coverage configuration. + # Add unit test and coverage configuration. -# Use CTest utility for managing test runs. This has to be added BEFORE -# defining test targets with add_test() -enable_testing() + # Use CTest utility for managing test runs. This has to be added BEFORE + # defining test targets with add_test() + enable_testing() -# Add build targets for Unity and Unit, required for unit testing. -add_unity_targets() + # Add build targets for Unity and Unit, required for unit testing. + add_unity_targets() -# Add function to enable Unity based tests and coverage. -include( ${MODULE_ROOT_DIR}/tools/unity/create_test.cmake ) + # Add function 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 core_json_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 core_json_utest + WORKING_DIRECTORY ${CMAKE_BINARY_DIR} + ) +endif() diff --git a/tools/coverity/misra.config b/tools/coverity/misra.config index c4f53306..11d88d65 100644 --- a/tools/coverity/misra.config +++ b/tools/coverity/misra.config @@ -1,42 +1,40 @@ -// MISRA C-2012 Rules - { - version : "2.0", - standard : "c2012", - title: "Coverity MISRA Configuration", - deviations : [ + "version" : "2.0", + "standard" : "c2012", + "title": "Coverity MISRA Configuration", + "deviations" : [ { - deviation: "Directive 4.9", - category: "Advisory", - reason: "Allow inclusion of function like macros." + "deviation": "Directive 4.9", + "category": "Advisory", + "reason": "Allow inclusion of function like macros." }, { - deviation: "Rule 2.5", - reason: "Allow unused macros. Library headers may define macros intended for the application's use, but not used by a specific file." + "deviation": "Rule 2.5", + "reason": "Allow unused macros. Library headers may define macros intended for the application's use, but not used by a specific file." }, { - deviation: "Rule 3.1", - category: "Required", - reason: "Allow nested comments. Documentation blocks contain comments for example code." + "deviation": "Rule 3.1", + "category": "Required", + "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." }, { - deviation: "Rule 8.13", - category: "Advisory", - reason: "Allow one function to have a char * argument without const qualifier." + "deviation": "Rule 8.13", + "category": "Advisory", + "reason": "Allow one function to have a char * argument without const qualifier." }, { - deviation: "Rule 15.4", - category: "Advisory", - reason: "Allow more then one break statement to terminate a loop" + "deviation": "Rule 15.4", + "category": "Advisory", + "reason": "Allow more then one break statement to terminate a loop" }, { - deviation: "Rule 19.2", - category: "Advisory", - reason: "Allow a union of a signed and unsigned type of identical sizes." - }, + "deviation": "Rule 19.2", + "category": "Advisory", + "reason": "Allow a union of a signed and unsigned type of identical sizes." + } ] }