Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbfdb8ed

Browse files
committed
Declarations6: add RULE-18-7
1 parent5ee232d commitbfdb8ed

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-
classFlexibleArrayMemberextendsMemberVariable{
24-
Structs;
25-
26-
FlexibleArrayMember(){
27-
this.getType()instanceofArrayTypeand
28-
this.getDeclaringType()=sand
29-
notexists(inti,intj|
30-
s.getAMember(i)=thisand
31-
exists(s.getAMember(j))and
32-
j>i
33-
)
34-
}
35-
}
18+
import codingstandards.c.Variable
3619

3720
fromVariableDeclarationEntrym,ArrayTypea
3821
where
3922
notisExcluded(m, Declarations2Package::declaringAFlexibleArrayMemberQuery())and
4023
m.getType()=aand
41-
m.getVariable()instanceofFlexibleArrayMemberand
24+
m.getVariable()instanceofFlexibleArrayMemberCandidateand
4225
a.getArraySize()=1
4326
selectm,"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
overridepredicateisVolatile(){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+
classFlexibleArrayMemberextendsFlexibleArrayMemberCandidate{
16+
FlexibleArrayMember(){
17+
exists(ArrayTypet|
18+
this.getType()=tand
19+
notexists(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+
classFlexibleArrayMemberCandidateextendsMemberVariable{
29+
Structs;
30+
31+
FlexibleArrayMemberCandidate(){
32+
this.getType()instanceofArrayTypeand
33+
this.getDeclaringType()=sand
34+
notexists(inti,intj|
35+
s.getAMember(i)=thisand
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+
fromFlexibleArrayMemberf
18+
wherenotisExcluded(f, Declarations6Package::flexibleArrayMembersDeclaredQuery())
19+
selectf,"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
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
structs {
2+
inta;
3+
intb[1];// COMPLIANT
4+
};
5+
6+
structs1 {
7+
inta;
8+
intb[];// NON_COMPLIANT
9+
};
10+
11+
structs2 {
12+
inta;
13+
intb[2];// COMPLIANT
14+
};
15+
16+
structs3 {
17+
inta;
18+
intb[1];// COMPLIANT
19+
inta1;
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
newtypeDeclarations6Query=
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+
QueryflexibleArrayMembersDeclaredQuery(){
89+
//autogenerate `Query` type
90+
result=
91+
// `Query` type for `flexibleArrayMembersDeclared` query
92+
TQueryC(TDeclarations6PackageQuery(TFlexibleArrayMembersDeclaredQuery()))
93+
}
94+
7895
QueryidentifiersWithExternalLinkageNotUniqueQuery(){
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)

[8]ページ先頭

©2009-2025 Movatter.jp