Skip to content

Commit 1487ba0

Browse files
Merge pull request #1024 from knewbury01/knewbury01/Lifetime
Add lifetime package
2 parents 25403fc + 10f562a commit 1487ba0

File tree

17 files changed

+1055
-8
lines changed

17 files changed

+1055
-8
lines changed

.vscode/tasks.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@
259259
"Language1",
260260
"Language2",
261261
"Language3",
262+
"Lifetime",
262263
"Linkage1",
263264
"Linkage2",
264265
"Literals",
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `A8-5-0`, `EXP53-CPP`, `EXP33-C`, `RULE-9-1` - `MemoryNotInitializedBeforeItIsRead.ql`, `DoNotReadUninitializedMemory.ql`, `DoNotReadUninitializedMemory.ql`, `ObjectWithAutoStorageDurationReadBeforeInit.ql`:
2+
- The queries listed now find uses of the operator 'new' where there is no value initialization provided. The queries listed now also uses an out of the box library to consider initialization within another function as valid initialization (`InitializationFunctions.qll`). We do not yet track finely track the initialization/use of `p` vs `*p`.
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
@@ -134,6 +135,7 @@ newtype TCPPQuery =
134135
TInvariantsPackageQuery(InvariantsQuery q) or
135136
TIteratorsPackageQuery(IteratorsQuery q) or
136137
TLambdasPackageQuery(LambdasQuery q) or
138+
TLifetimePackageQuery(LifetimeQuery q) or
137139
TLinkage1PackageQuery(Linkage1Query q) or
138140
TLinkage2PackageQuery(Linkage2Query q) or
139141
TLiteralsPackageQuery(LiteralsQuery q) or
@@ -223,6 +225,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
223225
isInvariantsQueryMetadata(query, queryId, ruleId, category) or
224226
isIteratorsQueryMetadata(query, queryId, ruleId, category) or
225227
isLambdasQueryMetadata(query, queryId, ruleId, category) or
228+
isLifetimeQueryMetadata(query, queryId, ruleId, category) or
226229
isLinkage1QueryMetadata(query, queryId, ruleId, category) or
227230
isLinkage2QueryMetadata(query, queryId, ruleId, category) or
228231
isLiteralsQueryMetadata(query, queryId, ruleId, category) or

cpp/common/src/codingstandards/cpp/lifetimes/CppObjects.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ class AggregateLiteralObjectIdentity extends AggregateLiteral, ObjectIdentityBas
246246
}
247247

248248
/**
249-
* An object identified by a call to `malloc`.
249+
* An object identified by a call to `malloc` or allcoated with a `new` or `new[]` expression.
250250
*
251251
* Note: the malloc expression returns an address to this object, not the object itself. Therefore,
252252
* `getAnAccess()` returns cases where this malloc result is dereferenced, and not the malloc call
@@ -262,6 +262,8 @@ class AggregateLiteralObjectIdentity extends AggregateLiteral, ObjectIdentityBas
262262
class AllocatedObjectIdentity extends AllocationExpr, ObjectIdentityBase {
263263
AllocatedObjectIdentity() {
264264
this.(FunctionCall).getTarget().(AllocationFunction).requiresDealloc()
265+
or
266+
this = any(NewOrNewArrayExpr new | not exists(new.getPlacementPointer()))
265267
}
266268

267269
override StorageDuration getStorageDuration() { result.isAllocated() }

0 commit comments

Comments
 (0)