Skip to content

Commit e3d7f27

Browse files
SkptakSoren PtakAniruddhaKanhere
authored
MISRA Compliance Update (#121)
* Updated the MISRA.md and misra.config files after meeting with senior SDE. Put inline supression for a comparison related to the SIZE_MAX macro. Want to get clarification about the line before putting a change in. * Fixing some whitespace/formatting issues * Changing MISRA.md file to reflect new format, modified inline supression in source file to match new formatting * Adding words to lexicon, and fixing links * Minor update to MISRA.md file to use an actual violation as the example, and expanding on a message * Update source/core_json.c Remove extra set of square brackets Co-authored-by: Aniruddha Kanhere <60444055+AniruddhaKanhere@users.noreply.github.com> * Changes to the way we determine the end in skipOneHexEscape() * Removed a redundant check of a variable * Formatting fix and adding a test in to get more line coverage * Trying to reach 100% branch coverage * Adding the removal of debug for coverity target, and then removing two rule exceptions from the misra.config due to the change * skipOneHexEscape had a line that was flagged as a MISRA 14.3 rule violation. It was flagged because it believed that the if statement comparison was invariant. This could be proven as a bug by assigning the variable a value larger than the comparison, and then still receiving the violation. A logic change has been made to get around this, but it now fails CBMC proofs. Trying a different if statement to see if this passes checks. * Forgot to add inital assign back in * After a lot of attempts to create a MISRA and CBMC compliant version of skipOneHexEscape() I believe proof was found that shows the MISRA violation is a false flag. Due to this I believe that this should simply receive a suppression and the focus should be on the CBMC proofs. Co-authored-by: Soren Ptak <skptak@amazon.com> Co-authored-by: Aniruddha Kanhere <60444055+AniruddhaKanhere@users.noreply.github.com>
1 parent cf14dc5 commit e3d7f27

File tree

5 files changed

+48
-32
lines changed

5 files changed

+48
-32
lines changed

MISRA.md

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,30 @@
22

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

7-
### Ignored by [Coverity Configuration](tools/coverity/misra.config)
8-
| Deviation | Category | Justification |
9-
| :-: | :-: | :-: |
10-
| Directive 4.9 | Advisory | Allow inclusion of function like macros. |
11-
| Rule 3.1 | Required | Allow nested comments. C++ style `//` comments are used in example code within Doxygen documentation blocks. |
12-
| Rule 8.13 | Advisory | Allow one function to have a char * argument without const qualifier. |
13-
| Rule 15.4 | Advisory | Allow more then one `break` statement to terminate a loop. |
14-
| Rule 19.2 | Advisory | Allow a `union` of a signed and unsigned type of identical sizes. |
15-
| Rule 20.12 | Required | Allow use of `assert()`, which uses a parameter in both expanded and raw forms. |
16-
17-
### Flagged by Coverity
18-
| Deviation | Category | Justification |
19-
| :-: | :-: | :-: |
20-
| Rule 2.5 | Advisory | A macro is not used by the library; however, it exists to be used by an application. |
21-
| Rule 8.7 | Advisory | API functions are not used by the library; however, they must be externally visible in order to be used by an application. |
7+
Additionally, [MISRA configuration file](https://github.com/FreeRTOS/coreJSON/blob/main/tools/coverity/misra.config) contains the project wide deviations.
228

239
### Suppressed with Coverity Comments
24-
| Deviation | Category | Justification |
25-
| :-: | :-: | :-: |
26-
| Rule 11.3 | Required | False positive - the rule permits type qualifiers to be added. |
10+
To find the violation references in the source files run grep on the source code
11+
with ( Assuming rule 11.3 violation; with justification in point 1 ):
12+
```
13+
grep 'MISRA Ref 11.3.1' . -rI
14+
```
15+
16+
#### Rule 11.3
17+
_Ref 11.3.1_
18+
19+
- MISRA C-2012 Rule 11.3 prohibits casting a pointer to a different type.
20+
This instance is a false positive, as the rule permits the
21+
addition of a const type qualifier.
22+
23+
#### Rule 14.3
24+
_Ref 14.3.1_
25+
26+
- MISRA C-2012 Rule 14.3 False positive as the static analysis tool believes
27+
i can never be larger than SIZE_MAX - HEX_ESCAPE_LENGTH. This can be proven as
28+
a bug by setting i to be 18446744073709551615UL at initial assignment, then require
29+
start != NULL before assigning the vaue of i to start. This creates a case
30+
where i should be large enough to hit the else statement, but the tool still flags
31+
this as invariant.

lexicon.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ fd
3232
fe
3333
ff
3434
ffff
35+
freertos
3536
foo
3637
gcc
38+
github
3739
html
3840
https
3941
ifndef

source/core_json.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,10 @@ static bool skipOneHexEscape( const char * buf,
335335

336336
i = *start;
337337
#define HEX_ESCAPE_LENGTH ( 6U ) /* e.g., \u1234 */
338+
339+
/* MISRA Ref 14.3.1 [Configuration dependent invariant] */
340+
/* More details at: https://github.com/FreeRTOS/coreJSON/blob/main/MISRA.md#rule-143 */
341+
/* coverity[misra_c_2012_rule_14_3_violation] */
338342
end = ( i <= ( SIZE_MAX - HEX_ESCAPE_LENGTH ) ) ? ( i + HEX_ESCAPE_LENGTH ) : SIZE_MAX;
339343

340344
if( ( end < max ) && ( buf[ i ] == '\\' ) && ( buf[ i + 1U ] == 'u' ) )
@@ -1677,9 +1681,8 @@ JSONStatus_t JSON_SearchT( char * buf,
16771681
size_t * outValueLength,
16781682
JSONTypes_t * outType )
16791683
{
1680-
/* MISRA Rule 11.3 prohibits casting a pointer to a different type.
1681-
* This instance is a false positive, as the rule permits the
1682-
* addition of a type qualifier. */
1684+
/* MISRA Ref 11.3.1 [Misaligned access] */
1685+
/* More details at: https://github.com/FreeRTOS/coreJSON/blob/main/MISRA.md#rule-113 */
16831686
/* coverity[misra_c_2012_rule_11_3_violation] */
16841687
return JSON_SearchConst( ( const char * ) buf, max, query, queryLength,
16851688
( const char ** ) outValue, outValueLength, outType );

test/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ add_library( coverity_analysis
4141
# JSON public include path.
4242
target_include_directories( coverity_analysis PUBLIC ${JSON_INCLUDE_PUBLIC_DIRS} )
4343

44+
# When building the coverity analysis target we disable debug
45+
target_compile_options(coverity_analysis PUBLIC -DNDEBUG )
46+
4447
# ==================================== Test Configuration ========================================
4548

4649
# Include Unity build configuration.

tools/coverity/misra.config

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@
1010
category: "Advisory",
1111
reason: "Allow inclusion of function like macros."
1212
},
13+
{
14+
deviation: "Rule 2.5",
15+
reason: "Allow unused macros. Library headers may define macros intended for the application's use, but not used by a specific file."
16+
},
17+
{
18+
deviation: "Rule 3.1",
19+
category: "Required",
20+
reason: "Allow nested comments. Documentation blocks contain comments for example code."
21+
},
22+
{
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+
},
1326
{
1427
deviation: "Rule 8.13",
1528
category: "Advisory",
@@ -25,15 +38,5 @@
2538
category: "Advisory",
2639
reason: "Allow a union of a signed and unsigned type of identical sizes."
2740
},
28-
{
29-
deviation: "Rule 3.1",
30-
category: "Required",
31-
reason: "Allow nested comments. Documentation blocks contain comments for example code."
32-
},
33-
{
34-
deviation: "Rule 20.12",
35-
category: "Required",
36-
reason: "Allow use of assert(), which uses a parameter in both expanded and raw forms."
37-
},
3841
]
3942
}

0 commit comments

Comments
 (0)