diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 6ecb832a6..1f9e763d7 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -100,7 +100,7 @@ void CheckStl::iterators() // Scan through the rest of the code and see if the iterator is // used against other containers. - for (const Token *tok2 = var->nameToken(); tok2 != var->scope()->classEnd; tok2 = tok2->next()) { + for (const Token *tok2 = var->nameToken(); tok2 && tok2 != var->scope()->classEnd; tok2 = tok2->next()) { if (invalidationScope && tok2 == invalidationScope->classEnd) validIterator = true; // Assume that the iterator becomes valid again if (containerAssignScope && tok2 == containerAssignScope->classEnd) @@ -208,7 +208,7 @@ void CheckStl::iterators() // bailout handling. Assume that the iterator becomes valid if we see else. // TODO: better handling - else if (tok2->str() == "else") { + else if (tok2 && tok2->str() == "else") { validIterator = true; } } diff --git a/test/teststl.cpp b/test/teststl.cpp index 22ec6bb18..3fd783371 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -45,6 +45,7 @@ private: TEST_CASE(iterator11); TEST_CASE(iterator12); TEST_CASE(iterator13); + TEST_CASE(iterator14); // #5598 invalid code causing a crash TEST_CASE(dereference); TEST_CASE(dereference_break); // #3644 - handle "break" @@ -440,6 +441,11 @@ private: ASSERT_EQUALS("", errout.str()); } + void iterator14() { + check(" { { void foo() { struct }; template struct S { Used x; void bar() } auto f = [this] { }; } };"); + ASSERT_EQUALS("", errout.str()); + } + // Dereferencing invalid pointer void dereference() { check("void f()\n"