Skip to content

Commit 190d950

Browse files
chinglee-iotUbuntu
and
Ubuntu
authored
Fix MISRA C 2012 deviations (#85)
* 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. * Update CMakeList for build target * Rename UNIT_TEST to UNITTEST and COVERITY to COV_ANALYSIS to align FreeRTOS libraries * Use curly brackets for operator precedence * Update CMakeLists.txt for project version --------- Co-authored-by: Ubuntu <[email protected]>
1 parent c5face5 commit 190d950

File tree

5 files changed

+64
-48
lines changed

5 files changed

+64
-48
lines changed

.github/workflows/ci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ jobs:
6161
cmake -S test -B build/ \
6262
-G "Unix Makefiles" \
6363
-DCMAKE_BUILD_TYPE=Debug \
64-
-DBUILD_UNIT_TESTS=ON \
64+
-DUNITTEST=ON \
6565
-DCMAKE_C_FLAGS="${CFLAGS}"
6666
make -C build all -j8
6767
@@ -84,7 +84,7 @@ jobs:
8484
cmake -S test -B build/ \
8585
-G "Unix Makefiles" \
8686
-DCMAKE_BUILD_TYPE=Debug \
87-
-DBUILD_UNIT_TESTS=ON \
87+
-DUNITTEST=ON \
8888
-DCMAKE_C_FLAGS='--coverage -Wall -Wextra -Werror -DNDEBUG -Wno-error=pedantic -Wno-variadic-macros -DLOGGING_LEVEL_DEBUG=1'
8989
make -C build/ all
9090

source/core_sntp_client.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ static SntpStatus_t processServerResponse( SntpContext_t * pContext,
675675

676676
if( status == SntpSuccess )
677677
{
678-
SntpResponseData_t parsedResponse;
678+
SntpResponseData_t parsedResponse = { 0 };
679679

680680
/* De-serialize response packet to determine whether the server accepted or rejected
681681
* the request for time. Also, calculate the system clock offset if the server responded

source/core_sntp_serializer.c

+10-8
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,14 @@ typedef struct SntpPacket
221221
static void fillWordMemoryInNetworkOrder( uint32_t * pWordMemory,
222222
uint32_t data )
223223
{
224+
uint8_t * pByteMemory = ( uint8_t * ) pWordMemory;
225+
224226
assert( pWordMemory != NULL );
225227

226-
*( ( uint8_t * ) pWordMemory ) = ( uint8_t ) ( data >> 24 );
227-
*( ( uint8_t * ) pWordMemory + 1 ) = ( uint8_t ) ( ( data >> 16 ) & 0x000000FFU );
228-
*( ( uint8_t * ) pWordMemory + 2 ) = ( uint8_t ) ( ( data >> 8 ) & 0x000000FFU );
229-
*( ( uint8_t * ) pWordMemory + 3 ) = ( uint8_t ) ( ( data ) & 0x000000FFU );
228+
pByteMemory[ 0 ] = ( uint8_t ) ( data >> 24 );
229+
pByteMemory[ 1 ] = ( uint8_t ) ( ( data >> 16 ) & 0x000000FFU );
230+
pByteMemory[ 2 ] = ( uint8_t ) ( ( data >> 8 ) & 0x000000FFU );
231+
pByteMemory[ 3 ] = ( uint8_t ) ( ( data ) & 0x000000FFU );
230232
}
231233

232234
/**
@@ -244,10 +246,10 @@ static uint32_t readWordFromNetworkByteOrderMemory( const uint32_t * ptr )
244246

245247
assert( ptr != NULL );
246248

247-
return ( uint32_t ) ( ( ( uint32_t ) *( pMemStartByte ) << 24 ) |
248-
( 0x00FF0000U & ( ( uint32_t ) *( pMemStartByte + 1 ) << 16 ) ) |
249-
( 0x0000FF00U & ( ( uint32_t ) *( pMemStartByte + 2 ) << 8 ) ) |
250-
( ( uint32_t ) *( pMemStartByte + 3 ) ) );
249+
return ( uint32_t ) ( ( ( ( uint32_t ) pMemStartByte[ 0 ] ) << 24 ) |
250+
( 0x00FF0000U & ( ( ( uint32_t ) pMemStartByte[ 1 ] ) << 16 ) ) |
251+
( 0x0000FF00U & ( ( ( uint32_t ) pMemStartByte[ 2 ] ) << 8 ) ) |
252+
( ( uint32_t ) pMemStartByte[ 3 ] ) );
251253
}
252254

253255
/**

test/CMakeLists.txt

+36-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
cmake_minimum_required( VERSION 3.13.0 )
2-
project( "coreSNTP unit test"
2+
project( "coreSNTP tests"
3+
VERSION 1.2.0
34
LANGUAGES C )
45

56
# Allow the project to be organized into folders.
@@ -9,6 +10,13 @@ set_property( GLOBAL PROPERTY USE_FOLDERS ON )
910
set( CMAKE_C_STANDARD 90 )
1011
set( CMAKE_C_STANDARD_REQUIRED ON )
1112

13+
# If no configuration is defined, turn everything on.
14+
if( NOT DEFINED COV_ANALYSIS AND NOT DEFINED UNITTEST AND NOT DEFINED BUILD_CODE_EXAMPLE )
15+
set( COV_ANALYSIS ON )
16+
set( UNITTEST ON )
17+
set( BUILD_CODE_EXAMPLE ON )
18+
endif()
19+
1220
# Do not allow in-source build.
1321
if( ${PROJECT_SOURCE_DIR} STREQUAL ${PROJECT_BINARY_DIR} )
1422
message( FATAL_ERROR "In-source build is not allowed. Please build in a separate directory, such as ${PROJECT_SOURCE_DIR}/build." )
@@ -30,42 +38,51 @@ set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
3038
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib )
3139
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib )
3240

33-
# ================================ Coverity Analysis Configuration =================================
34-
3541
# Include filepaths for source and include.
3642
include( ${MODULE_ROOT_DIR}/coreSntpFilePaths.cmake )
3743

38-
# Target for Coverity analysis that builds the library.
39-
add_library( coverity_analysis
40-
${CORE_SNTP_SOURCES} )
44+
# ================================ Coverity Analysis Configuration =================================
45+
46+
if( COV_ANALYSIS )
4147

42-
# Add coreSNTP library public include path.
43-
target_include_directories( coverity_analysis
44-
PUBLIC
45-
${CORE_SNTP_INCLUDE_PUBLIC_DIRS} )
48+
# Target for Coverity analysis that builds the library.
49+
add_library( coverity_analysis
50+
${CORE_SNTP_SOURCES} )
51+
52+
# Add coreSNTP library public include path.
53+
target_include_directories( coverity_analysis
54+
PUBLIC
55+
${CORE_SNTP_INCLUDE_PUBLIC_DIRS} )
4656

47-
# Build SNTP library target without custom config dependency.
48-
target_compile_definitions( coverity_analysis PUBLIC SNTP_DO_NOT_USE_CUSTOM_CONFIG=1 )
57+
# Build SNTP library target without custom config dependency.
58+
target_compile_definitions( coverity_analysis PUBLIC SNTP_DO_NOT_USE_CUSTOM_CONFIG=1 )
4959

50-
# Build without debug enabled when performing static analysis
51-
target_compile_options(coverity_analysis PUBLIC -DNDEBUG )
60+
# Build without debug enabled when performing static analysis
61+
target_compile_options(coverity_analysis PUBLIC -DNDEBUG )
62+
endif()
5263

5364
# ==================================== Code Example Build ====================================
5465

55-
if(${BUILD_CODE_EXAMPLE})
66+
if( BUILD_CODE_EXAMPLE )
5667
# Target for Coverity analysis that builds the library.
5768
add_executable( code_example_posix
69+
${CORE_SNTP_SOURCES}
5870
${MODULE_ROOT_DIR}/docs/doxygen/code_examples/example_sntp_client_posix.c )
5971

60-
# Add coreSNTP library public include path.
61-
target_link_libraries( code_example_posix
62-
coverity_analysis )
72+
target_include_directories( code_example_posix
73+
PUBLIC
74+
${CORE_SNTP_INCLUDE_PUBLIC_DIRS} )
75+
76+
# Build SNTP library target without custom config dependency.
77+
target_compile_definitions( code_example_posix PUBLIC SNTP_DO_NOT_USE_CUSTOM_CONFIG=1 )
6378

79+
# Build without debug enabled when performing static analysis
80+
target_compile_options( code_example_posix PUBLIC -DNDEBUG )
6481
endif()
6582

6683
# ==================================== Unit Test Configuration ====================================
6784

68-
if(${BUILD_UNIT_TESTS})
85+
if( UNITTEST )
6986
# Include CMock build configuration.
7087
include( unit-test/cmock_build.cmake )
7188

tools/coverity/misra.config

+15-18
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
1-
// MISRA C-2012 Rules
2-
31
{
4-
version : "2.0",
5-
standard : "c2012",
6-
title: "Coverity MISRA Configuration",
7-
deviations : [
8-
// Disable the following rules.
2+
"version" : "2.0",
3+
"standard" : "c2012",
4+
"title" : "Coverity MISRA Configuration",
5+
"deviations" : [
96
{
10-
deviation: "Directive 4.9",
11-
reason: "Allow inclusion of function like macros. Asserts and logging are done using function like macros."
7+
"deviation": "Directive 4.9",
8+
"reason": "Allow inclusion of function like macros. Asserts and logging are done using function like macros."
129
},
1310
{
14-
deviation: "Rule 2.4",
15-
reason: "Allow unused tags. Some compilers warn if types are not tagged."
11+
"deviation": "Rule 2.4",
12+
"reason": "Allow unused tags. Some compilers warn if types are not tagged."
1613
},
1714
{
18-
deviation: "Rule 2.5",
19-
reason: "Allow unused macros. coreSNTP Library headers define macros intended for the application's use, but are not used by the agent."
15+
"deviation": "Rule 2.5",
16+
"reason": "Allow unused macros. coreSNTP Library headers define macros intended for the application's use, but are not used by the agent."
2017
},
2118
{
22-
deviation: "Rule 3.1",
23-
reason: "Allow nested comments. Documentation blocks contain comments for example code."
19+
"deviation": "Rule 3.1",
20+
"reason": "Allow nested comments. Documentation blocks contain comments for example code."
2421
},
2522
{
26-
deviation: "Rule 8.7",
27-
reason: "API functions are not used by library. They must be externally visible in order to be used by the application."
28-
},
23+
"deviation": "Rule 8.7",
24+
"reason": "API functions are not used by library. They must be externally visible in order to be used by the application."
25+
}
2926
]
3027
}

0 commit comments

Comments
 (0)