STL: fixed false negative when erasing invalidated iterator

This commit is contained in:
Daniel Marjamäki 2010-04-17 13:37:04 +02:00
parent 6371327487
commit 9cd8bd842e
3 changed files with 11 additions and 2 deletions

View File

@ -30,6 +30,11 @@ CheckStl instance;
// Error message for bad iterator usage.. // Error message for bad iterator usage..
void CheckStl::invalidIteratorError(const Token *tok, const std::string &iteratorName)
{
reportError(tok, Severity::error, "invalidIterator", "Invalid iterator: " + iteratorName);
}
void CheckStl::iteratorsError(const Token *tok, const std::string &container1, const std::string &container2) void CheckStl::iteratorsError(const Token *tok, const std::string &container1, const std::string &container2)
{ {
reportError(tok, Severity::error, "iterators", "Same iterator is used with both " + container1 + " and " + container2); reportError(tok, Severity::error, "iterators", "Same iterator is used with both " + container1 + " and " + container2);
@ -66,6 +71,9 @@ void CheckStl::iterators()
} }
else if (Token::Match(tok2, "%var% . insert|erase ( %varid% )|,", iteratorId)) else if (Token::Match(tok2, "%var% . insert|erase ( %varid% )|,", iteratorId))
{ {
if (!validIterator)
invalidIteratorError(tok2, tok2->strAt(4));
if (tok2->varId() != containerId && tok2->tokAt(5)->str() != ".") if (tok2->varId() != containerId && tok2->tokAt(5)->str() != ".")
{ {
// skip error message if container is a set.. // skip error message if container is a set..

View File

@ -117,6 +117,7 @@ private:
void eraseCheckLoop(const Token *it); void eraseCheckLoop(const Token *it);
void stlOutOfBoundsError(const Token *tok, const std::string &num, const std::string &var); void stlOutOfBoundsError(const Token *tok, const std::string &num, const std::string &var);
void invalidIteratorError(const Token *tok, const std::string &iteratorName);
void iteratorsError(const Token *tok, const std::string &container1, const std::string &container2); void iteratorsError(const Token *tok, const std::string &container1, const std::string &container2);
void mismatchingContainersError(const Token *tok); void mismatchingContainersError(const Token *tok);
void eraseError(const Token *tok); void eraseError(const Token *tok);
@ -128,6 +129,7 @@ private:
void getErrorMessages() void getErrorMessages()
{ {
invalidIteratorError(0, "iterator");
iteratorsError(0, "container1", "container2"); iteratorsError(0, "container1", "container2");
mismatchingContainersError(0); mismatchingContainersError(0);
dereferenceErasedError(0, "iter"); dereferenceErasedError(0, "iter");

View File

@ -437,8 +437,7 @@ private:
" ints.erase(iter);\n" " ints.erase(iter);\n"
" ints.erase(iter);\n" " ints.erase(iter);\n"
"}\n"); "}\n");
TODO_ASSERT_EQUALS("[test.cpp:6]: (error) Erasing invalid iterator\n", errout.str()); ASSERT_EQUALS("[test.cpp:6]: (error) Invalid iterator: iter\n", errout.str());
ASSERT_EQUALS("", errout.str());
} }