false poitive - invalid iterator after break

This commit is contained in:
Daniel Marjamäki 2012-04-09 07:19:39 +02:00
parent 66227c4da0
commit b8d233f1a2
2 changed files with 21 additions and 1 deletions

View File

@ -69,6 +69,8 @@ void CheckStl::iterators()
// the validIterator flag says if the iterator has a valid value or not // the validIterator flag says if the iterator has a valid value or not
bool validIterator = true; bool validIterator = true;
// When "validatingToken" is reached the validIterator is set to true
const Token* validatingToken = 0; const Token* validatingToken = 0;
// counter for { and } // counter for { and }
@ -166,7 +168,7 @@ void CheckStl::iterators()
// bailout handling. Assume that the iterator becomes valid if we see return/break. // bailout handling. Assume that the iterator becomes valid if we see return/break.
// TODO: better handling // TODO: better handling
else if (Token::Match(tok2, "return|break")) { else if (Token::Match(tok2, "return|break")) {
validatingToken = Token::findsimplematch(tok->next(), ";"); validatingToken = Token::findsimplematch(tok2->next(), ";");
} }
// bailout handling. Assume that the iterator becomes valid if we see else. // bailout handling. Assume that the iterator becomes valid if we see else.

View File

@ -46,6 +46,7 @@ private:
TEST_CASE(iterator12); TEST_CASE(iterator12);
TEST_CASE(dereference); TEST_CASE(dereference);
TEST_CASE(dereference_break); // #3644 - handle "break"
TEST_CASE(dereference_member); TEST_CASE(dereference_member);
TEST_CASE(STLSize); TEST_CASE(STLSize);
@ -383,6 +384,23 @@ private:
ASSERT_EQUALS("[test.cpp:7]: (error) Dereferenced iterator 'iter' has been erased\n", errout.str()); ASSERT_EQUALS("[test.cpp:7]: (error) Dereferenced iterator 'iter' has been erased\n", errout.str());
} }
void dereference_break() { // #3644
check("void f(std::vector<int> &ints) {\n"
" std::vector<int>::iterator iter;\n"
" for (iter=ints.begin();iter!=ints.end();++iter) {\n"
" if (*iter == 2) {\n"
" ints.erase(iter);\n"
" break;\n"
" }\n"
" if (*iter == 3) {\n"
" ints.erase(iter);\n"
" break;\n"
" }\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void dereference_member() { void dereference_member() {
check("void f()\n" check("void f()\n"
"{\n" "{\n"