From 6c82fb05a4329efe18b204210b49be95049bf734 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Tue, 12 Apr 2022 01:09:13 -0500 Subject: [PATCH] Fix 10954: False positive: returnDanglingLifetime when using c_str() (#4006) --- lib/valueflow.cpp | 12 +++++------- test/testautovariables.cpp | 6 ++++++ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index d070bab94..9a12f4772 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -3441,13 +3441,11 @@ bool isLifetimeBorrowed(const Token *tok, const Settings *settings) const Token* parent = nullptr; const ValueType* vt = tok->valueType(); std::vector vtParents = getParentValueTypes(tok, settings, &parent); - if (vt) { - for (const ValueType& vtParent : vtParents) { - if (isLifetimeBorrowed(vt, &vtParent)) - return true; - if (isLifetimeOwned(vt, &vtParent)) - return false; - } + for (const ValueType& vtParent : vtParents) { + if (isLifetimeBorrowed(vt, &vtParent)) + return true; + if (isLifetimeOwned(vt, &vtParent)) + return false; } if (parent) { if (isDifferentType(tok, parent)) diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 5f3cbb728..63771346d 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -2573,6 +2573,12 @@ private: " h = 1;\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("std::string f(std::string s) {\n" + " std::string ss = (\":\" + s).c_str();\n" + " return ss;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void danglingLifetimeContainerView()