From 3edb10a0064fcecce4f897d43870e0bd9e1b8e24 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Tue, 3 May 2022 04:42:32 -0500 Subject: [PATCH] Fix 11024: FP returnDanglingLifetime with c_str() passed to constructor (#4072) --- lib/valueflow.cpp | 2 ++ test/testautovariables.cpp | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 6a0b8a090..4ea28f2d3 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -3888,6 +3888,8 @@ static void valueFlowLifetimeConstructor(Token* tok, const Token* expr = tok2->astOperand2(); if (!var) continue; + if (!isLifetimeBorrowed(expr, settings)) + continue; const Variable* argvar = getLifetimeVariable(expr); if (var->isReference() || var->isRValueReference()) { if (argvar && argvar->isArgument() && (argvar->isReference() || argvar->isRValueReference())) { diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index f97034bc3..09c265f64 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -3269,6 +3269,17 @@ private: "}\n", true); ASSERT_EQUALS("", errout.str()); + + check("struct S {\n" + " std::string msg;\n" + " explicit S(const char* m) : msg(m) {}\n" + "};\n" + "S f() {\n" + " std::string s(\"abc\");\n" + " return S(s.c_str());\n" + "}\n", + true); + ASSERT_EQUALS("", errout.str()); } void danglingLifetimeAggegrateConstructor() {