Skip to content

Commit f49f268

Browse files
SkptakSoren Ptak
andauthored
MISRA Compliance Update (#102)
* Adding a MISRA.md file Swapping inline suppression to follow new format. Cleaning up some of the violations, removing suppressions that weren't needed. * Formatting * Missed a space * Swapping back to original casts for CBMC proofs * Fixing cast issue * Reverting type changes for CBMC proofs * Reverting casts for CBMC proofs * Added a MISRA readme * Some changes to the Coverity README after reading it over again * Added a newline at the end of the coverity README * Adding newline to end of coverity README * Removing unnecessary suppression Co-authored-by: Soren Ptak <skptak@amazon.com>
1 parent 066cf4c commit f49f268

10 files changed

Lines changed: 297 additions & 160 deletions

MISRA.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# MISRA Compliance
2+
3+
The FreeRTOS-Cellular-Interface library files conform to the [MISRA C:2012](https://www.misra.org.uk/MISRAHome/MISRAC2012/tabid/196/Default.aspx)
4+
guidelines, with the deviations listed below. Compliance is checked with Coverity static analysis.
5+
The specific deviations, suppressed inline, are listed below.
6+
7+
Additionally, [MISRA configuration file](https://github.com/FreeRTOS/FreeRTOS-Cellular-Interface/blob/main/tools/coverity/misra.config) contains the project wide deviations.
8+
9+
### Suppressed with Coverity Comments
10+
To find the violation references in the source files run grep on the source code
11+
with ( Assuming rule 10.4 violation; with justification in point 1 ):
12+
```
13+
grep 'MISRA Ref 10.4.1' . -rI
14+
```
15+
16+
#### Directive 4.6
17+
_Ref 4.6.1_
18+
19+
- MISRA C-2012 Directive 4.6 warns against using types that do not contain size
20+
and sign information. The use of isalpha, isdigit, and isspace set this directive
21+
off since they use base types that don't contain this information. We do not have
22+
control over these functions so we are suppressing these violations.
23+
24+
#### Directive 4.7
25+
_Ref 4.7.1_
26+
27+
- MISRA C 2012 Directive 4.7 requires that when a function returns error information,
28+
that error information shall be tested. The following line violates MISRA rule 4.7
29+
because return value of strtol() is not checked for error. This violation is
30+
justified because error checking by "errno" for any POSIX API is not thread
31+
safe in FreeRTOS unless "configUSE_POSIX_ERRNO" is enabled. In order to avoid the
32+
dependency on this feature, errno variable is not used.
33+
34+
#### Rule 10.4
35+
_Ref 10.4.1_
36+
37+
- MISRA C-2012 Rule 10.4 warns about using different types in an operation.
38+
This rule is being flagged because of use of the standard library functions
39+
isalpha, isdigit, and isspace. We do not have control over these functions so we
40+
are suppressing these violations.
41+
42+
#### Rule 10.5
43+
_Ref 10.5.1_
44+
45+
- MISRA C-2012 Rule 10.5 Converting to an enum type. The variables
46+
are checked to ensure that they are valid, and within a valid range.
47+
Hence, assigning the value of the variable with a enum cast.
48+
49+
#### Rule 10.8
50+
_Ref 10.8.1_
51+
52+
- MISRA C-2012 Rule 10.8 warns about casting from unsigned to signed types.
53+
This rule is being flagged because of use of the standard library functions
54+
isalpha and isdigit. We do not have control over these so we are suppressing
55+
these violations.
56+
57+
#### Rule 11.3
58+
_Ref 11.3.1_
59+
60+
- MISRA C-2012 Rule 11.3 does not allow casting of a pointer to different object types.
61+
We are passing in a length variable which is then checked to determine what to
62+
cast this value to. As such we are not worried about the chance of
63+
misaligned access when using the cast variable.
64+
65+
#### Rule 21.6
66+
_Ref 21.6.1_
67+
68+
- MISRA C-2012 Rule 21.6 warns about the use of standard library input/output
69+
functions as they might have implementation defined or undefined
70+
behaviour. The max length of the strings are fixed and checked offline.
71+
72+
#### Rule 21.9
73+
_Ref 21.9.1_
74+
75+
- MISRA C-2012 Rule 21.9 does not allow the use of bsearch. This is becasue of
76+
unspecified behavior, which relates to the treatment of elements that compare as
77+
equal, can be avoided by ensuring that the comparison function never returns 0.
78+
When two elements are otherwise equal, the comparison function could
79+
return a value that indicates their relative order in the initial array.
80+
This the token table must be checked without duplicated string. The return value
81+
is 0 only if the string is exactly the same.
82+
83+
#### Rule 22.8
84+
_Ref 22.8.1_
85+
86+
- MISRA C 2012 Rule 22.8 Requires the errno variable must be "zero" before calling
87+
strtol function. This violation is justified because error checking by "errno"
88+
for any POSIX API is not thread safe in FreeRTOS unless "configUSE_POSIX_ERRNO"
89+
is enabled. In order to avoid the dependency on this feature, errno variable is
90+
not used. The function strtol returns LONG_MIN and LONG_MAX in case of underflow
91+
and overflow respectively and sets the errno to ERANGE. It is not possible to
92+
distinguish between valid LONG_MIN and LONG_MAX return values and underflow and
93+
overflow scenarios without checking errno. Therefore, we cannot check return value
94+
of strtol for errors. We ensure that strtol performed a valid conversion by
95+
checking that *pEndPtr is '\0'. strtol stores the address of the first invalid
96+
character in *pEndPtr and therefore, '\0' value of *pEndPtr means that the complete
97+
pToken string passed for conversion was valid and a valid conversion wasperformed.
98+
99+
#### Rule 22.9
100+
_Ref 22.9.1_
101+
102+
- MISRA C 2012 Rule 22.9 requires that errno must be tested after strtol function is
103+
called.This violation is justified because error checking by "errno"
104+
for any POSIX API is not thread safe in FreeRTOS unless "configUSE_POSIX_ERRNO"
105+
is enabled. In order to avoid the dependency on this feature, errno variable is
106+
not used. The function strtol returns LONG_MIN and LONG_MAX in case of underflow
107+
and overflow respectively and sets the errno to ERANGE. It is not possible to
108+
distinguish between valid LONG_MIN and LONG_MAX return values and underflow and
109+
overflow scenarios without checking errno. Therefore, we cannot check return value
110+
of strtol for errors. We ensure that strtol performed a valid conversion by
111+
checking that *pEndPtr is '\0'. strtol stores the address of the first invalid
112+
character in *pEndPtr and therefore, '\0' value of *pEndPtr means that the complete
113+
pToken string passed for conversion was valid and a valid conversion wasperformed.

source/cellular_3gpp_api.c

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -894,9 +894,8 @@ static bool _parseCopsRegModeToken( char * pToken,
894894
{
895895
if( ( var >= 0 ) && ( var < ( int32_t ) REGISTRATION_MODE_MAX ) )
896896
{
897-
/* Variable "var" is ensured that it is valid and within
898-
* a valid range. Hence, assigning the value of the variable to
899-
* networkRegMode with a enum cast. */
897+
/* MISRA Ref 10.5.1 [Essential type casting] */
898+
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Cellular-Interface/blob/main/MISRA.md#rule-105 */
900899
/* coverity[misra_c_2012_rule_10_5_violation] */
901900
pOperatorInfo->networkRegMode = ( CellularNetworkRegistrationMode_t ) var;
902901
}
@@ -934,9 +933,8 @@ static bool _parseCopsNetworkNameFormatToken( const char * pToken,
934933
if( ( var >= 0 ) &&
935934
( var < ( int32_t ) OPERATOR_NAME_FORMAT_MAX ) )
936935
{
937-
/* Variable "var" is ensured that it is valid and within
938-
* a valid range. Hence, assigning the value of the variable to
939-
* operatorNameFormat with a enum cast. */
936+
/* MISRA Ref 10.5.1 [Essential type casting] */
937+
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Cellular-Interface/blob/main/MISRA.md#rule-105 */
940938
/* coverity[misra_c_2012_rule_10_5_violation] */
941939
pOperatorInfo->operatorNameFormat = ( CellularOperatorNameFormat_t ) var;
942940
}
@@ -1022,9 +1020,8 @@ static bool _parseCopsRatToken( const char * pToken,
10221020
{
10231021
if( ( var < ( int32_t ) CELLULAR_RAT_MAX ) && ( var >= 0 ) )
10241022
{
1025-
/* Variable "var" is ensured that it is valid and within
1026-
* a valid range. Hence, assigning the value of the variable to
1027-
* rat with a enum cast. */
1023+
/* MISRA Ref 10.5.1 [Essential type casting] */
1024+
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Cellular-Interface/blob/main/MISRA.md#rule-105 */
10281025
/* coverity[misra_c_2012_rule_10_5_violation] */
10291026
pOperatorInfo->rat = ( CellularRat_t ) var;
10301027
}
@@ -1697,8 +1694,8 @@ CellularError_t Cellular_CommonSetEidrxSettings( CellularHandle_t cellularHandle
16971694
{
16981695
/* Form the AT command. */
16991696

1700-
/* The return value of snprintf is not used.
1701-
* The max length of the string is fixed and checked offline. */
1697+
/* MISRA Ref 21.6.1 [Use of snprintf] */
1698+
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Cellular-Interface/blob/main/MISRA.md#rule-216 */
17021699
/* coverity[misra_c_2012_rule_21_6_violation]. */
17031700
( void ) snprintf( cmdBuf, CELLULAR_AT_CMD_MAX_SIZE, "%s%d,%d,\"" PRINTF_BINARY_PATTERN_INT4 "\"",
17041701
"AT+CEDRXS=",
@@ -2050,10 +2047,12 @@ CellularError_t Cellular_CommonGetIPAddress( CellularHandle_t cellularHandle,
20502047
{
20512048
/* Form the AT command. */
20522049

2053-
/* The return value of snprintf is not used.
2054-
* The max length of the string is fixed and checked offline. */
2050+
2051+
/* MISRA Ref 21.6.1 [Use of snprintf] */
2052+
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Cellular-Interface/blob/main/MISRA.md#rule-216 */
20552053
/* coverity[misra_c_2012_rule_21_6_violation]. */
20562054
( void ) snprintf( cmdBuf, CELLULAR_AT_CMD_TYPICAL_MAX_SIZE, "%s%d", "AT+CGPADDR=", contextId );
2055+
20572056
pktStatus = _Cellular_AtcmdRequestWithCallback( pContext, atReqGetIp );
20582057

20592058
if( pktStatus != CELLULAR_PKT_STATUS_OK )
@@ -2196,8 +2195,8 @@ CellularError_t Cellular_CommonSetPdnConfig( CellularHandle_t cellularHandle,
21962195
{
21972196
/* Form the AT command. */
21982197

2199-
/* The return value of snprintf is not used.
2200-
* The max length of the string is fixed and checked offline. */
2198+
/* MISRA Ref 21.6.1 [Use of snprintf] */
2199+
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Cellular-Interface/blob/main/MISRA.md#rule-216 */
22012200
/* coverity[misra_c_2012_rule_21_6_violation]. */
22022201
( void ) snprintf( cmdBuf, CELLULAR_AT_CMD_MAX_SIZE, "%s%d,\"%s\",\"%s\"",
22032202
"AT+CGDCONT=",
@@ -2782,16 +2781,16 @@ static uint32_t appendBinaryPattern( char * cmdBuf,
27822781

27832782
if( value != 0U )
27842783
{
2785-
/* The return value of snprintf is not used.
2786-
* The max length of the string is fixed and checked offline. */
2784+
/* MISRA Ref 21.6.1 [Use of snprintf] */
2785+
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Cellular-Interface/blob/main/MISRA.md#rule-216 */
27872786
/* coverity[misra_c_2012_rule_21_6_violation]. */
27882787
( void ) snprintf( cmdBuf, cmdLen, "\"" PRINTF_BINARY_PATTERN_INT8 "\"%c",
27892788
PRINTF_BYTE_TO_BINARY_INT8( value ), endOfString ? '\0' : ',' );
27902789
}
27912790
else
27922791
{
2793-
/* The return value of snprintf is not used.
2794-
* The max length of the string is fixed and checked offline. */
2792+
/* MISRA Ref 21.6.1 [Use of snprintf] */
2793+
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Cellular-Interface/blob/main/MISRA.md#rule-216 */
27952794
/* coverity[misra_c_2012_rule_21_6_violation]. */
27962795
( void ) snprintf( cmdBuf, cmdLen, "%c", endOfString ? '\0' : ',' );
27972796
}
@@ -2835,8 +2834,8 @@ CellularError_t Cellular_CommonSetPsmSettings( CellularHandle_t cellularHandle,
28352834
{
28362835
/* Form the AT command. */
28372836

2838-
/* The return value of snprintf is not used.
2839-
* The max length of the string is fixed and checked offline. */
2837+
/* MISRA Ref 21.6.1 [Use of snprintf] */
2838+
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Cellular-Interface/blob/main/MISRA.md#rule-216 */
28402839
/* coverity[misra_c_2012_rule_21_6_violation]. */
28412840
( void ) snprintf( cmdBuf, CELLULAR_AT_CMD_MAX_SIZE, "AT+CPSMS=%d,", pPsmSettings->mode );
28422841
cmdBufLen = ( uint32_t ) strlen( cmdBuf );

source/cellular_3gpp_urc_handler.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ static CellularPktStatus_t _parseRegStatusInRegStatusParsing( CellularContext_t
112112
{
113113
if( ( tempValue >= 0 ) && ( tempValue < ( int32_t ) REGISTRATION_STATUS_MAX ) )
114114
{
115-
/* tempValue range is checked before casting. */
115+
/* MISRA Ref 10.5.1 [Essential type casting] */
116+
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Cellular-Interface/blob/main/MISRA.md#rule-105 */
116117
/* coverity[misra_c_2012_rule_10_5_violation] */
117118
regStatus = ( CellularNetworkRegistrationStatus_t ) tempValue;
118119
}
@@ -257,9 +258,8 @@ static CellularPktStatus_t _parseRatInfoInRegStatus( const char * pToken,
257258
else if( ( var == ( int32_t ) CELLULAR_RAT_GSM ) || ( var == ( int32_t ) CELLULAR_RAT_EDGE ) ||
258259
( var == ( int32_t ) CELLULAR_RAT_CATM1 ) || ( var == ( int32_t ) CELLULAR_RAT_NBIOT ) )
259260
{
260-
/* Variable "var" is ensured that it is valid and within
261-
* a valid range. Hence, assigning the value of the variable to
262-
* rat with a enum cast. */
261+
/* MISRA Ref 10.5.1 [Essential type casting] */
262+
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Cellular-Interface/blob/main/MISRA.md#rule-105 */
263263
/* coverity[misra_c_2012_rule_10_5_violation] */
264264
pLibAtData->rat = ( CellularRat_t ) var;
265265
}

0 commit comments

Comments
 (0)