Fixed #1600 (false positive: After insert, the iterator 'aI' may be invalid)

This commit is contained in:
Daniel Marjamäki 2010-04-16 16:56:55 +02:00
parent 10a7c3f70c
commit c9fb5f87e0
2 changed files with 45 additions and 0 deletions

View File

@ -432,6 +432,11 @@ void CheckStl::pushback()
}
}
else if (tok2->str() == "return")
{
invalidIterator.clear();
}
// Using invalid iterator..
if (!invalidIterator.empty())
{

View File

@ -40,6 +40,7 @@ private:
TEST_CASE(iterator4);
TEST_CASE(iterator5);
TEST_CASE(iterator6);
TEST_CASE(iterator7);
TEST_CASE(dereference);
TEST_CASE(dereference_member);
@ -176,6 +177,45 @@ private:
ASSERT_EQUALS("", errout.str());
}
void iterator7()
{
// Ticket #1600
check("void foo(std::vector<int> &r)\n"
"{\n"
" std::vector<int>::iterator aI = r.begin();\n"
" while(aI != r.end())\n"
" {\n"
" if (*aI == 0)\n"
" {\n"
" r.insert(aI, 42);\n"
" return;\n"
" }\n"
" ++aI;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// Execution path checking..
check("void foo(std::vector<int> &r, int c)\n"
"{\n"
" std::vector<int>::iterator aI = r.begin();\n"
" while(aI != r.end())\n"
" {\n"
" if (*aI == 0)\n"
" {\n"
" r.insert(aI, 42);\n"
" if (c)\n"
" {\n"
" return;\n"
" }\n"
" }\n"
" ++aI;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
TODO_ASSERT_EQUALS("[test.cpp:14] (error) After insert, the iterator 'aI' may be invalid", errout.str());
}
// Dereferencing invalid pointer
void dereference()
{