Fix #10151 FP eraseDereference when returning iterator from loop (#3787)

This commit is contained in:
chrchr-github 2022-02-03 21:13:48 +01:00 committed by GitHub
parent 1798b73808
commit 17b538210d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 1 deletions

View File

@ -1370,7 +1370,7 @@ void CheckStl::eraseCheckLoopVar(const Scope &scope, const Variable *var)
// Vector erases are handled by invalidContainer check // Vector erases are handled by invalidContainer check
if (isVector(tok->tokAt(-3))) if (isVector(tok->tokAt(-3)))
continue; continue;
if (Token::simpleMatch(tok->astParent(), "=")) if (Token::Match(tok->astParent(), "=|return"))
continue; continue;
// Iterator is invalid.. // Iterator is invalid..
int indentlevel = 0U; int indentlevel = 0U;

View File

@ -4285,6 +4285,15 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:9]: (error) Out of bounds access in expression 'e[d].begin()' because 'e[d]' is empty.\n", ASSERT_EQUALS("[test.cpp:9]: (error) Out of bounds access in expression 'e[d].begin()' because 'e[d]' is empty.\n",
errout.str()); errout.str());
// #10151
check("std::set<int>::iterator f(std::set<int>& s) {\n"
"for (auto it = s.begin(); it != s.end(); ++it)\n"
" if (*it == 42)\n"
" return s.erase(it);\n"
" return s.end();\n"
"}\n");
ASSERT_EQUALS("", errout.str());
} }
void dereferenceInvalidIterator2() { void dereferenceInvalidIterator2() {