Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions change_notes/2024-05-22-fix-fp-rule-A18-5-8.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `A18-5-8` - `UnnecessaryUseOfDynamicStorage.ql`:
- Address FP reported in #20. Add model of flow from MakeSharedOrUnique to return expression to capture copy/move elision case NRVO.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class MakeSharedOrUnique extends FunctionCall, CandidateFunctionLocalHeapAllocat
// This includes the case where a result of `make_shared` or `make_unique` is return by a function
// because the compiler will call the appropriate constructor.
not exists(FunctionCall fc | DataFlow::localExprFlow(this, fc.getAnArgument())) and
// The flow to a return statement is explicitly modelled for the case where
// the copy/move constructor is elided and therefore there is no actual function call in the database
not exists(ReturnStmt ret | DataFlow::localExprFlow(this, ret.getExpr())) and
// Not assigned to a field
not exists(Field f | DataFlow::localExprFlow(this, f.getAnAssignedValue()))
}
Expand Down
9 changes: 9 additions & 0 deletions cpp/autosar/test/rules/A18-5-8/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,13 @@ StructA *test_failure() {
a = nullptr;
}
return a;
}

#include <string>
std::unique_ptr<StructA>
test_for_fp_reported_in_20(const std::string &s) noexcept {
// make_unique performs heap allocation
// but this outlives the function due to copy elision
// (specifically NRVO)
return std::make_unique<StructA>(s); // COMPLIANT
}