Invalid code cause SIGSEGV since loop variable tok2 was not checked properly

This commit is contained in:
Alexander Mai 2014-03-22 10:32:24 +01:00
parent fbd607d35d
commit e1c565357a
2 changed files with 8 additions and 2 deletions

View File

@ -100,7 +100,7 @@ void CheckStl::iterators()
// Scan through the rest of the code and see if the iterator is // Scan through the rest of the code and see if the iterator is
// used against other containers. // 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) if (invalidationScope && tok2 == invalidationScope->classEnd)
validIterator = true; // Assume that the iterator becomes valid again validIterator = true; // Assume that the iterator becomes valid again
if (containerAssignScope && tok2 == containerAssignScope->classEnd) if (containerAssignScope && tok2 == containerAssignScope->classEnd)
@ -208,7 +208,7 @@ void CheckStl::iterators()
// bailout handling. Assume that the iterator becomes valid if we see else. // bailout handling. Assume that the iterator becomes valid if we see else.
// TODO: better handling // TODO: better handling
else if (tok2->str() == "else") { else if (tok2 && tok2->str() == "else") {
validIterator = true; validIterator = true;
} }
} }

View File

@ -45,6 +45,7 @@ private:
TEST_CASE(iterator11); TEST_CASE(iterator11);
TEST_CASE(iterator12); TEST_CASE(iterator12);
TEST_CASE(iterator13); TEST_CASE(iterator13);
TEST_CASE(iterator14); // #5598 invalid code causing a crash
TEST_CASE(dereference); TEST_CASE(dereference);
TEST_CASE(dereference_break); // #3644 - handle "break" TEST_CASE(dereference_break); // #3644 - handle "break"
@ -440,6 +441,11 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void iterator14() {
check(" { { void foo() { struct }; template <typename> struct S { Used x; void bar() } auto f = [this] { }; } };");
ASSERT_EQUALS("", errout.str());
}
// Dereferencing invalid pointer // Dereferencing invalid pointer
void dereference() { void dereference() {
check("void f()\n" check("void f()\n"