From 2b6a89e30ebdba4f595afb23b0e514642a452944 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Fri, 14 Jan 2022 16:50:33 -0600 Subject: [PATCH] Fix 10716: Crash in CheckStl::checkDereferenceInvalidIterator2 (#3709) * Fix 10716: Crash in CheckStl::checkDereferenceInvalidIterator2 * Format --- lib/checkstl.cpp | 2 +- test/teststl.cpp | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 264ecab0d..b5bc660da 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -2329,7 +2329,7 @@ void CheckStl::checkDereferenceInvalidIterator2() inconclusive = true; } if (cValue) { - const ValueFlow::Value& lValue = getLifetimeObjValue(tok, true); + const ValueFlow::Value& lValue = getLifetimeIteratorValue(tok); if (emptyAdvance) outOfBoundsError(emptyAdvance, lValue.tokvalue->expressionString(), diff --git a/test/teststl.cpp b/test/teststl.cpp index 0d27837cd..4357c5049 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -4243,6 +4243,20 @@ private: " return *(v.begin() + v.size() - 1);\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + // #10716 + check("struct a;\n" + "class b {\n" + " void c(std::map &);\n" + " std::string d;\n" + " std::map> e;\n" + "};\n" + "void b::c(std::map &) {\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() {