Skip to content

Commit 7644032

Browse files
authored
Merge pull request #596 from knewbury01/knewbury01/fix-20
A18-5-8: address fp report
2 parents 1eb6ed7 + c2b32b0 commit 7644032

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

Diff for: change_notes/2024-05-22-fix-fp-rule-A18-5-8.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `A18-5-8` - `UnnecessaryUseOfDynamicStorage.ql`:
2+
- Address FP reported in #20. Add model of flow from MakeSharedOrUnique to return expression to capture copy/move elision case NRVO.

Diff for: cpp/autosar/src/rules/A18-5-8/UnnecessaryUseOfDynamicStorage.ql

+3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ class MakeSharedOrUnique extends FunctionCall, CandidateFunctionLocalHeapAllocat
5353
// This includes the case where a result of `make_shared` or `make_unique` is return by a function
5454
// because the compiler will call the appropriate constructor.
5555
not exists(FunctionCall fc | DataFlow::localExprFlow(this, fc.getAnArgument())) and
56+
// The flow to a return statement is explicitly modelled for the case where
57+
// the copy/move constructor is elided and therefore there is no actual function call in the database
58+
not exists(ReturnStmt ret | DataFlow::localExprFlow(this, ret.getExpr())) and
5659
// Not assigned to a field
5760
not exists(Field f | DataFlow::localExprFlow(this, f.getAnAssignedValue()))
5861
}

Diff for: cpp/autosar/test/rules/A18-5-8/test.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,13 @@ StructA *test_failure() {
6868
a = nullptr;
6969
}
7070
return a;
71+
}
72+
73+
#include <string>
74+
std::unique_ptr<StructA>
75+
test_for_fp_reported_in_20(const std::string &s) noexcept {
76+
// make_unique performs heap allocation
77+
// but this outlives the function due to copy elision
78+
// (specifically NRVO)
79+
return std::make_unique<StructA>(s); // COMPLIANT
7180
}

0 commit comments

Comments
 (0)