Skip to content

Commit 11bc7ce

Browse files
committed
Implement Rule 17.6
Adds a query to identify parameter array types which use the static keyword. Note: there is a CodeQL bug which means the static keyword is associated with the array size, not the specific parameter.
1 parent 200c125 commit 11bc7ce

File tree

8 files changed

+86
-1
lines changed

8 files changed

+86
-1
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @id c/misra/use-of-array-static
3+
* @name RULE-17-6: The declaration of an array parameter shall not contain the static keyword between the [ ]
4+
* @description Using the static keyword in an array type is error prone, and relies on the
5+
* programmer to adhere to the guarantees to avoid undefined behavior.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-17-6
10+
* correctness
11+
* external/misra/obligation/mandatory
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.misra
16+
17+
from Parameter p
18+
where
19+
not isExcluded(p, StaticPackage::useOfArrayStaticQuery()) and
20+
p.getType().(ArrayType).hasSpecifier("static")
21+
select p, "Parameter " + p + " is declared as an array type using the static keyword."
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| test.c:2:33:2:36 | arr2 | Parameter arr2 is declared as an array type using the static keyword. |
2+
| test.c:3:39:3:42 | arr3 | Parameter arr3 is declared as an array type using the static keyword. |
3+
| test.c:5:9:5:12 | arr4 | Parameter arr4 is declared as an array type using the static keyword. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-17-6/UseOfArrayStatic.ql

c/misra/test/rules/RULE-17-6/test.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
void test_array(int arr1[10]) {} // COMPLIANT
2+
void test_array_uses_static(int arr2[static 11]) {} // NON_COMPLIANT
3+
void test_array_uses_static_multi(int arr3[static 12][5]) {} // NON_COMPLIANT
4+
void test_array_uses_static_again(
5+
int arr4[11]) { // COMPLIANT[FALSE_POSITIVE] - apparently a CodeQL
6+
// bug where the static is associated with the fixed
7+
// size
8+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import Statements3
6161
import Statements4
6262
import Statements5
6363
import Statements6
64+
import Static
6465
import Strings1
6566
import Strings2
6667
import Strings3
@@ -128,6 +129,7 @@ newtype TCQuery =
128129
TStatements4PackageQuery(Statements4Query q) or
129130
TStatements5PackageQuery(Statements5Query q) or
130131
TStatements6PackageQuery(Statements6Query q) or
132+
TStaticPackageQuery(StaticQuery q) or
131133
TStrings1PackageQuery(Strings1Query q) or
132134
TStrings2PackageQuery(Strings2Query q) or
133135
TStrings3PackageQuery(Strings3Query q) or
@@ -195,6 +197,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
195197
isStatements4QueryMetadata(query, queryId, ruleId, category) or
196198
isStatements5QueryMetadata(query, queryId, ruleId, category) or
197199
isStatements6QueryMetadata(query, queryId, ruleId, category) or
200+
isStaticQueryMetadata(query, queryId, ruleId, category) or
198201
isStrings1QueryMetadata(query, queryId, ruleId, category) or
199202
isStrings2QueryMetadata(query, queryId, ruleId, category) or
200203
isStrings3QueryMetadata(query, queryId, ruleId, category) or
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/
2+
import cpp
3+
import RuleMetadata
4+
import codingstandards.cpp.exclusions.RuleMetadata
5+
6+
newtype StaticQuery = TUseOfArrayStaticQuery()
7+
8+
predicate isStaticQueryMetadata(Query query, string queryId, string ruleId, string category) {
9+
query =
10+
// `Query` instance for the `useOfArrayStatic` query
11+
StaticPackage::useOfArrayStaticQuery() and
12+
queryId =
13+
// `@id` for the `useOfArrayStatic` query
14+
"c/misra/use-of-array-static" and
15+
ruleId = "RULE-17-6" and
16+
category = "mandatory"
17+
}
18+
19+
module StaticPackage {
20+
Query useOfArrayStaticQuery() {
21+
//autogenerate `Query` type
22+
result =
23+
// `Query` type for `useOfArrayStatic` query
24+
TQueryC(TStaticPackageQuery(TUseOfArrayStaticQuery()))
25+
}
26+
}

rule_packages/c/Static.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"MISRA-C-2012": {
3+
"RULE-17-6": {
4+
"properties": {
5+
"obligation": "mandatory"
6+
},
7+
"queries": [
8+
{
9+
"description": "Using the static keyword in an array type is error prone, and relies on the programmer to adhere to the guarantees to avoid undefined behavior.",
10+
"kind": "problem",
11+
"name": "The declaration of an array parameter shall not contain the static keyword between the [ ]",
12+
"precision": "very-high",
13+
"severity": "error",
14+
"short_name": "UseOfArrayStatic",
15+
"tags": [
16+
"correctness"
17+
]
18+
}
19+
],
20+
"title": "The declaration of an array parameter shall not contain the static keyword between the [ ]"
21+
}
22+
}
23+
}

rules.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ c,MISRA-C-2012,RULE-17-2,Yes,Required,,,"Functions shall not call themselves, ei
716716
c,MISRA-C-2012,RULE-17-3,Yes,Mandatory,,,A function shall not be declared implicitly,,Declarations6,Medium,
717717
c,MISRA-C-2012,RULE-17-4,Yes,Mandatory,,,All exit paths from a function with non-void return type shall have an explicit return statement with an expression,MSC52-CPP,Statements5,Medium,
718718
c,MISRA-C-2012,RULE-17-5,Yes,Advisory,,,The function argument corresponding to a parameter declared to have an array type shall have an appropriate number of elements,,Contracts6,Hard,
719-
c,MISRA-C-2012,RULE-17-6,No,Mandatory,,,The declaration of an array parameter shall not contain the static keyword between the [ ],,,,
719+
c,MISRA-C-2012,RULE-17-6,Yes,Mandatory,,,The declaration of an array parameter shall not contain the static keyword between the [ ],,Static,Easy,
720720
c,MISRA-C-2012,RULE-17-7,Yes,Required,,,The value returned by a function having non-void return type shall be used,A0-1-2,Contracts6,Easy,
721721
c,MISRA-C-2012,RULE-17-8,Yes,Advisory,,,A function parameter should not be modified,,SideEffects2,Medium,
722722
c,MISRA-C-2012,RULE-18-1,Yes,Required,,,A pointer resulting from arithmetic on a pointer operand shall address an element of the same array as that pointer operand,M5-0-16,Pointers1,Import,

0 commit comments

Comments
 (0)