Skip to content

Commit 71eb244

Browse files
committed
Statements: Implement Rule 15.1
Adds a query to find uses of goto in the program. This was originally marked as compiler implemented, but even though it is true that the compiler can check this case we can still provide a query for it.
1 parent bac2291 commit 71eb244

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/goto-statement-used
3+
* @name RULE-15-1: The goto statement should not be used
4+
* @description The goto statement shall not be used.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity error
8+
* @tags external/misra/id/rule-15-1
9+
* correctness
10+
* security
11+
* external/misra/obligation/advisory
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.misra
16+
17+
from Stmt s
18+
where
19+
not isExcluded(s, Statements6Package::gotoStatementUsedQuery()) and
20+
(s instanceof GotoStmt or s instanceof ComputedGotoStmt)
21+
select s, "Use of goto."
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| test.c:4:3:4:14 | goto ... | Use of goto. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-15-1/GotoStatementUsed.ql

c/misra/test/rules/RULE-15-1/test.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
void test_goto() {
2+
int x = 1;
3+
4+
goto label1; // NON_COMPLIANT
5+
6+
label1:
7+
8+
x = 2;
9+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import Statements2
5151
import Statements3
5252
import Statements4
5353
import Statements5
54+
import Statements6
5455
import Strings1
5556
import Strings2
5657
import Strings3
@@ -107,6 +108,7 @@ newtype TCQuery =
107108
TStatements3PackageQuery(Statements3Query q) or
108109
TStatements4PackageQuery(Statements4Query q) or
109110
TStatements5PackageQuery(Statements5Query q) or
111+
TStatements6PackageQuery(Statements6Query q) or
110112
TStrings1PackageQuery(Strings1Query q) or
111113
TStrings2PackageQuery(Strings2Query q) or
112114
TStrings3PackageQuery(Strings3Query q) or
@@ -163,6 +165,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
163165
isStatements3QueryMetadata(query, queryId, ruleId, category) or
164166
isStatements4QueryMetadata(query, queryId, ruleId, category) or
165167
isStatements5QueryMetadata(query, queryId, ruleId, category) or
168+
isStatements6QueryMetadata(query, queryId, ruleId, category) or
166169
isStrings1QueryMetadata(query, queryId, ruleId, category) or
167170
isStrings2QueryMetadata(query, queryId, ruleId, category) or
168171
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 Statements6Query = TGotoStatementUsedQuery()
7+
8+
predicate isStatements6QueryMetadata(Query query, string queryId, string ruleId, string category) {
9+
query =
10+
// `Query` instance for the `gotoStatementUsed` query
11+
Statements6Package::gotoStatementUsedQuery() and
12+
queryId =
13+
// `@id` for the `gotoStatementUsed` query
14+
"c/misra/goto-statement-used" and
15+
ruleId = "RULE-15-1" and
16+
category = "advisory"
17+
}
18+
19+
module Statements6Package {
20+
Query gotoStatementUsedQuery() {
21+
//autogenerate `Query` type
22+
result =
23+
// `Query` type for `gotoStatementUsed` query
24+
TQueryC(TStatements6PackageQuery(TGotoStatementUsedQuery()))
25+
}
26+
}

rule_packages/c/Statements6.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"MISRA-C-2012": {
3+
"RULE-15-1": {
4+
"properties": {
5+
"obligation": "advisory"
6+
},
7+
"queries": [
8+
{
9+
"description": "The goto statement shall not be used.",
10+
"kind": "problem",
11+
"name": "The goto statement should not be used",
12+
"precision": "very-high",
13+
"severity": "error",
14+
"short_name": "GotoStatementUsed",
15+
"tags": [
16+
"correctness",
17+
"security"
18+
]
19+
}
20+
],
21+
"title": "The goto statement should not be used"
22+
}
23+
}
24+
}

rules.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ c,MISRA-C-2012,RULE-14-1,Yes,Required,,,A loop counter shall not have essentiall
697697
c,MISRA-C-2012,RULE-14-2,Yes,Required,,,A for loop shall be well-formed,M6-5-1...M6-5-6,Statements4,Medium,
698698
c,MISRA-C-2012,RULE-14-3,Yes,Required,,,Controlling expressions shall not be invariant,,Statements5,Medium,
699699
c,MISRA-C-2012,RULE-14-4,Yes,Required,,,The controlling expression of an if statement and the controlling expression of an iteration-statement shall have essentially Boolean type,A5-0-2,Statements4,Medium,
700-
c,MISRA-C-2012,RULE-15-1,No,Advisory,,,The goto statement should not be used,A6-6-1,,Import,
700+
c,MISRA-C-2012,RULE-15-1,Yes,Advisory,,,The goto statement should not be used,A6-6-1,Statements6,Import,
701701
c,MISRA-C-2012,RULE-15-2,Yes,Required,,,The goto statement shall jump to a label declared later in the same function,M6-6-2,Statements2,Import,
702702
c,MISRA-C-2012,RULE-15-3,Yes,Required,,,"Any label referenced by a goto statement shall be declared in the same block, or in any block enclosing the goto statement",M6-6-1,Statements2,Import,
703703
c,MISRA-C-2012,RULE-15-4,Yes,Advisory,,,There should be no more than one break or goto statement used to terminate any iteration statement,,Statements2,Medium,

0 commit comments

Comments
 (0)