From 5cb6c281559e4d0192a179270ac65ff5bd54fbc0 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 11 Apr 2024 19:30:49 +0200 Subject: [PATCH 1/2] Update valueflow.cpp --- lib/valueflow.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index ffdb189d012..dcb0eb710bc 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -3665,10 +3665,14 @@ static std::vector getLifetimeTokens(const Token* tok, const Token *vartok = tok; while (vartok) { - if (vartok->str() == "[" || vartok->originalName() == "->" || vartok->isUnaryOp("*")) - vartok = vartok->astOperand1(); - else if (vartok->str() == ".") + if (vartok->str() == "[" || vartok->isUnaryOp("*")) vartok = vartok->astOperand1(); + else if (vartok->str() == ".") { + if (vartok->originalName().empty() || !Token::simpleMatch(vartok->astOperand1(), ".")) + vartok = vartok->astOperand1(); + else + break; + } else if (vartok->str() == "::") vartok = vartok->astOperand2(); else From b81efa099bfb7a745c80189fe5bd2504a79982c0 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 11 Apr 2024 19:32:10 +0200 Subject: [PATCH 2/2] Update testautovariables.cpp --- test/testautovariables.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index a676772cd3d..2d0129a3317 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -115,6 +115,7 @@ class TestAutoVariables : public TestFixture { TEST_CASE(returnReference24); // #10098 TEST_CASE(returnReference25); // #10983 TEST_CASE(returnReference26); + TEST_CASE(returnReference27); TEST_CASE(returnReferenceFunction); TEST_CASE(returnReferenceContainer); TEST_CASE(returnReferenceLiteral); @@ -1656,6 +1657,18 @@ class TestAutoVariables : public TestFixture { ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (error) Reference to local variable returned.\n", errout_str()); } + void returnReference27() + { + check("struct S {\n" // #12607 + " const std::string & f(const std::string& a, const std::string& b) {\n" + " auto status = m.emplace(a, b);\n" + " return status.first->second;\n" + " }\n" + " std::map m;\n" + "};\n"); + ASSERT_EQUALS("", errout_str()); + } + void returnReferenceFunction() { check("int& f(int& a) {\n" " return a;\n"