From cd21f55cdf2d6107933012c119d1cab002ef6a0c Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Fri, 24 Mar 2023 07:29:43 -0500 Subject: [PATCH] Fix 11630 and 11620 lifetime issues (#4913) --- lib/valueflow.cpp | 11 +++++++++-- test/testautovariables.cpp | 8 ++++++++ test/teststl.cpp | 9 +++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 04458d84a..1f54779bb 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -4813,8 +4813,15 @@ static void valueFlowLifetime(TokenList *tokenlist, SymbolDatabase* /*db*/, Erro // container lifetimes else if (astIsContainer(tok)) { Token * parent = astParentSkipParens(tok); - if (!Token::Match(parent, ". %name% (") && - !Token::simpleMatch(parent, "(")) + if (!parent) + continue; + if (!Token::Match(parent, ". %name% (") && !Token::Match(parent->previous(), "%name% (")) + continue; + + // Skip if its a free function that doesnt yield an iterator to the container + if (Token::Match(parent->previous(), "%name% (") && + !contains({Library::Container::Yield::START_ITERATOR, Library::Container::Yield::END_ITERATOR}, + astFunctionYield(parent->previous(), settings))) continue; ValueFlow::Value master; diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index ef71b0c91..69495e517 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -3901,6 +3901,14 @@ private: " std::map m;\n" "};\n"); ASSERT_EQUALS("", errout.str()); + + // #11628 + check("std::vector* g();\n" + "void f() {\n" + " std::unique_ptr> p(g());\n" + " if (!p) {}\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void danglingLifetimeBorrowedMembers() diff --git a/test/teststl.cpp b/test/teststl.cpp index 4c48aa2b6..8b88ef26e 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -5873,6 +5873,15 @@ private: ASSERT_EQUALS( "[test.cpp:4]: (performance) Ineffective call of function 'substr' because a prefix of the string is assigned to itself. Use resize() or pop_back() instead.\n", errout.str()); + + // #11630 + check("int main(int argc, const char* argv[]) {\n" + " std::vector args(argv + 1, argv + argc);\n" + " args.push_back(\"-h\");\n" + " args.front();\n" + "}\n", + true); + ASSERT_EQUALS("", errout.str()); } void invalidContainerLoop() {