Fix 11609: False positive: Returning iterator to local container 'k' that will be invalid when returning where 'k' is an iterator. (#4907)

This commit is contained in:
Paul Fultz II 2023-03-21 17:18:01 -05:00 committed by GitHub
parent d4b030694b
commit e1a4a18528
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 4 deletions

View File

@ -4824,12 +4824,14 @@ static void valueFlowLifetime(TokenList *tokenlist, SymbolDatabase* /*db*/, Erro
if (astIsIterator(parent->tokAt(2))) {
master.errorPath.emplace_back(parent->tokAt(2), "Iterator to container is created here.");
master.lifetimeKind = ValueFlow::Value::LifetimeKind::Iterator;
} else if (astIsIterator(parent)) {
} else if (astIsIterator(parent) && Token::Match(parent->previous(), "%name% (") &&
contains({Library::Container::Yield::START_ITERATOR, Library::Container::Yield::END_ITERATOR},
astFunctionYield(parent->previous(), settings))) {
master.errorPath.emplace_back(parent, "Iterator to container is created here.");
master.lifetimeKind = ValueFlow::Value::LifetimeKind::Iterator;
}
else if ((astIsPointer(parent->tokAt(2)) && !isContainerOfPointers(tok->valueType()->containerTypeToken, settings)) ||
Token::Match(parent->next(), "data|c_str")) {
} else if ((astIsPointer(parent->tokAt(2)) &&
!isContainerOfPointers(tok->valueType()->containerTypeToken, settings)) ||
Token::Match(parent->next(), "data|c_str")) {
master.errorPath.emplace_back(parent->tokAt(2), "Pointer to container is created here.");
master.lifetimeKind = ValueFlow::Value::LifetimeKind::Object;
} else {

View File

@ -3891,6 +3891,16 @@ private:
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// #11609
check("struct S {\n"
" void f(const std::string& s) {\n"
" auto it = m.find(s.substr(1,4));\n"
" if (it == m.end()) {}\n"
" }\n"
" std::map<std::string, int> m;\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
void danglingLifetimeBorrowedMembers()