Skip to content

Commit 78a2db9

Browse files
Merge remote-tracking branch 'origin/michaelrfairhurst/package-undefined-behavior' into michaelrfairhurst/package-undefined-behavior-divide-or-modulo-by-zero
2 parents f5f59c7 + 7e4819f commit 78a2db9

File tree

30 files changed

+302
-26
lines changed

30 files changed

+302
-26
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `A2-13-4` - `StringLiteralsAssignedToNonConstantPointers.ql`:
2+
- Refactored query logic into a shared module (`StringLiteralsAssignedToNonConstantPointersShared`) to enable reuse by MISRA C++ `RULE-4-1-3`. The query logic is unchanged. No visible changes to results or performance are expected.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `MEM51-CPP` - `ProperlyDeallocateDynamicallyAllocatedResources.ql`:
2+
- Refactored query logic into a shared library (`ProperlyDeallocateDynamicallyAllocatedResourcesShared.qll`) to enable reuse by MISRA C++ `RULE-4-1-3`. The query logic is unchanged and no visible changes to results or performance are expected.

cpp/autosar/src/rules/A2-13-4/StringLiteralsAssignedToNonConstantPointers.ql

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717

1818
import cpp
1919
import codingstandards.cpp.autosar
20+
import codingstandards.cpp.rules.stringliteralsassignedtononconstantpointersshared.StringLiteralsAssignedToNonConstantPointersShared
2021

21-
from ArrayToPointerConversion apc
22-
where
23-
not isExcluded(apc, StringsPackage::stringLiteralsAssignedToNonConstantPointersQuery()) and
24-
apc.getExpr() instanceof StringLiteral and
25-
apc.getExpr().getUnderlyingType().(ArrayType).getBaseType().isConst() and
26-
not apc.getFullyConverted().getType().getUnderlyingType().(PointerType).getBaseType().isConst()
27-
select apc, "String literal assigned to non-const pointer."
22+
module StringLiteralsAssignedToNonConstantPointersConfig implements
23+
StringLiteralsAssignedToNonConstantPointersSharedConfigSig
24+
{
25+
Query getQuery() { result = StringsPackage::stringLiteralsAssignedToNonConstantPointersQuery() }
26+
}
27+
28+
import StringLiteralsAssignedToNonConstantPointersShared<StringLiteralsAssignedToNonConstantPointersConfig>

cpp/autosar/test/rules/A2-13-4/StringLiteralsAssignedToNonConstantPointers.qlref

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cpp/common/test/rules/stringliteralsassignedtononconstantpointersshared/StringLiteralsAssignedToNonConstantPointersShared.ql

cpp/cert/src/rules/MEM51-CPP/ProperlyDeallocateDynamicallyAllocatedResources.ql

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,14 @@
1919

2020
import cpp
2121
import codingstandards.cpp.cert
22-
import codingstandards.cpp.Allocations
22+
import codingstandards.cpp.rules.properlydeallocatedynamicallyallocatedresourcesshared.ProperlyDeallocateDynamicallyAllocatedResourcesShared
2323

24-
predicate matching(string allocKind, string deleteKind) {
25-
allocKind = "new" and deleteKind = "delete"
26-
or
27-
allocKind = "new[]" and deleteKind = "delete[]"
28-
or
29-
allocKind = "malloc" and deleteKind = "free"
24+
module ProperlyDeallocateDynamicallyAllocatedResourcesConfig implements
25+
ProperlyDeallocateDynamicallyAllocatedResourcesSharedConfigSig
26+
{
27+
Query getQuery() {
28+
result = AllocationsPackage::properlyDeallocateDynamicallyAllocatedResourcesQuery()
29+
}
3030
}
3131

32-
from Expr alloc, Expr free, Expr freed, string allocKind, string deleteKind
33-
where
34-
not isExcluded(freed, AllocationsPackage::properlyDeallocateDynamicallyAllocatedResourcesQuery()) and
35-
allocReaches(freed, alloc, allocKind) and
36-
freeExprOrIndirect(free, freed, deleteKind) and
37-
not matching(allocKind, deleteKind)
38-
select free, "Memory allocated with $@ but deleted with " + deleteKind + ".", alloc, allocKind
32+
import ProperlyDeallocateDynamicallyAllocatedResourcesShared<ProperlyDeallocateDynamicallyAllocatedResourcesConfig>

cpp/cert/test/rules/MEM51-CPP/ProperlyDeallocateDynamicallyAllocatedResources.qlref

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cpp/common/test/rules/properlydeallocatedynamicallyallocatedresourcesshared/ProperlyDeallocateDynamicallyAllocatedResourcesShared.ql

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ newtype UndefinedQuery =
99
TUndefinedBehaviorAuditQuery() or
1010
TCriticalUnspecifiedBehaviorAuditQuery() or
1111
TPossibleDataRaceBetweenThreadsQuery() or
12-
TDivisionByZeroUndefinedBehaviorQuery()
12+
TDivisionByZeroUndefinedBehaviorQuery() or
13+
TDeallocationTypeMismatchQuery() or
14+
TStringLiteralPossiblyModifiedAuditQuery()
1315

1416
predicate isUndefinedQueryMetadata(Query query, string queryId, string ruleId, string category) {
1517
query =
@@ -65,6 +67,24 @@ predicate isUndefinedQueryMetadata(Query query, string queryId, string ruleId, s
6567
"cpp/misra/division-by-zero-undefined-behavior" and
6668
ruleId = "RULE-4-1-3" and
6769
category = "required"
70+
or
71+
query =
72+
// `Query` instance for the `deallocationTypeMismatch` query
73+
UndefinedPackage::deallocationTypeMismatchQuery() and
74+
queryId =
75+
// `@id` for the `deallocationTypeMismatch` query
76+
"cpp/misra/deallocation-type-mismatch" and
77+
ruleId = "RULE-4-1-3" and
78+
category = "required"
79+
or
80+
query =
81+
// `Query` instance for the `stringLiteralPossiblyModifiedAudit` query
82+
UndefinedPackage::stringLiteralPossiblyModifiedAuditQuery() and
83+
queryId =
84+
// `@id` for the `stringLiteralPossiblyModifiedAudit` query
85+
"cpp/misra/string-literal-possibly-modified-audit" and
86+
ruleId = "RULE-4-1-3" and
87+
category = "required"
6888
}
6989

7090
module UndefinedPackage {
@@ -109,4 +129,18 @@ module UndefinedPackage {
109129
// `Query` type for `divisionByZeroUndefinedBehavior` query
110130
TQueryCPP(TUndefinedPackageQuery(TDivisionByZeroUndefinedBehaviorQuery()))
111131
}
132+
133+
Query deallocationTypeMismatchQuery() {
134+
//autogenerate `Query` type
135+
result =
136+
// `Query` type for `deallocationTypeMismatch` query
137+
TQueryCPP(TUndefinedPackageQuery(TDeallocationTypeMismatchQuery()))
138+
}
139+
140+
Query stringLiteralPossiblyModifiedAuditQuery() {
141+
//autogenerate `Query` type
142+
result =
143+
// `Query` type for `stringLiteralPossiblyModifiedAudit` query
144+
TQueryCPP(TUndefinedPackageQuery(TStringLiteralPossiblyModifiedAuditQuery()))
145+
}
112146
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Provides a configurable module ProperlyDeallocateDynamicallyAllocatedResourcesShared with a
3+
* `problems` predicate for the following issue:
4+
* Deallocation functions should only be called on nullptr or a pointer returned by the
5+
* corresponding allocation function, that hasn't already been deallocated.
6+
*/
7+
8+
import cpp
9+
import codingstandards.cpp.Customizations
10+
import codingstandards.cpp.Exclusions
11+
import codingstandards.cpp.Allocations
12+
13+
signature module ProperlyDeallocateDynamicallyAllocatedResourcesSharedConfigSig {
14+
Query getQuery();
15+
}
16+
17+
module ProperlyDeallocateDynamicallyAllocatedResourcesShared<
18+
ProperlyDeallocateDynamicallyAllocatedResourcesSharedConfigSig Config>
19+
{
20+
private predicate matching(string allocKind, string deleteKind) {
21+
allocKind = "new" and deleteKind = "delete"
22+
or
23+
allocKind = "new[]" and deleteKind = "delete[]"
24+
or
25+
allocKind = "malloc" and deleteKind = "free"
26+
}
27+
28+
query predicate problems(Expr free, string message, Expr alloc, string allocKind) {
29+
exists(Expr freed, string deleteKind |
30+
not isExcluded(freed, Config::getQuery()) and
31+
allocReaches(freed, alloc, allocKind) and
32+
freeExprOrIndirect(free, freed, deleteKind) and
33+
not matching(allocKind, deleteKind) and
34+
message = "Memory allocated with $@ but deleted with " + deleteKind + "."
35+
)
36+
}
37+
}

0 commit comments

Comments
 (0)