Skip to content

Commit 1c610ee

Browse files
committed
Add shared query for RULE-6-8-3
1 parent d0f2ede commit 1c610ee

File tree

7 files changed

+104
-4
lines changed

7 files changed

+104
-4
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/
2+
import cpp
3+
import RuleMetadata
4+
import codingstandards.cpp.exclusions.RuleMetadata
5+
6+
newtype LifetimeQuery =
7+
TValueOfAnObjectMustNotBeReadBeforeItHasBeenSetQuery() or
8+
TAutomaticStorageAssignedToObjectGreaterLifetimeQuery()
9+
10+
predicate isLifetimeQueryMetadata(Query query, string queryId, string ruleId, string category) {
11+
query =
12+
// `Query` instance for the `valueOfAnObjectMustNotBeReadBeforeItHasBeenSet` query
13+
LifetimePackage::valueOfAnObjectMustNotBeReadBeforeItHasBeenSetQuery() and
14+
queryId =
15+
// `@id` for the `valueOfAnObjectMustNotBeReadBeforeItHasBeenSet` query
16+
"cpp/misra/value-of-an-object-must-not-be-read-before-it-has-been-set" and
17+
ruleId = "RULE-11-6-2" and
18+
category = "mandatory"
19+
or
20+
query =
21+
// `Query` instance for the `automaticStorageAssignedToObjectGreaterLifetime` query
22+
LifetimePackage::automaticStorageAssignedToObjectGreaterLifetimeQuery() and
23+
queryId =
24+
// `@id` for the `automaticStorageAssignedToObjectGreaterLifetime` query
25+
"cpp/misra/automatic-storage-assigned-to-object-greater-lifetime" and
26+
ruleId = "RULE-6-8-3" and
27+
category = "required"
28+
}
29+
30+
module LifetimePackage {
31+
Query valueOfAnObjectMustNotBeReadBeforeItHasBeenSetQuery() {
32+
//autogenerate `Query` type
33+
result =
34+
// `Query` type for `valueOfAnObjectMustNotBeReadBeforeItHasBeenSet` query
35+
TQueryCPP(TLifetimePackageQuery(TValueOfAnObjectMustNotBeReadBeforeItHasBeenSetQuery()))
36+
}
37+
38+
Query automaticStorageAssignedToObjectGreaterLifetimeQuery() {
39+
//autogenerate `Query` type
40+
result =
41+
// `Query` type for `automaticStorageAssignedToObjectGreaterLifetime` query
42+
TQueryCPP(TLifetimePackageQuery(TAutomaticStorageAssignedToObjectGreaterLifetimeQuery()))
43+
}
44+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import IntegerConversion
4545
import Invariants
4646
import Iterators
4747
import Lambdas
48+
import Lifetime
4849
import Linkage1
4950
import Linkage2
5051
import Literals
@@ -132,6 +133,7 @@ newtype TCPPQuery =
132133
TInvariantsPackageQuery(InvariantsQuery q) or
133134
TIteratorsPackageQuery(IteratorsQuery q) or
134135
TLambdasPackageQuery(LambdasQuery q) or
136+
TLifetimePackageQuery(LifetimeQuery q) or
135137
TLinkage1PackageQuery(Linkage1Query q) or
136138
TLinkage2PackageQuery(Linkage2Query q) or
137139
TLiteralsPackageQuery(LiteralsQuery q) or
@@ -219,6 +221,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
219221
isInvariantsQueryMetadata(query, queryId, ruleId, category) or
220222
isIteratorsQueryMetadata(query, queryId, ruleId, category) or
221223
isLambdasQueryMetadata(query, queryId, ruleId, category) or
224+
isLifetimeQueryMetadata(query, queryId, ruleId, category) or
222225
isLinkage1QueryMetadata(query, queryId, ruleId, category) or
223226
isLinkage2QueryMetadata(query, queryId, ruleId, category) or
224227
isLiteralsQueryMetadata(query, queryId, ruleId, category) or

cpp/common/test/rules/donotcopyaddressofautostorageobjecttootherobject/DoNotCopyAddressOfAutoStorageObjectToOtherObject.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,5 @@
6262
| stack_escapes_test.cpp:367:3:367:17 | ... = ... | A stack address ($@) may be assigned to a non-local variable. | stack_escapes_test.cpp:367:17:367:17 | x | source |
6363
| stack_escapes_test.cpp:368:3:368:20 | ... = ... | A stack address ($@) may be assigned to a non-local variable. | stack_escapes_test.cpp:368:20:368:20 | x | source |
6464
| test.cpp:7:5:7:10 | ... = ... | A stack address ($@) may be assigned to a non-local variable. | test.cpp:7:10:7:10 | c | source |
65+
| test.cpp:15:5:15:11 | ... = ... | A stack address ($@) may be assigned to a non-local variable. | test.cpp:15:9:15:11 | l_a | source |
66+
| test.cpp:26:5:26:10 | ... = ... | A stack address ($@) may be assigned to a non-local variable. | test.cpp:25:11:25:11 | l | source |

cpp/common/test/rules/donotcopyaddressofautostorageobjecttootherobject/test.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,23 @@ void test_simple_aliasing() {
66
int c;
77
a = &c; // NON_COMPLIANT - different scope
88
}
9+
}
10+
11+
void extra_test_simple_aliasing() {
12+
int *p;
13+
{
14+
int l_a[1];
15+
p = l_a; // NON_COMPLIANT
16+
}
17+
}
18+
19+
void extra_test2_simple_aliasing() {
20+
int *p;
21+
{
22+
int *p2 = nullptr;
23+
int l;
24+
25+
p2 = &l; // COMPLIANT
26+
p = p2; // NON_COMPLIANT
27+
}
928
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @id cpp/misra/automatic-storage-assigned-to-object-greater-lifetime
3+
* @name RULE-6-8-3: Declare objects with appropriate storage durations
4+
* @description When storage durations are not compatible between assigned pointers it can lead to
5+
* referring to objects outside of their lifetime, which is undefined behaviour.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-6-8-3
10+
* correctness
11+
* security
12+
* scope/single-translation-unit
13+
* external/misra/enforcement/decidable
14+
* external/misra/obligation/required
15+
*/
16+
17+
import cpp
18+
import codingstandards.cpp.misra
19+
import codingstandards.cpp.rules.donotcopyaddressofautostorageobjecttootherobject.DoNotCopyAddressOfAutoStorageObjectToOtherObject
20+
21+
module AutomaticStorageAssignedToObjectGreaterLifetimeConfig implements DoNotCopyAddressOfAutoStorageObjectToOtherObjectConfigSig {
22+
Query getQuery() { result = LifetimePackage::automaticStorageAssignedToObjectGreaterLifetimeQuery() }
23+
}
24+
25+
import DoNotCopyAddressOfAutoStorageObjectToOtherObject<AutomaticStorageAssignedToObjectGreaterLifetimeConfig>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cpp/common/test/rules/donotcopyaddressofautostorageobjecttootherobject/DoNotCopyAddressOfAutoStorageObjectToOtherObject.ql

rule_packages/cpp/Lifetime.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,21 @@
3030
},
3131
"queries": [
3232
{
33-
"description": "An assignment operator shall not assign the address of an object with automatic storage duration to an object with a greater lifetime",
33+
"description": "When storage durations are not compatible between assigned pointers it can lead to referring to objects outside of their lifetime, which is undefined behaviour.",
3434
"kind": "problem",
35-
"name": "An assignment operator shall not assign the address of an object with automatic storage duration to",
35+
"name": "Declare objects with appropriate storage durations",
3636
"precision": "very-high",
3737
"severity": "error",
38-
"short_name": "AssignmentOperatorAssignTheAddressOfAnObjectWithAutomaticStorageDurationToAnObjectWithAGreaterLifetime",
38+
"short_name": "AutomaticStorageAssignedToObjectGreaterLifetime",
39+
"shared_implementation_short_name": "DoNotCopyAddressOfAutoStorageObjectToOtherObject",
3940
"tags": [
41+
"correctness",
42+
"security",
4043
"scope/single-translation-unit"
41-
]
44+
],
45+
"implementation_scope": {
46+
"description": "The rule checks specifically for pointers to objects with automatic storage duration that are assigned to static storage duration variables."
47+
}
4248
}
4349
],
4450
"title": "An assignment operator shall not assign the address of an object with automatic storage duration to an object with a greater lifetime"

0 commit comments

Comments
 (0)