Skip to content

Commit 8eba41c

Browse files
Fix misra violations
Update CMake to optionally compile for coverity Update coverity config
1 parent f1c3bd6 commit 8eba41c

File tree

5 files changed

+102
-97
lines changed

5 files changed

+102
-97
lines changed

MISRA.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# MISRA Compliance
22

33
The jobs library files conform to the [MISRA C:2012](https://www.misra.org.uk)
4-
guidelines, with some noted exceptions. Compliance is checked with Coverity static analysis.
5-
The specific deviations, suppressed inline, are listed below.
4+
guidelines, with some noted exceptions. Compliance is checked with Coverity static analysis
5+
version 2023.6.1. The specific deviations, suppressed inline, are listed below.
66

77
Additionally, [MISRA configuration file](https://github.com/aws/Jobs-for-AWS-IoT-embedded-sdk/blob/main/tools/coverity/misra.config) contains the project wide deviations.
88

source/jobs.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,11 @@
4141
/**
4242
* @brief Get the length of a string literal.
4343
*/
44-
#ifdef CONST_STRLEN
45-
#undef CONST_STRLEN
46-
#endif
4744
#define CONST_STRLEN( x ) ( sizeof( ( x ) ) - 1U )
4845

4946
/**
5047
* @brief Get the length on an array.
5148
*/
52-
#ifdef ARRAY_LENGTH
53-
#undef ARRAY_LENGTH
54-
#endif
5549
#define ARRAY_LENGTH( x ) ( sizeof( ( x ) ) / sizeof( ( x )[ 0 ] ) )
5650

5751
/**

source/otaJobParser/job_parser.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,8 @@ static void buildIndexedFileQueryString( int32_t fileIndex,
385385
( void ) strncpy( result, ( const char * ) "afr_ota.files[", 15U );
386386
int32_t index = ( fileIndex + ( int32_t ) '0' );
387387
result[ 14 ] = ( char ) index;
388-
( void ) strncpy( ( result + 15 ), ( const char * ) "].", 3U );
389-
( void ) memcpy( ( result + 17 ), queryString, queryStringLength );
388+
( void ) strncpy( &result[ 15 ], ( const char * ) "].", 3U );
389+
( void ) memcpy( &result[ 17 ], queryString, queryStringLength );
390390

391391
*resultLength = 17U + queryStringLength;
392392
}

test/CMakeLists.txt

Lines changed: 80 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.16.0)
1+
cmake_minimum_required(VERSION 3.22.0)
22

33
project(
44
"AWS IoT Jobs Tests"
@@ -15,6 +15,12 @@ if(NOT DEFINED CMAKE_C_STANDARD_REQUIRED)
1515
set(CMAKE_C_STANDARD_REQUIRED ON)
1616
endif()
1717

18+
# If no configuration is defined, turn everything on.
19+
if( NOT DEFINED COV_ANALYSIS AND NOT DEFINED UNITTEST )
20+
set( COV_ANALYSIS TRUE )
21+
set( UNITTEST TRUE )
22+
endif()
23+
1824
# Do not allow in-source build.
1925
if(${PROJECT_SOURCE_DIR} STREQUAL ${PROJECT_BINARY_DIR})
2026
message(
@@ -30,23 +36,6 @@ set(MODULE_ROOT_DIR
3036
${__MODULE_ROOT_DIR}
3137
CACHE INTERNAL "Jobs repository root.")
3238

33-
# ================================ Coverity Analysis Configuration =================================
34-
35-
# Include filepaths for source and include.
36-
include( ${MODULE_ROOT_DIR}/jobsFilePaths.cmake )
37-
# Target for Coverity analysis that builds the library.
38-
add_library( coverity_analysis
39-
${JOBS_SOURCES}
40-
${OTA_HANDLER_SOURCES} )
41-
# JOBS public include path.
42-
target_include_directories( coverity_analysis PUBLIC ${JOBS_INCLUDE_PUBLIC_DIRS}
43-
${OTA_HANDLER_INCLUDES} )
44-
45-
# ==================================================================================================
46-
47-
# Build HTTP library target without logging
48-
target_compile_options(coverity_analysis PUBLIC -DNDEBUG )
49-
5039
include(FetchContent)
5140

5241
FetchContent_Declare(
@@ -58,65 +47,89 @@ FetchContent_Declare(
5847
CoreJSON
5948
# hash: sha256-r0lJff61NK2rPtO7Wr6RudFNQiLt1D4M30V7/p60Zi0=
6049
GIT_REPOSITORY https://github.com/FreeRTOS/coreJSON.git
61-
GIT_TAG a0cd6122745a879225bf459dd257e79bdd63d37a)
50+
GIT_TAG dc1ab9130a1fb99b801a2a1fa8e9f42239f752be)
6251

6352
FetchContent_MakeAvailable(CMock CoreJSON)
6453

65-
add_library(
66-
unity STATIC
67-
"${cmock_SOURCE_DIR}/vendor/unity/src/unity.c"
68-
"${cmock_SOURCE_DIR}/vendor/unity/extras/fixture/src/unity_fixture.c"
69-
"${cmock_SOURCE_DIR}/vendor/unity/extras/memory/src/unity_memory.c")
70-
target_include_directories(
71-
unity
72-
PUBLIC "${cmock_SOURCE_DIR}/vendor/unity/src"
73-
"${cmock_SOURCE_DIR}/vendor/unity/extras/fixture/src"
74-
"${cmock_SOURCE_DIR}/vendor/unity/extras/memory/src"
75-
"${cmock_SOURCE_DIR}/src")
76-
77-
set_target_properties(
78-
unity PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
79-
POSITION_INDEPENDENT_CODE ON)
80-
81-
add_library(cmock STATIC)
82-
target_sources(cmock PRIVATE ${cmock_SOURCE_DIR}/src/cmock.c)
83-
target_include_directories(
84-
cmock
85-
PUBLIC "${cmock_SOURCE_DIR}/src"
86-
"${cmock_SOURCE_DIR}/vendor/unity/src/"
87-
"${cmock_SOURCE_DIR}/examples"
88-
"${cmock_SOURCE_DIR}/vendor/unity/extras/fixture/src"
89-
"${cmock_SOURCE_DIR}/vendor/unity/extras/memory/src")
90-
set_target_properties(
91-
cmock
92-
PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
93-
POSITION_INDEPENDENT_CODE ON
94-
COMPILE_FLAGS "-Og")
95-
96-
# coreJSON
54+
# Add coreJSON library
9755
include("${corejson_SOURCE_DIR}/jsonFilePaths.cmake")
9856
add_library(coreJSON ${JSON_SOURCES})
9957
target_include_directories(coreJSON PUBLIC ${JSON_INCLUDE_PUBLIC_DIRS})
100-
target_link_libraries(coverity_analysis PUBLIC coreJSON)
58+
59+
# ================================ Coverity Analysis Configuration =================================
60+
61+
if( COV_ANALYSIS )
62+
# Include filepaths for source and include.
63+
include( ${MODULE_ROOT_DIR}/jobsFilePaths.cmake )
64+
# Target for Coverity analysis that builds the library.
65+
add_library( coverity_analysis
66+
${JOBS_SOURCES}
67+
${OTA_HANDLER_SOURCES} )
68+
# JOBS public include path.
69+
target_include_directories( coverity_analysis PUBLIC ${JOBS_INCLUDE_PUBLIC_DIRS}
70+
${OTA_HANDLER_INCLUDES} )
71+
72+
target_link_libraries(coverity_analysis PUBLIC coreJSON)
73+
74+
# Build HTTP library target without logging
75+
target_compile_options(coverity_analysis PUBLIC -DNDEBUG )
76+
77+
# Remove inclusion of assert.
78+
add_compile_definitions( NDEBUG=1 )
79+
endif()
80+
81+
# ==================================================================================================
82+
if( UNITTEST )
83+
add_library(
84+
unity STATIC
85+
"${cmock_SOURCE_DIR}/vendor/unity/src/unity.c"
86+
"${cmock_SOURCE_DIR}/vendor/unity/extras/fixture/src/unity_fixture.c"
87+
"${cmock_SOURCE_DIR}/vendor/unity/extras/memory/src/unity_memory.c")
88+
target_include_directories(
89+
unity
90+
PUBLIC "${cmock_SOURCE_DIR}/vendor/unity/src"
91+
"${cmock_SOURCE_DIR}/vendor/unity/extras/fixture/src"
92+
"${cmock_SOURCE_DIR}/vendor/unity/extras/memory/src"
93+
"${cmock_SOURCE_DIR}/src")
94+
95+
set_target_properties(
96+
unity PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
97+
POSITION_INDEPENDENT_CODE ON)
98+
99+
add_library(cmock STATIC)
100+
target_sources(cmock PRIVATE ${cmock_SOURCE_DIR}/src/cmock.c)
101+
target_include_directories(
102+
cmock
103+
PUBLIC "${cmock_SOURCE_DIR}/src"
104+
"${cmock_SOURCE_DIR}/vendor/unity/src/"
105+
"${cmock_SOURCE_DIR}/examples"
106+
"${cmock_SOURCE_DIR}/vendor/unity/extras/fixture/src"
107+
"${cmock_SOURCE_DIR}/vendor/unity/extras/memory/src")
108+
set_target_properties(
109+
cmock
110+
PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
111+
POSITION_INDEPENDENT_CODE ON
112+
COMPILE_FLAGS "-Og")
101113

102114
# ==================================== Test Configuration ========================================
103115

104-
# Use CTest utility for managing test runs. This has to be added BEFORE defining
105-
# test targets with add_test()
106-
enable_testing()
116+
# Use CTest utility for managing test runs. This has to be added BEFORE defining
117+
# test targets with add_test()
118+
enable_testing()
107119

108-
# Add function to enable CMock based tests and coverage.
109-
include(${MODULE_ROOT_DIR}/tools/cmock/create_test.cmake)
120+
# Add function to enable CMock based tests and coverage.
121+
include(${MODULE_ROOT_DIR}/tools/cmock/create_test.cmake)
110122

111-
# Include build configuration for unit tests.
112-
add_subdirectory(unit-test)
123+
# Include build configuration for unit tests.
124+
add_subdirectory(unit-test)
113125

114-
# ==================================== Coverage Analysis configuration ========================================
126+
# ==================================== Coverage Analysis configuration ========================================
115127

116-
# Add a target for running coverage on tests.
117-
add_custom_target(
118-
coverage
119-
COMMAND ${CMAKE_COMMAND} -DCMOCK_DIR=${cmock_SOURCE_DIR} -P
120-
${MODULE_ROOT_DIR}/tools/cmock/coverage.cmake
121-
DEPENDS cmock unity jobs_utest ota_job_handler_utest job_parser_utest
122-
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
128+
# Add a target for running coverage on tests.
129+
add_custom_target(
130+
coverage
131+
COMMAND ${CMAKE_COMMAND} -DCMOCK_DIR=${cmock_SOURCE_DIR} -P
132+
${MODULE_ROOT_DIR}/tools/cmock/coverage.cmake
133+
DEPENDS cmock unity jobs_utest ota_job_handler_utest job_parser_utest
134+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
135+
endif()

tools/coverity/misra.config

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
1-
// MISRA C-2012 Rules
2-
31
{
4-
version : "2.0",
5-
standard : "c2012",
6-
title: "Coverity MISRA Configuration",
7-
deviations : [
2+
"version" : "2.0",
3+
"standard" : "c2012",
4+
"title": "Coverity MISRA Configuration",
5+
"deviations" : [
86
{
9-
deviation: "Directive 4.8",
10-
category: "Advisory",
11-
reason: "AfrOtaJobDocumentFields_t struct must be externally visible in able to be used by the application."
7+
"deviation": "Directive 4.8",
8+
"category": "Advisory",
9+
"reason": "AfrOtaJobDocumentFields_t struct must be externally visible in able to be used by the application."
1210
},
1311
{
14-
deviation: "Directive 4.9",
15-
category: "Advisory",
16-
reason: "Allow inclusion of function like macros."
12+
"deviation": "Directive 4.9",
13+
"category": "Advisory",
14+
"reason": "Allow inclusion of function like macros."
1715
},
1816
{
19-
deviation: "Rule 2.5",
20-
category: "Advisory",
21-
reason: "Allow unused macros. Library headers may define macros intended for the application's use, but not used by a specific file."
17+
"deviation": "Rule 2.5",
18+
"category": "Advisory",
19+
"reason": "Allow unused macros. Library headers may define macros intended for the application's use, but not used by a specific file."
2220
},
2321
{
24-
deviation: "Rule 3.1",
25-
category: "Required",
26-
reason: "Allow nested comments. Documentation blocks contain comments for example code."
22+
"deviation": "Rule 3.1",
23+
"category": "Required",
24+
"reason": "Allow nested comments. Documentation blocks contain comments for example code."
2725
},
2826
{
29-
deviation: "Rule 8.7",
30-
reason: "API functions are not used by library. They must be externally visible in order to be used by the application."
27+
"deviation": "Rule 8.7",
28+
"reason": "API functions are not used by library. They must be externally visible in order to be used by the application."
3129
}
3230
]
3331
}

0 commit comments

Comments
 (0)