diff --git a/change_notes/2024-05-22-fix-fp-rule-A18-5-8.md b/change_notes/2024-05-22-fix-fp-rule-A18-5-8.md new file mode 100644 index 0000000000..3091ce9ef3 --- /dev/null +++ b/change_notes/2024-05-22-fix-fp-rule-A18-5-8.md @@ -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. \ No newline at end of file diff --git a/cpp/autosar/src/rules/A18-5-8/UnnecessaryUseOfDynamicStorage.ql b/cpp/autosar/src/rules/A18-5-8/UnnecessaryUseOfDynamicStorage.ql index 979dc0824e..7b68030476 100644 --- a/cpp/autosar/src/rules/A18-5-8/UnnecessaryUseOfDynamicStorage.ql +++ b/cpp/autosar/src/rules/A18-5-8/UnnecessaryUseOfDynamicStorage.ql @@ -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())) } diff --git a/cpp/autosar/test/rules/A18-5-8/test.cpp b/cpp/autosar/test/rules/A18-5-8/test.cpp index fcaf482777..3183810942 100644 --- a/cpp/autosar/test/rules/A18-5-8/test.cpp +++ b/cpp/autosar/test/rules/A18-5-8/test.cpp @@ -68,4 +68,13 @@ StructA *test_failure() { a = nullptr; } return a; +} + +#include +std::unique_ptr +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(s); // COMPLIANT } \ No newline at end of file