Fix 10716: Crash in CheckStl::checkDereferenceInvalidIterator2 (#3709)

* Fix 10716: Crash in CheckStl::checkDereferenceInvalidIterator2

* Format
This commit is contained in:
Paul Fultz II 2022-01-14 16:50:33 -06:00 committed by GitHub
parent 781a145680
commit 2b6a89e30e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View File

@ -2329,7 +2329,7 @@ void CheckStl::checkDereferenceInvalidIterator2()
inconclusive = true; inconclusive = true;
} }
if (cValue) { if (cValue) {
const ValueFlow::Value& lValue = getLifetimeObjValue(tok, true); const ValueFlow::Value& lValue = getLifetimeIteratorValue(tok);
if (emptyAdvance) if (emptyAdvance)
outOfBoundsError(emptyAdvance, outOfBoundsError(emptyAdvance,
lValue.tokvalue->expressionString(), lValue.tokvalue->expressionString(),

View File

@ -4243,6 +4243,20 @@ private:
" return *(v.begin() + v.size() - 1);\n" " return *(v.begin() + v.size() - 1);\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// #10716
check("struct a;\n"
"class b {\n"
" void c(std::map<std::string, a *> &);\n"
" std::string d;\n"
" std::map<std::string, std::set<std::string>> e;\n"
"};\n"
"void b::c(std::map<std::string, a *> &) {\n"
" e.clear();\n"
" auto f = *e[d].begin();\n"
"}\n");
ASSERT_EQUALS("[test.cpp:9]: (error) Out of bounds access in expression 'e[d].begin()' because 'e[d]' is empty.\n",
errout.str());
} }
void dereferenceInvalidIterator2() { void dereferenceInvalidIterator2() {