Skip to content

Commit cb41055

Browse files
committed
Change treatment plain char type and CV qualified integer types.
The plain char type can hold numeric data, but is frequently used to hold character data. To be able to support the case where a plain char type holds character data we split the rule into two queries. One considering all the variable width integer types excluding the plain char type and one considering just the plain char type. This allows for deviation on the second case. Additionally, the original query wasn't considering CV qualified variable with integer types. Those are now included.
1 parent d9f0911 commit cb41055

File tree

10 files changed

+151
-9
lines changed

10 files changed

+151
-9
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
- `A3-9-1` - `VariableWidthIntegerTypesUsed.ql`:
2+
- Exclude the plain char type. Still includes `signed char` and `unsigned char`.
3+
- Include CV-qualified variable width integer types.
4+
- `A3-9-1` - `VariableWidthPlainCharTypeUsed.ql`:
5+
- New query to support fine grained deviation support for the plain char type.

cpp/autosar/src/rules/A3-9-1/VariableWidthIntegerTypesUsed.ql

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
22
* @id cpp/autosar/variable-width-integer-types-used
33
* @name A3-9-1: Use fixed-width integer types instead of basic, variable-width, integer types
4-
* @description The basic numerical types of char, int, short, long are not supposed to be used. The
5-
* specific-length types from <cstdint> header need be used instead.
4+
* @description The basic numerical types of signed/unsigned char, int, short, long are not supposed
5+
* to be used. The specific-length types from <cstdint> header need be used instead.
66
* @kind problem
77
* @precision very-high
88
* @problem.severity error
@@ -19,15 +19,17 @@ import cpp
1919
import codingstandards.cpp.autosar
2020
import codingstandards.cpp.EncapsulatingFunctions
2121
import codingstandards.cpp.BuiltInNumericTypes
22+
import codingstandards.cpp.Type
2223

23-
from Variable v
24+
25+
from Variable v, Type typeStrippedOfSpecifiers
2426
where
2527
not isExcluded(v, DeclarationsPackage::variableWidthIntegerTypesUsedQuery()) and
28+
typeStrippedOfSpecifiers = stripSpecifiers(v.getType()) and
2629
(
27-
v.getType() instanceof BuiltInIntegerType or
28-
v.getType() instanceof PlainCharType or
29-
v.getType() instanceof UnsignedCharType or
30-
v.getType() instanceof SignedCharType
30+
typeStrippedOfSpecifiers instanceof BuiltInIntegerType or
31+
typeStrippedOfSpecifiers instanceof UnsignedCharType or
32+
typeStrippedOfSpecifiers instanceof SignedCharType
3133
) and
3234
not v instanceof ExcludedVariable
3335
select v, "Variable '" + v.getName() + "' has variable-width type."
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @id cpp/autosar/variable-width-plain-char-types-used
3+
* @name A3-9-1: Use a fixed-width integer type instead of a char type
4+
* @description The basic numerical type char is not supposed to be used. The specific-length types
5+
* from <cstdint> header need be used instead.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/autosar/id/a3-9-1
10+
* correctness
11+
* security
12+
* maintainability
13+
* external/autosar/allocated-target/implementation
14+
* external/autosar/enforcement/automated
15+
* external/autosar/obligation/required
16+
*/
17+
18+
import cpp
19+
import codingstandards.cpp.autosar
20+
import codingstandards.cpp.Type
21+
22+
from Variable variable
23+
where
24+
not isExcluded(variable, DeclarationsPackage::variableWidthPlainCharTypeUsedQuery()) and
25+
stripSpecifiers(variable.getType()) instanceof PlainCharType
26+
select variable, "Variable '" + variable.getName() + "' has variable-width char type."

cpp/autosar/test/rules/A3-9-1/VariableWidthIntegerTypesUsed.expected

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
| test.cpp:4:8:4:8 | c | Variable 'c' has variable-width type. |
21
| test.cpp:5:17:5:18 | uc | Variable 'uc' has variable-width type. |
32
| test.cpp:6:15:6:16 | sc | Variable 'sc' has variable-width type. |
43
| test.cpp:8:7:8:7 | i | Variable 'i' has variable-width type. |
@@ -12,3 +11,29 @@
1211
| test.cpp:18:8:18:8 | l | Variable 'l' has variable-width type. |
1312
| test.cpp:19:17:19:18 | ul | Variable 'ul' has variable-width type. |
1413
| test.cpp:20:15:20:16 | sl | Variable 'sl' has variable-width type. |
14+
| test.cpp:39:23:39:25 | uc1 | Variable 'uc1' has variable-width type. |
15+
| test.cpp:40:21:40:23 | sc1 | Variable 'sc1' has variable-width type. |
16+
| test.cpp:42:13:42:14 | i1 | Variable 'i1' has variable-width type. |
17+
| test.cpp:43:22:43:24 | ui1 | Variable 'ui1' has variable-width type. |
18+
| test.cpp:44:18:44:19 | u1 | Variable 'u1' has variable-width type. |
19+
| test.cpp:45:20:45:22 | si1 | Variable 'si1' has variable-width type. |
20+
| test.cpp:46:16:46:17 | s1 | Variable 's1' has variable-width type. |
21+
| test.cpp:48:15:48:17 | sh1 | Variable 'sh1' has variable-width type. |
22+
| test.cpp:49:24:49:27 | ush1 | Variable 'ush1' has variable-width type. |
23+
| test.cpp:50:22:50:25 | ssh1 | Variable 'ssh1' has variable-width type. |
24+
| test.cpp:52:14:52:15 | l1 | Variable 'l1' has variable-width type. |
25+
| test.cpp:53:23:53:25 | ul1 | Variable 'ul1' has variable-width type. |
26+
| test.cpp:54:21:54:23 | sl1 | Variable 'sl1' has variable-width type. |
27+
| test.cpp:57:26:57:28 | uc2 | Variable 'uc2' has variable-width type. |
28+
| test.cpp:58:24:58:26 | sc2 | Variable 'sc2' has variable-width type. |
29+
| test.cpp:60:16:60:17 | i2 | Variable 'i2' has variable-width type. |
30+
| test.cpp:61:25:61:27 | ui2 | Variable 'ui2' has variable-width type. |
31+
| test.cpp:62:21:62:22 | u2 | Variable 'u2' has variable-width type. |
32+
| test.cpp:63:23:63:25 | si2 | Variable 'si2' has variable-width type. |
33+
| test.cpp:64:19:64:20 | s2 | Variable 's2' has variable-width type. |
34+
| test.cpp:66:18:66:20 | sh2 | Variable 'sh2' has variable-width type. |
35+
| test.cpp:67:27:67:30 | ush2 | Variable 'ush2' has variable-width type. |
36+
| test.cpp:68:25:68:28 | ssh2 | Variable 'ssh2' has variable-width type. |
37+
| test.cpp:70:17:70:18 | l2 | Variable 'l2' has variable-width type. |
38+
| test.cpp:71:26:71:28 | ul2 | Variable 'ul2' has variable-width type. |
39+
| test.cpp:72:24:72:26 | sl2 | Variable 'sl2' has variable-width type. |
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| test.cpp:4:8:4:8 | c | Variable 'c' has variable-width char type. |
2+
| test.cpp:38:14:38:15 | c1 | Variable 'c1' has variable-width char type. |
3+
| test.cpp:56:17:56:18 | c2 | Variable 'c2' has variable-width char type. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/A3-9-1/VariableWidthPlainCharTypeUsed.ql

cpp/autosar/test/rules/A3-9-1/test.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,42 @@ void test_variable_width_type_variables() {
3232

3333
int main(int argc, char *argv[]) { // COMPLIANT
3434
// main as an exception
35+
}
36+
37+
void test_variable_width_type_qualified_variables() {
38+
const char c1 = 0; // NON_COMPLIANT
39+
const unsigned char uc1 = 0; // NON_COMPLIANT
40+
const signed char sc1 = 0; // NON_COMPLIANt
41+
42+
const int i1 = 0; // NON_COMPLIANT
43+
const unsigned int ui1 = 0; // NON_COMPLIANT
44+
const unsigned u1 = 0; // NON_COMPLIANT
45+
const signed int si1 = 0; // NON_COMPLIANT
46+
const signed s1 = 0; // NON_COMPLIANT
47+
48+
const short sh1 = 0; // NON_COMPLIANT
49+
const unsigned short ush1 = 0; // NON_COMPLIANT
50+
const signed short ssh1 = 0; // NON_COMPLIANT
51+
52+
const long l1 = 0; // NON_COMPLIANT
53+
const unsigned long ul1 = 0; // NON_COMPLIANT
54+
const signed long sl1 = 0; // NON_COMPLIANT
55+
56+
volatile char c2; // NON_COMPLIANT
57+
volatile unsigned char uc2; // NON_COMPLIANT
58+
volatile signed char sc2; // NON_COMPLIANt
59+
60+
volatile int i2; // NON_COMPLIANT
61+
volatile unsigned int ui2; // NON_COMPLIANT
62+
volatile unsigned u2; // NON_COMPLIANT
63+
volatile signed int si2; // NON_COMPLIANT
64+
volatile signed s2; // NON_COMPLIANT
65+
66+
volatile short sh2; // NON_COMPLIANT
67+
volatile unsigned short ush2; // NON_COMPLIANT
68+
volatile signed short ssh2; // NON_COMPLIANT
69+
70+
volatile long l2; // NON_COMPLIANT
71+
volatile unsigned long ul2; // NON_COMPLIANT
72+
volatile signed long sl2; // NON_COMPLIANT
3573
}

cpp/common/src/codingstandards/cpp/Type.qll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,12 @@ class FundamentalType extends BuiltInType {
2222
class IncompleteType extends Class {
2323
IncompleteType() { not hasDefinition() }
2424
}
25+
26+
/**
27+
* A type without `const` and `volatile` specifiers.
28+
*/
29+
Type stripSpecifiers(Type type) {
30+
if type instanceof SpecifiedType
31+
then result = stripSpecifiers(type.(SpecifiedType).getBaseType())
32+
else result = type
33+
}

cpp/common/src/codingstandards/cpp/exclusions/cpp/Declarations.qll

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ newtype DeclarationsQuery =
99
TGlobalSizedOperatorDeleteNotDefinedQuery() or
1010
TGlobalUnsizedOperatorDeleteNotDefinedQuery() or
1111
TVariableWidthIntegerTypesUsedQuery() or
12+
TVariableWidthPlainCharTypeUsedQuery() or
1213
TAutoSpecifierNotUsedAppropriatelyInFunctionDefinitionQuery() or
1314
TAutoSpecifierNotUsedAppropriatelyInVariableDefinitionQuery() or
1415
TIdentifierDeclarationAndInitializationNotOnSeparateLinesQuery() or
@@ -68,6 +69,15 @@ predicate isDeclarationsQueryMetadata(Query query, string queryId, string ruleId
6869
ruleId = "A3-9-1" and
6970
category = "required"
7071
or
72+
query =
73+
// `Query` instance for the `variableWidthPlainCharTypeUsed` query
74+
DeclarationsPackage::variableWidthPlainCharTypeUsedQuery() and
75+
queryId =
76+
// `@id` for the `variableWidthPlainCharTypeUsed` query
77+
"cpp/autosar/variable-width-plain-char-type-used" and
78+
ruleId = "A3-9-1" and
79+
category = "required"
80+
or
7181
query =
7282
// `Query` instance for the `autoSpecifierNotUsedAppropriatelyInFunctionDefinition` query
7383
DeclarationsPackage::autoSpecifierNotUsedAppropriatelyInFunctionDefinitionQuery() and
@@ -213,6 +223,13 @@ module DeclarationsPackage {
213223
TQueryCPP(TDeclarationsPackageQuery(TVariableWidthIntegerTypesUsedQuery()))
214224
}
215225

226+
Query variableWidthPlainCharTypeUsedQuery() {
227+
//autogenerate `Query` type
228+
result =
229+
// `Query` type for `variableWidthPlainCharTypeUsed` query
230+
TQueryCPP(TDeclarationsPackageQuery(TVariableWidthPlainCharTypeUsedQuery()))
231+
}
232+
216233
Query autoSpecifierNotUsedAppropriatelyInFunctionDefinitionQuery() {
217234
//autogenerate `Query` type
218235
result =

rule_packages/cpp/Declarations.json

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,28 @@
7878
},
7979
"queries": [
8080
{
81-
"description": "The basic numerical types of char, int, short, long are not supposed to be used. The specific-length types from <cstdint> header need be used instead.",
81+
"description": "The basic numerical types of signed/unsigned char, int, short, long are not supposed to be used. The specific-length types from <cstdint> header need be used instead.",
8282
"kind": "problem",
8383
"name": "Use fixed-width integer types instead of basic, variable-width, integer types",
8484
"precision": "very-high",
8585
"severity": "error",
8686
"short_name": "VariableWidthIntegerTypesUsed",
87+
"tags": [
88+
"correctness",
89+
"security",
90+
"maintainability"
91+
],
92+
"implementation_scope": {
93+
"description": "This implementation excludes the plain char type from consideration."
94+
}
95+
},
96+
{
97+
"description": "The basic numerical type char is not supposed to be used. The specific-length types from <cstdint> header need be used instead.",
98+
"kind": "problem",
99+
"name": "Use a fixed-width integer type instead of a char type",
100+
"precision": "very-high",
101+
"severity": "error",
102+
"short_name": "VariableWidthPlainCharTypeUsed",
87103
"tags": [
88104
"correctness",
89105
"security",

0 commit comments

Comments
 (0)