Skip to content

Commit bfdb8ed

Browse files
committed
Declarations6: add RULE-18-7
1 parent 5ee232d commit bfdb8ed

File tree

9 files changed

+112
-20
lines changed

9 files changed

+112
-20
lines changed

c/cert/src/rules/DCL38-C/DeclaringAFlexibleArrayMember.ql

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,12 @@
1515

1616
import cpp
1717
import codingstandards.c.cert
18-
19-
/**
20-
* A member with the type array that is last in a struct
21-
* includes any sized array (either specified or not)
22-
*/
23-
class FlexibleArrayMember extends MemberVariable {
24-
Struct s;
25-
26-
FlexibleArrayMember() {
27-
this.getType() instanceof ArrayType and
28-
this.getDeclaringType() = s and
29-
not exists(int i, int j |
30-
s.getAMember(i) = this and
31-
exists(s.getAMember(j)) and
32-
j > i
33-
)
34-
}
35-
}
18+
import codingstandards.c.Variable
3619

3720
from VariableDeclarationEntry m, ArrayType a
3821
where
3922
not isExcluded(m, Declarations2Package::declaringAFlexibleArrayMemberQuery()) and
4023
m.getType() = a and
41-
m.getVariable() instanceof FlexibleArrayMember and
24+
m.getVariable() instanceof FlexibleArrayMemberCandidate and
4225
a.getArraySize() = 1
4326
select m, "Incorrect syntax used for declaring this flexible array member."

c/common/src/codingstandards/c/Variable.qll

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,35 @@ class VlaVariable extends Variable {
66
/* Extractor workaround do determine if a VLA array has the specifier volatile.*/
77
override predicate isVolatile() { this.getType().(ArrayType).getBaseType().isVolatile() }
88
}
9+
10+
/**
11+
* A flexible array member
12+
* ie member with the type array that is last in a struct
13+
* has no size specified
14+
*/
15+
class FlexibleArrayMember extends FlexibleArrayMemberCandidate {
16+
FlexibleArrayMember() {
17+
exists(ArrayType t |
18+
this.getType() = t and
19+
not exists(t.getSize())
20+
)
21+
}
22+
}
23+
24+
/**
25+
* A member with the type array that is last in a struct
26+
* includes any sized array (either specified or not)
27+
*/
28+
class FlexibleArrayMemberCandidate extends MemberVariable {
29+
Struct s;
30+
31+
FlexibleArrayMemberCandidate() {
32+
this.getType() instanceof ArrayType and
33+
this.getDeclaringType() = s and
34+
not exists(int i, int j |
35+
s.getAMember(i) = this and
36+
exists(s.getAMember(j)) and
37+
j > i
38+
)
39+
}
40+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @id c/misra/flexible-array-members-declared
3+
* @name RULE-18-7: Flexible array members shall not be declared
4+
* @description The use of flexible array members can lead to unexpected program behaviour.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity error
8+
* @tags external/misra/id/rule-18-7
9+
* correctness
10+
* external/misra/obligation/required
11+
*/
12+
13+
import cpp
14+
import codingstandards.c.misra
15+
import codingstandards.c.Variable
16+
17+
from FlexibleArrayMember f
18+
where not isExcluded(f, Declarations6Package::flexibleArrayMembersDeclaredQuery())
19+
select f, "Flexible array member declared."
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| test.c:8:7:8:7 | b | Flexible array member declared. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-18-7/FlexibleArrayMembersDeclared.ql

c/misra/test/rules/RULE-18-7/test.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
struct s {
2+
int a;
3+
int b[1]; // COMPLIANT
4+
};
5+
6+
struct s1 {
7+
int a;
8+
int b[]; // NON_COMPLIANT
9+
};
10+
11+
struct s2 {
12+
int a;
13+
int b[2]; // COMPLIANT
14+
};
15+
16+
struct s3 {
17+
int a;
18+
int b[1]; // COMPLIANT
19+
int a1;
20+
};

cpp/common/src/codingstandards/cpp/exclusions/c/Declarations6.qll

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import codingstandards.cpp.exclusions.RuleMetadata
55

66
newtype Declarations6Query =
77
TFunctionDeclaredImplicitlyQuery() or
8+
TFlexibleArrayMembersDeclaredQuery() or
89
TIdentifiersWithExternalLinkageNotUniqueQuery() or
910
TIdentifiersWithInternalLinkageNotUniqueQuery() or
1011
TInlineFunctionNotDeclaredStaticStorageQuery() or
@@ -21,6 +22,15 @@ predicate isDeclarations6QueryMetadata(Query query, string queryId, string ruleI
2122
ruleId = "RULE-17-3" and
2223
category = "mandatory"
2324
or
25+
query =
26+
// `Query` instance for the `flexibleArrayMembersDeclared` query
27+
Declarations6Package::flexibleArrayMembersDeclaredQuery() and
28+
queryId =
29+
// `@id` for the `flexibleArrayMembersDeclared` query
30+
"c/misra/flexible-array-members-declared" and
31+
ruleId = "RULE-18-7" and
32+
category = "required"
33+
or
2434
query =
2535
// `Query` instance for the `identifiersWithExternalLinkageNotUnique` query
2636
Declarations6Package::identifiersWithExternalLinkageNotUniqueQuery() and
@@ -75,6 +85,13 @@ module Declarations6Package {
7585
TQueryC(TDeclarations6PackageQuery(TFunctionDeclaredImplicitlyQuery()))
7686
}
7787

88+
Query flexibleArrayMembersDeclaredQuery() {
89+
//autogenerate `Query` type
90+
result =
91+
// `Query` type for `flexibleArrayMembersDeclared` query
92+
TQueryC(TDeclarations6PackageQuery(TFlexibleArrayMembersDeclaredQuery()))
93+
}
94+
7895
Query identifiersWithExternalLinkageNotUniqueQuery() {
7996
//autogenerate `Query` type
8097
result =

rule_packages/c/Declarations6.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,25 @@
2020
],
2121
"title": "A function shall not be declared implicitly"
2222
},
23+
"RULE-18-7": {
24+
"properties": {
25+
"obligation": "required"
26+
},
27+
"queries": [
28+
{
29+
"description": "The use of flexible array members can lead to unexpected program behaviour.",
30+
"kind": "problem",
31+
"name": "Flexible array members shall not be declared",
32+
"precision": "very-high",
33+
"severity": "error",
34+
"short_name": "FlexibleArrayMembersDeclared",
35+
"tags": [
36+
"correctness"
37+
]
38+
}
39+
],
40+
"title": "Flexible array members shall not be declared"
41+
},
2342
"RULE-5-8": {
2443
"properties": {
2544
"obligation": "required"

rules.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ c,MISRA-C-2012,RULE-18-3,Yes,Required,,,"The relational operators >, >=, < and <
725725
c,MISRA-C-2012,RULE-18-4,Yes,Advisory,,,"The +, -, += and -= operators should not be applied to an expression of pointer type",M5-0-15,Pointers1,Medium,
726726
c,MISRA-C-2012,RULE-18-5,Yes,Advisory,,,Declarations should contain no more than two levels of pointer nesting,A5-0-3,Pointers1,Import,
727727
c,MISRA-C-2012,RULE-18-6,Yes,Required,,,The address of an object with automatic storage shall not be copied to another object that persists after the first object has ceased to exist,M7-5-2,Pointers1,Import,
728-
c,MISRA-C-2012,RULE-18-7,Yes,Required,,,Flexible array members shall not be declared,,Declarations,Medium,
728+
c,MISRA-C-2012,RULE-18-7,Yes,Required,,,Flexible array members shall not be declared,,Declarations6,Medium,
729729
c,MISRA-C-2012,RULE-18-8,Yes,Required,,,Variable-length array types shall not be used,,Declarations,Medium,
730730
c,MISRA-C-2012,RULE-19-1,Yes,Mandatory,,,An object shall not be assigned or copied to an overlapping object,M0-2-1,Contracts,Hard,
731731
c,MISRA-C-2012,RULE-19-2,Yes,Advisory,,,The union keyword should not be used,A9-5-1,Banned,Import,

0 commit comments

Comments
 (0)