From 03cefd5d7035aaaa019aca3b64254b9a1fd4e00c Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Wed, 2 Sep 2020 13:01:08 -0500 Subject: [PATCH] Fix issue 9853: False positive: returnReference when using a pointer to container (#2765) --- lib/valueflow.cpp | 2 +- test/testautovariables.cpp | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 86cc5c01b..746ed1f35 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -3063,7 +3063,7 @@ std::vector getLifetimeTokens(const Token* tok, ValueFlow::Value: } } return result; - } else if (Token::Match(tok->tokAt(-2), ". %name% (") && astIsContainer(tok->tokAt(-2)->astOperand1())) { + } else if (Token::Match(tok->tokAt(-2), ". %name% (") && tok->tokAt(-2)->originalName() != "->" && astIsContainer(tok->tokAt(-2)->astOperand1())) { const Library::Container* library = getLibraryContainer(tok->tokAt(-2)->astOperand1()); Library::Container::Yield y = library->getYield(tok->previous()->str()); if (y == Library::Container::Yield::AT_INDEX || y == Library::Container::Yield::ITEM) { diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 7d5320c88..059d15064 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -2143,6 +2143,13 @@ private: "}\n"); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:3] -> [test.cpp:2] -> [test.cpp:3]: (error) Returning object that points to local variable 'a' that will be invalid when returning.\n", errout.str()); + check("std::vector* g();\n" + "int& f() {\n" + " auto* p = g();\n" + " return p->front();\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + check("std::vector> g();\n" "void f() {\n" " for(auto& x:g())\n"