From 60b670babdbb2a01cd4b89d6e668c4bbac96fc2f Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Tue, 23 Jul 2019 14:59:05 -0500 Subject: [PATCH] Fix issue 9219: False positive, returnDanglingLifetime (#2026) * Check for pointer deref for container methods * Formatting --- lib/valueflow.cpp | 4 +++- test/testautovariables.cpp | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 381bd0e57..7f09418f0 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -3371,7 +3371,9 @@ static void valueFlowLifetime(TokenList *tokenlist, SymbolDatabase*, ErrorLogger valueFlowForwardLifetime(tok, tokenlist, errorLogger, settings); } // container lifetimes - else if (tok->variable() && Token::Match(tok, "%var% . begin|cbegin|rbegin|crbegin|end|cend|rend|crend|data|c_str|find|insert (")) { + else if (tok->variable() && + Token::Match(tok, "%var% . begin|cbegin|rbegin|crbegin|end|cend|rend|crend|data|c_str|find|insert (") && + tok->next()->originalName() != "->") { if (Token::simpleMatch(tok->tokAt(2), "find") && !astIsIterator(tok->tokAt(3))) continue; ErrorPath errorPath; diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 56631d27e..be3886dd0 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -1757,6 +1757,13 @@ private: " return y.find(x);\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("std::string* f();\n" + "const char* g() {\n" + " std::string* var = f();\n" + " return var->c_str();\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void danglingLifetime() {