diff --git a/MISRA.md b/MISRA.md index 95482605..f1f37c19 100644 --- a/MISRA.md +++ b/MISRA.md @@ -1,25 +1,15 @@ # MISRA Compliance -The Device Shadow library files conform to the [MISRA C:2012](https://www.misra.org.uk) +The Device Shadow Library files conform to the [MISRA C:2012](https://www.misra.org.uk) guidelines, with some noted exceptions. Compliance is checked with Coverity static analysis. -Deviations from the MISRA standard are listed below: +The specific deviations, suppressed inline, are listed below. -### Ignored by [Coverity Configuration](tools/misra.config) -| Deviation | Category | Justification | -| :-: | :-: | :-: | -| Directive 4.5 | Advisory | Allow names that MISRA considers ambiguous (such as LogInfo and LogError) | -| Directive 4.8 | Advisory | Allow inclusion of unused types. Header files for a specific port, which are needed by all files, may define types that are not used by a specific file. | -| Directive 4.9 | Advisory | Allow inclusion of function like macros. The `assert` macro is used throughout the library for parameter validation, and logging is done using function like macros. | -| Rule 2.3 | Advisory | Allow unused types. Library headers may define types intended for the application's use, but not used within the library files. | -| Rule 2.4 | Advisory | Allow unused tags. Some compilers warn if types are not tagged. | -| Rule 2.5 | Advisory | Allow unused macros. Library headers may define macros intended for the application's use, but are not used by a specific file. | -| Rule 3.1 | Required | Allow nested comments. C++ style `//` comments are used in example code within Doxygen documentation blocks. | -| Rule 11.5 | Advisory | Allow casts from `void *`. Fields may be passed as `void *`, requiring a cast to the correct data type before use. | - -### Flagged by Coverity -| Deviation | Category | Justification | -| :-: | :-: | :-: | -| Rule 8.7 | Advisory | API functions are not used by the library outside of the files they are defined; however, they must be externally visible in order to be used by an application. | +Additionally, [MISRA configuration file](https://github.com/aws/Device-Shadow-for-AWS-IoT-embedded-sdk/blob/main/tools/coverity/misra.config) contains the project wide deviations. ### Suppressed with Coverity Comments -*None.* +To find the violation references in the source files run grep on the source code +with ( Assuming rule 11.4 violation; with justification in point 2 ): +``` +grep 'MISRA Ref 11.4.2' . -rI + +*None.* \ No newline at end of file diff --git a/source/shadow.c b/source/shadow.c index 705147aa..e90a57b4 100644 --- a/source/shadow.c +++ b/source/shadow.c @@ -290,9 +290,9 @@ static ShadowStatus_t validateMatchTopicParameters( const char * pTopic, { shadowStatus = SHADOW_BAD_PARAMETER; LogError( ( "Invalid input parameters pTopic: %p, topicLength: %u, pMessageType: %p.", - ( void * ) pTopic, + ( const void * ) pTopic, ( unsigned int ) topicLength, - ( void * ) pMessageType ) ); + ( const void * ) pMessageType ) ); } return shadowStatus; @@ -319,13 +319,13 @@ static ShadowStatus_t validateAssembleTopicParameters( ShadowTopicStringType_t t { LogError( ( "Invalid input parameters pTopicBuffer: %p, pThingName: %p, thingNameLength: %u,\ pShadowName: %p, shadowNameLength: %u, topicType: %d, pOutLength: %p.", - ( void * ) pTopicBuffer, - ( void * ) pThingName, + ( const void * ) pTopicBuffer, + ( const void * ) pThingName, ( unsigned int ) thingNameLength, - ( void * ) pShadowName, + ( const void * ) pShadowName, ( unsigned int ) shadowNameLength, ( int ) topicType, - ( void * ) pOutLength ) ); + ( const void * ) pOutLength ) ); } else if( thingNameLength > SHADOW_THINGNAME_MAX_LENGTH ) { diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index cdbba22f..56a63901 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -47,6 +47,8 @@ target_include_directories( coverity_analysis ${SHADOW_INCLUDE_PUBLIC_DIRS} "${CMAKE_CURRENT_LIST_DIR}/include" ) +target_compile_options(coverity_analysis PUBLIC -DNDEBUG -DDISABLE_LOGGING ) + # ==================================== Test Configuration ======================================== # Define a CMock resource path. diff --git a/test/include/shadow_config.h b/test/include/shadow_config.h index e02871ae..f600e9cc 100644 --- a/test/include/shadow_config.h +++ b/test/include/shadow_config.h @@ -30,12 +30,29 @@ #include -#define LogError( message ) printf( "Error: " ); printf message; printf( "\n" ) - -#define LogWarn( message ) printf( "Warn: " ); printf message; printf( "\n" ) - -#define LogInfo( message ) printf( "Info: " ); printf message; printf( "\n" ) - -#define LogDebug( message ) printf( "Debug: " ); printf message; printf( "\n" ) +#ifdef DISABLE_LOGGING + #ifndef LogError + #define LogError( message ) + #endif + #ifndef LogWarn + #define LogWarn( message ) + #endif + + #ifndef LogInfo + #define LogInfo( message ) + #endif + + #ifndef LogDebug + #define LogDebug( message ) + #endif +#else /* ! DISABLE_LOGGING */ + #define LogError( message ) printf( "Error: " ); printf message; printf( "\n" ) + + #define LogWarn( message ) printf( "Warn: " ); printf message; printf( "\n" ) + + #define LogInfo( message ) printf( "Info: " ); printf message; printf( "\n" ) + + #define LogDebug( message ) printf( "Debug: " ); printf message; printf( "\n" ) +#endif /* DISABLE_LOGGING */ #endif /* ifndef SHADOW_CONFIG_H_ */ diff --git a/tools/coverity/misra.config b/tools/coverity/misra.config new file mode 100644 index 00000000..ffe13b6a --- /dev/null +++ b/tools/coverity/misra.config @@ -0,0 +1,26 @@ +// MISRA C-2012 Rules + +{ + version : "2.0", + standard : "c2012", + title: "Coverity MISRA Configuration", + deviations : [ + // Disable the following rules. + { + deviation: "Rule 2.4", + reason: "Allow unused tags. Some compilers warn if types are not tagged." + }, + { + 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", + 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." + }, + ] +} diff --git a/tools/misra.config b/tools/misra.config deleted file mode 100644 index ed2927c3..00000000 --- a/tools/misra.config +++ /dev/null @@ -1,46 +0,0 @@ -// MISRA C-2012 Rules - -{ - version : "2.0", - standard : "c2012", - title: "Coverity MISRA Configuration", - deviations : [ - // Disable the following rules. - { - deviation: "Directive 4.5", - reason: "Allow names that MISRA considers ambiguous (such as LogInfo and LogError)." - }, - { - deviation: "Directive 4.8", - reason: "Allow inclusion of unused types. Header files for a specific port, which are needed by all files, may define types that are not used by a specific file." - }, - { - deviation: "Directive 4.9", - reason: "Allow inclusion of function like macros. Logging is done using function like macros." - }, - { - deviation: "Rule 2.4", - reason: "Allow unused tags. Some compilers warn if types are not tagged." - }, - { - 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", - reason: "Allow nested comments. Documentation blocks contain comments for example code." - }, - { - deviation: "Rule 11.5", - reason: "Allow casts from void *. Contexts are passed as void * and must be cast to the correct data type before use." - }, - { - deviation: "Rule 21.1", - reason: "Allow use of all macro names. For compatibility, some macros introduced in C99 are defined for use with C90 compilers." - }, - { - deviation: "Rule 21.2", - reason: "Allow use of all macro and identifier names. For compatibility, some macros introduced in C99 are defined for use with C90 compilers." - } - ] -}