Fixed #2481 (false positive with break: After insert, the iterator '*' may be invalid)

This commit is contained in:
Daniel Marjamäki 2011-01-20 19:26:52 +01:00
parent 9dce0dd75c
commit a21f8eec7c
2 changed files with 25 additions and 7 deletions

View File

@ -594,7 +594,7 @@ void CheckStl::pushback()
break; break;
--indent3; --indent3;
} }
else if (tok3->str() == "break") else if (tok3->str() == "break" || tok3->str() == "return")
{ {
pushbackTok = 0; pushbackTok = 0;
break; break;
@ -634,12 +634,7 @@ void CheckStl::pushback()
} }
invalidIterator = tok2->strAt(2); invalidIterator = tok2->strAt(2);
if (!iteratorDeclaredInsideLoop)
{
tok2 = tok2->tokAt(3)->link(); tok2 = tok2->tokAt(3)->link();
if (!tok2)
break;
}
} }
else if (tok2->str() == "return" || tok2->str() == "break") else if (tok2->str() == "return" || tok2->str() == "break")

View File

@ -76,6 +76,7 @@ private:
TEST_CASE(pushback10); TEST_CASE(pushback10);
TEST_CASE(insert1); TEST_CASE(insert1);
TEST_CASE(insert2);
TEST_CASE(invalidcode); TEST_CASE(invalidcode);
@ -904,7 +905,29 @@ private:
ASSERT_EQUALS("[test.cpp:6]: (error) After insert, the iterator 'iter' may be invalid\n", errout.str()); ASSERT_EQUALS("[test.cpp:6]: (error) After insert, the iterator 'iter' may be invalid\n", errout.str());
} }
void insert2()
{
// Ticket: #2169
check("void f(std::vector<int> &vec) {\n"
" for(std::vector<int>::iterator iter = vec.begin(); iter != vec.end(); ++iter)\n"
" {\n"
" vec.insert(iter, 0);\n"
" break;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f(std::vector<int> &vec) {\n"
" for(std::vector<int>::iterator iter = vec.begin(); iter != vec.end(); ++iter)\n"
" {\n"
" if (*it == 0) {\n"
" vec.insert(iter, 0);\n"
" return;\n"
" }\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void invalidcode() void invalidcode()
{ {