Skip to content

Commit

Permalink
Fix 2 MISRA violations (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
AniruddhaKanhere authored Feb 20, 2024
1 parent b92c8cd commit dc1ab91
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 70 deletions.
7 changes: 3 additions & 4 deletions source/core_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );

Expand Down Expand Up @@ -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 */
Expand Down
90 changes: 50 additions & 40 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand All @@ -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()
50 changes: 24 additions & 26 deletions tools/coverity/misra.config
Original file line number Diff line number Diff line change
@@ -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."
}
]
}

0 comments on commit dc1ab91

Please sign in to comment.