Skip to content

Commit

Permalink
Fix MISRA C 2012 deviations (#85)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
chinglee-iot and Ubuntu authored Feb 29, 2024
1 parent c5face5 commit 190d950
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 48 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
cmake -S test -B build/ \
-G "Unix Makefiles" \
-DCMAKE_BUILD_TYPE=Debug \
-DBUILD_UNIT_TESTS=ON \
-DUNITTEST=ON \
-DCMAKE_C_FLAGS="${CFLAGS}"
make -C build all -j8
Expand All @@ -84,7 +84,7 @@ jobs:
cmake -S test -B build/ \
-G "Unix Makefiles" \
-DCMAKE_BUILD_TYPE=Debug \
-DBUILD_UNIT_TESTS=ON \
-DUNITTEST=ON \
-DCMAKE_C_FLAGS='--coverage -Wall -Wextra -Werror -DNDEBUG -Wno-error=pedantic -Wno-variadic-macros -DLOGGING_LEVEL_DEBUG=1'
make -C build/ all
Expand Down
2 changes: 1 addition & 1 deletion source/core_sntp_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ static SntpStatus_t processServerResponse( SntpContext_t * pContext,

if( status == SntpSuccess )
{
SntpResponseData_t parsedResponse;
SntpResponseData_t parsedResponse = { 0 };

/* De-serialize response packet to determine whether the server accepted or rejected
* the request for time. Also, calculate the system clock offset if the server responded
Expand Down
18 changes: 10 additions & 8 deletions source/core_sntp_serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,14 @@ typedef struct SntpPacket
static void fillWordMemoryInNetworkOrder( uint32_t * pWordMemory,
uint32_t data )
{
uint8_t * pByteMemory = ( uint8_t * ) pWordMemory;

assert( pWordMemory != NULL );

*( ( uint8_t * ) pWordMemory ) = ( uint8_t ) ( data >> 24 );
*( ( uint8_t * ) pWordMemory + 1 ) = ( uint8_t ) ( ( data >> 16 ) & 0x000000FFU );
*( ( uint8_t * ) pWordMemory + 2 ) = ( uint8_t ) ( ( data >> 8 ) & 0x000000FFU );
*( ( uint8_t * ) pWordMemory + 3 ) = ( uint8_t ) ( ( data ) & 0x000000FFU );
pByteMemory[ 0 ] = ( uint8_t ) ( data >> 24 );
pByteMemory[ 1 ] = ( uint8_t ) ( ( data >> 16 ) & 0x000000FFU );
pByteMemory[ 2 ] = ( uint8_t ) ( ( data >> 8 ) & 0x000000FFU );
pByteMemory[ 3 ] = ( uint8_t ) ( ( data ) & 0x000000FFU );
}

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

assert( ptr != NULL );

return ( uint32_t ) ( ( ( uint32_t ) *( pMemStartByte ) << 24 ) |
( 0x00FF0000U & ( ( uint32_t ) *( pMemStartByte + 1 ) << 16 ) ) |
( 0x0000FF00U & ( ( uint32_t ) *( pMemStartByte + 2 ) << 8 ) ) |
( ( uint32_t ) *( pMemStartByte + 3 ) ) );
return ( uint32_t ) ( ( ( ( uint32_t ) pMemStartByte[ 0 ] ) << 24 ) |
( 0x00FF0000U & ( ( ( uint32_t ) pMemStartByte[ 1 ] ) << 16 ) ) |
( 0x0000FF00U & ( ( ( uint32_t ) pMemStartByte[ 2 ] ) << 8 ) ) |
( ( uint32_t ) pMemStartByte[ 3 ] ) );
}

/**
Expand Down
55 changes: 36 additions & 19 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
cmake_minimum_required( VERSION 3.13.0 )
project( "coreSNTP unit test"
project( "coreSNTP tests"
VERSION 1.2.0
LANGUAGES C )

# Allow the project to be organized into folders.
Expand All @@ -9,6 +10,13 @@ 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 AND NOT DEFINED BUILD_CODE_EXAMPLE )
set( COV_ANALYSIS ON )
set( UNITTEST ON )
set( BUILD_CODE_EXAMPLE ON )
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." )
Expand All @@ -30,42 +38,51 @@ set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib )

# ================================ Coverity Analysis Configuration =================================

# Include filepaths for source and include.
include( ${MODULE_ROOT_DIR}/coreSntpFilePaths.cmake )

# Target for Coverity analysis that builds the library.
add_library( coverity_analysis
${CORE_SNTP_SOURCES} )
# ================================ Coverity Analysis Configuration =================================

if( COV_ANALYSIS )

# Add coreSNTP library public include path.
target_include_directories( coverity_analysis
PUBLIC
${CORE_SNTP_INCLUDE_PUBLIC_DIRS} )
# Target for Coverity analysis that builds the library.
add_library( coverity_analysis
${CORE_SNTP_SOURCES} )

# Add coreSNTP library public include path.
target_include_directories( coverity_analysis
PUBLIC
${CORE_SNTP_INCLUDE_PUBLIC_DIRS} )

# Build SNTP library target without custom config dependency.
target_compile_definitions( coverity_analysis PUBLIC SNTP_DO_NOT_USE_CUSTOM_CONFIG=1 )
# Build SNTP library target without custom config dependency.
target_compile_definitions( coverity_analysis PUBLIC SNTP_DO_NOT_USE_CUSTOM_CONFIG=1 )

# Build without debug enabled when performing static analysis
target_compile_options(coverity_analysis PUBLIC -DNDEBUG )
# Build without debug enabled when performing static analysis
target_compile_options(coverity_analysis PUBLIC -DNDEBUG )
endif()

# ==================================== Code Example Build ====================================

if(${BUILD_CODE_EXAMPLE})
if( BUILD_CODE_EXAMPLE )
# Target for Coverity analysis that builds the library.
add_executable( code_example_posix
${CORE_SNTP_SOURCES}
${MODULE_ROOT_DIR}/docs/doxygen/code_examples/example_sntp_client_posix.c )

# Add coreSNTP library public include path.
target_link_libraries( code_example_posix
coverity_analysis )
target_include_directories( code_example_posix
PUBLIC
${CORE_SNTP_INCLUDE_PUBLIC_DIRS} )

# Build SNTP library target without custom config dependency.
target_compile_definitions( code_example_posix PUBLIC SNTP_DO_NOT_USE_CUSTOM_CONFIG=1 )

# Build without debug enabled when performing static analysis
target_compile_options( code_example_posix PUBLIC -DNDEBUG )
endif()

# ==================================== Unit Test Configuration ====================================

if(${BUILD_UNIT_TESTS})
if( UNITTEST )
# Include CMock build configuration.
include( unit-test/cmock_build.cmake )

Expand Down
33 changes: 15 additions & 18 deletions tools/coverity/misra.config
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
// 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 and logging are done using function like macros."
"deviation": "Directive 4.9",
"reason": "Allow inclusion of function like macros. Asserts and logging are done using function like macros."
},
{
deviation: "Rule 2.4",
reason: "Allow unused tags. Some compilers warn if types are not tagged."
"deviation": "Rule 2.4",
"reason": "Allow unused tags. Some compilers warn if types are not tagged."
},
{
deviation: "Rule 2.5",
reason: "Allow unused macros. coreSNTP Library headers define macros intended for the application's use, but are not used by the agent."
"deviation": "Rule 2.5",
"reason": "Allow unused macros. coreSNTP Library headers define macros intended for the application's use, but are not used by the agent."
},
{
deviation: "Rule 3.1",
reason: "Allow nested comments. Documentation blocks contain comments for example code."
"deviation": "Rule 3.1",
"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."
}
]
}

0 comments on commit 190d950

Please sign in to comment.