Skip to content

Commit

Permalink
Change treatment plain char type and CV qualified integer types.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rvermeulen committed Jan 13, 2024
1 parent d9f0911 commit cb41055
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 9 deletions.
5 changes: 5 additions & 0 deletions change_notes/2024-01-12-fix-reported-fp-a3-9-1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- `A3-9-1` - `VariableWidthIntegerTypesUsed.ql`:
- Exclude the plain char type. Still includes `signed char` and `unsigned char`.
- Include CV-qualified variable width integer types.
- `A3-9-1` - `VariableWidthPlainCharTypeUsed.ql`:
- New query to support fine grained deviation support for the plain char type.
16 changes: 9 additions & 7 deletions cpp/autosar/src/rules/A3-9-1/VariableWidthIntegerTypesUsed.ql
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* @id cpp/autosar/variable-width-integer-types-used
* @name A3-9-1: Use fixed-width integer types instead of basic, variable-width, integer types
* @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.
* @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.
* @kind problem
* @precision very-high
* @problem.severity error
Expand All @@ -19,15 +19,17 @@ import cpp
import codingstandards.cpp.autosar
import codingstandards.cpp.EncapsulatingFunctions
import codingstandards.cpp.BuiltInNumericTypes
import codingstandards.cpp.Type

from Variable v

from Variable v, Type typeStrippedOfSpecifiers
where
not isExcluded(v, DeclarationsPackage::variableWidthIntegerTypesUsedQuery()) and
typeStrippedOfSpecifiers = stripSpecifiers(v.getType()) and
(
v.getType() instanceof BuiltInIntegerType or
v.getType() instanceof PlainCharType or
v.getType() instanceof UnsignedCharType or
v.getType() instanceof SignedCharType
typeStrippedOfSpecifiers instanceof BuiltInIntegerType or
typeStrippedOfSpecifiers instanceof UnsignedCharType or
typeStrippedOfSpecifiers instanceof SignedCharType
) and
not v instanceof ExcludedVariable
select v, "Variable '" + v.getName() + "' has variable-width type."
26 changes: 26 additions & 0 deletions cpp/autosar/src/rules/A3-9-1/VariableWidthPlainCharTypeUsed.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @id cpp/autosar/variable-width-plain-char-types-used
* @name A3-9-1: Use a fixed-width integer type instead of a char type
* @description The basic numerical type char is not supposed to be used. The specific-length types
* from <cstdint> header need be used instead.
* @kind problem
* @precision very-high
* @problem.severity error
* @tags external/autosar/id/a3-9-1
* correctness
* security
* maintainability
* external/autosar/allocated-target/implementation
* external/autosar/enforcement/automated
* external/autosar/obligation/required
*/

import cpp
import codingstandards.cpp.autosar
import codingstandards.cpp.Type

from Variable variable
where
not isExcluded(variable, DeclarationsPackage::variableWidthPlainCharTypeUsedQuery()) and
stripSpecifiers(variable.getType()) instanceof PlainCharType
select variable, "Variable '" + variable.getName() + "' has variable-width char type."
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
| test.cpp:4:8:4:8 | c | Variable 'c' has variable-width type. |
| test.cpp:5:17:5:18 | uc | Variable 'uc' has variable-width type. |
| test.cpp:6:15:6:16 | sc | Variable 'sc' has variable-width type. |
| test.cpp:8:7:8:7 | i | Variable 'i' has variable-width type. |
Expand All @@ -12,3 +11,29 @@
| test.cpp:18:8:18:8 | l | Variable 'l' has variable-width type. |
| test.cpp:19:17:19:18 | ul | Variable 'ul' has variable-width type. |
| test.cpp:20:15:20:16 | sl | Variable 'sl' has variable-width type. |
| test.cpp:39:23:39:25 | uc1 | Variable 'uc1' has variable-width type. |
| test.cpp:40:21:40:23 | sc1 | Variable 'sc1' has variable-width type. |
| test.cpp:42:13:42:14 | i1 | Variable 'i1' has variable-width type. |
| test.cpp:43:22:43:24 | ui1 | Variable 'ui1' has variable-width type. |
| test.cpp:44:18:44:19 | u1 | Variable 'u1' has variable-width type. |
| test.cpp:45:20:45:22 | si1 | Variable 'si1' has variable-width type. |
| test.cpp:46:16:46:17 | s1 | Variable 's1' has variable-width type. |
| test.cpp:48:15:48:17 | sh1 | Variable 'sh1' has variable-width type. |
| test.cpp:49:24:49:27 | ush1 | Variable 'ush1' has variable-width type. |
| test.cpp:50:22:50:25 | ssh1 | Variable 'ssh1' has variable-width type. |
| test.cpp:52:14:52:15 | l1 | Variable 'l1' has variable-width type. |
| test.cpp:53:23:53:25 | ul1 | Variable 'ul1' has variable-width type. |
| test.cpp:54:21:54:23 | sl1 | Variable 'sl1' has variable-width type. |
| test.cpp:57:26:57:28 | uc2 | Variable 'uc2' has variable-width type. |
| test.cpp:58:24:58:26 | sc2 | Variable 'sc2' has variable-width type. |
| test.cpp:60:16:60:17 | i2 | Variable 'i2' has variable-width type. |
| test.cpp:61:25:61:27 | ui2 | Variable 'ui2' has variable-width type. |
| test.cpp:62:21:62:22 | u2 | Variable 'u2' has variable-width type. |
| test.cpp:63:23:63:25 | si2 | Variable 'si2' has variable-width type. |
| test.cpp:64:19:64:20 | s2 | Variable 's2' has variable-width type. |
| test.cpp:66:18:66:20 | sh2 | Variable 'sh2' has variable-width type. |
| test.cpp:67:27:67:30 | ush2 | Variable 'ush2' has variable-width type. |
| test.cpp:68:25:68:28 | ssh2 | Variable 'ssh2' has variable-width type. |
| test.cpp:70:17:70:18 | l2 | Variable 'l2' has variable-width type. |
| test.cpp:71:26:71:28 | ul2 | Variable 'ul2' has variable-width type. |
| test.cpp:72:24:72:26 | sl2 | Variable 'sl2' has variable-width type. |
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
| test.cpp:4:8:4:8 | c | Variable 'c' has variable-width char type. |
| test.cpp:38:14:38:15 | c1 | Variable 'c1' has variable-width char type. |
| test.cpp:56:17:56:18 | c2 | Variable 'c2' has variable-width char type. |
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rules/A3-9-1/VariableWidthPlainCharTypeUsed.ql
38 changes: 38 additions & 0 deletions cpp/autosar/test/rules/A3-9-1/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,42 @@ void test_variable_width_type_variables() {

int main(int argc, char *argv[]) { // COMPLIANT
// main as an exception
}

void test_variable_width_type_qualified_variables() {
const char c1 = 0; // NON_COMPLIANT
const unsigned char uc1 = 0; // NON_COMPLIANT
const signed char sc1 = 0; // NON_COMPLIANt

const int i1 = 0; // NON_COMPLIANT
const unsigned int ui1 = 0; // NON_COMPLIANT
const unsigned u1 = 0; // NON_COMPLIANT
const signed int si1 = 0; // NON_COMPLIANT
const signed s1 = 0; // NON_COMPLIANT

const short sh1 = 0; // NON_COMPLIANT
const unsigned short ush1 = 0; // NON_COMPLIANT
const signed short ssh1 = 0; // NON_COMPLIANT

const long l1 = 0; // NON_COMPLIANT
const unsigned long ul1 = 0; // NON_COMPLIANT
const signed long sl1 = 0; // NON_COMPLIANT

volatile char c2; // NON_COMPLIANT
volatile unsigned char uc2; // NON_COMPLIANT
volatile signed char sc2; // NON_COMPLIANt

volatile int i2; // NON_COMPLIANT
volatile unsigned int ui2; // NON_COMPLIANT
volatile unsigned u2; // NON_COMPLIANT
volatile signed int si2; // NON_COMPLIANT
volatile signed s2; // NON_COMPLIANT

volatile short sh2; // NON_COMPLIANT
volatile unsigned short ush2; // NON_COMPLIANT
volatile signed short ssh2; // NON_COMPLIANT

volatile long l2; // NON_COMPLIANT
volatile unsigned long ul2; // NON_COMPLIANT
volatile signed long sl2; // NON_COMPLIANT
}
9 changes: 9 additions & 0 deletions cpp/common/src/codingstandards/cpp/Type.qll
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,12 @@ class FundamentalType extends BuiltInType {
class IncompleteType extends Class {
IncompleteType() { not hasDefinition() }
}

/**
* A type without `const` and `volatile` specifiers.
*/
Type stripSpecifiers(Type type) {
if type instanceof SpecifiedType
then result = stripSpecifiers(type.(SpecifiedType).getBaseType())
else result = type
}
17 changes: 17 additions & 0 deletions cpp/common/src/codingstandards/cpp/exclusions/cpp/Declarations.qll
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ newtype DeclarationsQuery =
TGlobalSizedOperatorDeleteNotDefinedQuery() or
TGlobalUnsizedOperatorDeleteNotDefinedQuery() or
TVariableWidthIntegerTypesUsedQuery() or
TVariableWidthPlainCharTypeUsedQuery() or
TAutoSpecifierNotUsedAppropriatelyInFunctionDefinitionQuery() or
TAutoSpecifierNotUsedAppropriatelyInVariableDefinitionQuery() or
TIdentifierDeclarationAndInitializationNotOnSeparateLinesQuery() or
Expand Down Expand Up @@ -68,6 +69,15 @@ predicate isDeclarationsQueryMetadata(Query query, string queryId, string ruleId
ruleId = "A3-9-1" and
category = "required"
or
query =
// `Query` instance for the `variableWidthPlainCharTypeUsed` query
DeclarationsPackage::variableWidthPlainCharTypeUsedQuery() and
queryId =
// `@id` for the `variableWidthPlainCharTypeUsed` query
"cpp/autosar/variable-width-plain-char-type-used" and
ruleId = "A3-9-1" and
category = "required"
or
query =
// `Query` instance for the `autoSpecifierNotUsedAppropriatelyInFunctionDefinition` query
DeclarationsPackage::autoSpecifierNotUsedAppropriatelyInFunctionDefinitionQuery() and
Expand Down Expand Up @@ -213,6 +223,13 @@ module DeclarationsPackage {
TQueryCPP(TDeclarationsPackageQuery(TVariableWidthIntegerTypesUsedQuery()))
}

Query variableWidthPlainCharTypeUsedQuery() {
//autogenerate `Query` type
result =
// `Query` type for `variableWidthPlainCharTypeUsed` query
TQueryCPP(TDeclarationsPackageQuery(TVariableWidthPlainCharTypeUsedQuery()))
}

Query autoSpecifierNotUsedAppropriatelyInFunctionDefinitionQuery() {
//autogenerate `Query` type
result =
Expand Down
18 changes: 17 additions & 1 deletion rule_packages/cpp/Declarations.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,28 @@
},
"queries": [
{
"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.",
"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.",
"kind": "problem",
"name": "Use fixed-width integer types instead of basic, variable-width, integer types",
"precision": "very-high",
"severity": "error",
"short_name": "VariableWidthIntegerTypesUsed",
"tags": [
"correctness",
"security",
"maintainability"
],
"implementation_scope": {
"description": "This implementation excludes the plain char type from consideration."
}
},
{
"description": "The basic numerical type char is not supposed to be used. The specific-length types from <cstdint> header need be used instead.",
"kind": "problem",
"name": "Use a fixed-width integer type instead of a char type",
"precision": "very-high",
"severity": "error",
"short_name": "VariableWidthPlainCharTypeUsed",
"tags": [
"correctness",
"security",
Expand Down

0 comments on commit cb41055

Please sign in to comment.