Skip to content

Commit 1371769

Browse files
authored
Merge branch 'main' into knewbury01/misracpp2023-declarations1
2 parents a4ca148 + 1487ba0 commit 1371769

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
@@ -46,6 +46,7 @@ import IntegerConversion
4646
import Invariants
4747
import Iterators
4848
import Lambdas
49+
import Lifetime
4950
import Linkage1
5051
import Linkage2
5152
import Literals
@@ -136,6 +137,7 @@ newtype TCPPQuery =
136137
TInvariantsPackageQuery(InvariantsQuery q) or
137138
TIteratorsPackageQuery(IteratorsQuery q) or
138139
TLambdasPackageQuery(LambdasQuery q) or
140+
TLifetimePackageQuery(LifetimeQuery q) or
139141
TLinkage1PackageQuery(Linkage1Query q) or
140142
TLinkage2PackageQuery(Linkage2Query q) or
141143
TLiteralsPackageQuery(LiteralsQuery q) or
@@ -226,6 +228,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
226228
isInvariantsQueryMetadata(query, queryId, ruleId, category) or
227229
isIteratorsQueryMetadata(query, queryId, ruleId, category) or
228230
isLambdasQueryMetadata(query, queryId, ruleId, category) or
231+
isLifetimeQueryMetadata(query, queryId, ruleId, category) or
229232
isLinkage1QueryMetadata(query, queryId, ruleId, category) or
230233
isLinkage2QueryMetadata(query, queryId, ruleId, category) or
231234
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)