Skip to content

Implement Rule 17.6 #280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions c/misra/src/rules/RULE-17-6/UseOfArrayStatic.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @id c/misra/use-of-array-static
* @name RULE-17-6: The declaration of an array parameter shall not contain the static keyword between the [ ]
* @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.
* @kind problem
* @precision very-high
* @problem.severity error
* @tags external/misra/id/rule-17-6
* correctness
* external/misra/obligation/mandatory
*/

import cpp
import codingstandards.c.misra

from Parameter p
where
not isExcluded(p, StaticPackage::useOfArrayStaticQuery()) and
p.getType().(ArrayType).hasSpecifier("static")
select p, "Parameter " + p + " is declared as an array type using the static keyword."
3 changes: 3 additions & 0 deletions c/misra/test/rules/RULE-17-6/UseOfArrayStatic.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
| test.c:2:33:2:36 | arr2 | Parameter arr2 is declared as an array type using the static keyword. |
| test.c:3:39:3:42 | arr3 | Parameter arr3 is declared as an array type using the static keyword. |
| test.c:5:9:5:12 | arr4 | Parameter arr4 is declared as an array type using the static keyword. |
1 change: 1 addition & 0 deletions c/misra/test/rules/RULE-17-6/UseOfArrayStatic.qlref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rules/RULE-17-6/UseOfArrayStatic.ql
8 changes: 8 additions & 0 deletions c/misra/test/rules/RULE-17-6/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
void test_array(int arr1[10]) {} // COMPLIANT
void test_array_uses_static(int arr2[static 11]) {} // NON_COMPLIANT
void test_array_uses_static_multi(int arr3[static 12][5]) {} // NON_COMPLIANT
void test_array_uses_static_again(
int arr4[11]) { // COMPLIANT[FALSE_POSITIVE] - apparently a CodeQL
// bug where the static is associated with the fixed
// size
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import Statements3
import Statements4
import Statements5
import Statements6
import Static
import Strings1
import Strings2
import Strings3
Expand Down Expand Up @@ -128,6 +129,7 @@ newtype TCQuery =
TStatements4PackageQuery(Statements4Query q) or
TStatements5PackageQuery(Statements5Query q) or
TStatements6PackageQuery(Statements6Query q) or
TStaticPackageQuery(StaticQuery q) or
TStrings1PackageQuery(Strings1Query q) or
TStrings2PackageQuery(Strings2Query q) or
TStrings3PackageQuery(Strings3Query q) or
Expand Down Expand Up @@ -195,6 +197,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
isStatements4QueryMetadata(query, queryId, ruleId, category) or
isStatements5QueryMetadata(query, queryId, ruleId, category) or
isStatements6QueryMetadata(query, queryId, ruleId, category) or
isStaticQueryMetadata(query, queryId, ruleId, category) or
isStrings1QueryMetadata(query, queryId, ruleId, category) or
isStrings2QueryMetadata(query, queryId, ruleId, category) or
isStrings3QueryMetadata(query, queryId, ruleId, category) or
Expand Down
26 changes: 26 additions & 0 deletions cpp/common/src/codingstandards/cpp/exclusions/c/Static.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/
import cpp
import RuleMetadata
import codingstandards.cpp.exclusions.RuleMetadata

newtype StaticQuery = TUseOfArrayStaticQuery()

predicate isStaticQueryMetadata(Query query, string queryId, string ruleId, string category) {
query =
// `Query` instance for the `useOfArrayStatic` query
StaticPackage::useOfArrayStaticQuery() and
queryId =
// `@id` for the `useOfArrayStatic` query
"c/misra/use-of-array-static" and
ruleId = "RULE-17-6" and
category = "mandatory"
}

module StaticPackage {
Query useOfArrayStaticQuery() {
//autogenerate `Query` type
result =
// `Query` type for `useOfArrayStatic` query
TQueryC(TStaticPackageQuery(TUseOfArrayStaticQuery()))
}
}
26 changes: 26 additions & 0 deletions rule_packages/c/Static.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"MISRA-C-2012": {
"RULE-17-6": {
"properties": {
"obligation": "mandatory"
},
"queries": [
{
"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.",
"kind": "problem",
"name": "The declaration of an array parameter shall not contain the static keyword between the [ ]",
"precision": "very-high",
"severity": "error",
"short_name": "UseOfArrayStatic",
"tags": [
"correctness"
],
"implementation_scope": {
"description": "The static keyword is associated with particular array types in our model. This means we can get false positives when two parameter use the same array type and size, but only one of which uses the `static` keyword."
}
}
],
"title": "The declaration of an array parameter shall not contain the static keyword between the [ ]"
}
}
}
2 changes: 1 addition & 1 deletion rules.csv
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ c,MISRA-C-2012,RULE-17-2,Yes,Required,,,"Functions shall not call themselves, ei
c,MISRA-C-2012,RULE-17-3,Yes,Mandatory,,,A function shall not be declared implicitly,,Declarations6,Medium,
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,
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,
c,MISRA-C-2012,RULE-17-6,No,Mandatory,,,The declaration of an array parameter shall not contain the static keyword between the [ ],,,,
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,
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,
c,MISRA-C-2012,RULE-17-8,Yes,Advisory,,,A function parameter should not be modified,,SideEffects2,Medium,
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,
Expand Down