From 4626f9ed763eec7cf7e6591ee656e42dd6be10ee Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Wed, 4 Aug 2021 01:16:31 -0500 Subject: [PATCH] Fix FP when returning a pointer to a container (#3379) --- lib/valueflow.cpp | 3 ++- test/testautovariables.cpp | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 20521adab..a285a5945 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -2838,7 +2838,8 @@ static std::vector getLifetimeTokens(const Token* tok, if (!vartok) return {{tok, std::move(errorPath)}}; const Variable *tokvar = vartok->variable(); - if (!isUniqueSmartPointer(vartok) && !astIsContainer(vartok) && + const bool isContainer = astIsContainer(vartok) && !astIsPointer(vartok); + if (!isUniqueSmartPointer(vartok) && !isContainer && !(tokvar && tokvar->isArray() && !tokvar->isArgument()) && (Token::Match(vartok->astParent(), "[|*") || vartok->astParent()->originalName() == "->")) { for (const ValueFlow::Value &v : vartok->values()) { diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 977d25453..6b70f08df 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -118,6 +118,7 @@ private: TEST_CASE(returnReference20); // #9536 TEST_CASE(returnReference21); // #9530 TEST_CASE(returnReference22); + TEST_CASE(returnReference23); TEST_CASE(returnReferenceFunction); TEST_CASE(returnReferenceContainer); TEST_CASE(returnReferenceLiteral); @@ -1451,6 +1452,14 @@ private: ASSERT_EQUALS("[test.cpp:4]: (error) Reference to local variable returned.\n", errout.str()); } + void returnReference23() { + check("const std::vector * g();\n" + "const std::vector& f() {\n" + " return *g();\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void returnReferenceFunction() { check("int& f(int& a) {\n" " return a;\n"