Fix issue 9219: False positive, returnDanglingLifetime (#2026)

* Check for pointer deref for container methods

* Formatting
This commit is contained in:
Paul Fultz II 2019-07-23 14:59:05 -05:00 committed by Sebastian
parent 5ad5cfcc29
commit 60b670babd
2 changed files with 10 additions and 1 deletions

View File

@ -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;

View File

@ -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() {