Fixed #1679 (Bad iterators checks fails to detect bugs with indent levels)

This commit is contained in:
Erik Lax 2010-05-15 20:00:41 +02:00 committed by Daniel Marjamki
parent 0510d9a2b0
commit c0149d3cd1
2 changed files with 21 additions and 1 deletions

View File

@ -64,9 +64,12 @@ void CheckStl::iterators()
continue;
bool validIterator = true;
unsigned int indent = 0;
for (const Token *tok2 = tok->tokAt(7); tok2; tok2 = tok2->next())
{
if (tok2->str() == "}")
if (tok2->str() == "{" && ++indent)
continue;
if (tok2->str() == "}" && --indent == 0)
break;
if (Token::Match(tok2, "%varid% != %var% . end ( )", iteratorId) && tok2->tokAt(2)->varId() != containerId)

View File

@ -41,6 +41,7 @@ private:
TEST_CASE(iterator5);
TEST_CASE(iterator6);
TEST_CASE(iterator7);
TEST_CASE(iterator8);
TEST_CASE(dereference);
TEST_CASE(dereference_member);
@ -220,6 +221,22 @@ private:
TODO_ASSERT_EQUALS("[test.cpp:14] (error) After insert, the iterator 'aI' may be invalid", errout.str());
}
void iterator8()
{
// Ticket #1679
check("void foo()\n"
"{\n"
" std::set<int> s1;\n"
" std::set<int> s2;\n"
" for (std::set<int>::iterator it = s1.begin(); it != s1.end(); ++it)\n"
" {\n"
" if (true) { }\n"
" if (it != s2.end()) continue;\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:8]: (error) Same iterator is used with both s1 and s2\n", errout.str());
}
// Dereferencing invalid pointer
void dereference()
{