Fixed #932 (false positive: Dereferenced iterator 'i' has been erased)

This commit is contained in:
Daniel Marjamäki 2009-11-10 17:20:20 +01:00
parent 293631745a
commit 51f983629b
2 changed files with 34 additions and 21 deletions

View File

@ -73,6 +73,13 @@ void CheckStl::iterators()
tok2 = tok2->tokAt(4);
}
else if (Token::Match(tok2, "%varid% = %var% . erase (", iteratorId))
{
validIterator = true;
tok2 = tok2->tokAt(5)->link();
if (!tok2)
break;
}
else if (!validIterator && Token::Match(tok2, "* %varid%", iteratorId))
{
dereferenceErasedError(tok2, tok2->strAt(1));

View File

@ -258,28 +258,34 @@ private:
void erase()
{
{
check("void f()\n"
"{\n"
" for (it = foo.begin(); it != foo.end(); ++it)\n"
" {\n"
" foo.erase(it);\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Dangerous usage of erase\n", errout.str());
}
check("void f()\n"
"{\n"
" for (it = foo.begin(); it != foo.end(); ++it)\n"
" {\n"
" foo.erase(it);\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Dangerous usage of erase\n", errout.str());
check("for (it = foo.begin(); it != foo.end(); ++it)\n"
"{\n"
" foo.erase(it);\n"
"}\n"
"for (it = foo.begin(); it != foo.end(); ++it)\n"
"{\n"
" foo.erase(it);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Dangerous usage of erase\n[test.cpp:7]: (error) Dangerous usage of erase\n", errout.str());
check("void f(std::list<int> &ints)\n"
"{\n"
" std::list<int>::iterator i = ints.begin();\n"
" i = ints.erase(i);\n"
" *i = 0;\n"
" \n"
"}\n");
ASSERT_EQUALS("", errout.str());
{
check("for (it = foo.begin(); it != foo.end(); ++it)\n"
"{\n"
" foo.erase(it);\n"
"}\n"
"for (it = foo.begin(); it != foo.end(); ++it)\n"
"{\n"
" foo.erase(it);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Dangerous usage of erase\n[test.cpp:7]: (error) Dangerous usage of erase\n", errout.str());
}
}
void eraseBreak()