stl: better checking for invalid iterator after push_back

This commit is contained in:
Daniel Marjamäki 2009-10-13 20:55:33 +02:00
parent 0c3da73ee1
commit a33b4744a3
2 changed files with 20 additions and 2 deletions

View File

@ -354,7 +354,7 @@ void CheckStl::pushback()
// Assigning iterator..
if (Token::Match(tok2, "%varid% =", iteratorid))
{
if (Token::Match(tok2->tokAt(2), "%var% . begin ( )"))
if (Token::Match(tok2->tokAt(2), "%var% . begin|end|rbegin|rend ( )"))
vectorname = tok2->strAt(2);
else
vectorname = "";
@ -368,7 +368,7 @@ void CheckStl::pushback()
// Using invalid iterator..
if (invalidIterator)
{
if (Token::Match(tok2, "++|--|*|+|-|(|, %varid%", iteratorid))
if (Token::Match(tok2, "++|--|*|+|-|(|,|=|!= %varid%", iteratorid))
pushbackError(tok2, tok2->strAt(1));
if (Token::Match(tok2, "%varid% ++|--|+|-", iteratorid))
pushbackError(tok2, tok2->str());

View File

@ -58,6 +58,7 @@ private:
TEST_CASE(pushback5);
TEST_CASE(pushback6);
TEST_CASE(pushback7);
TEST_CASE(pushback8);
TEST_CASE(insert1);
@ -449,6 +450,23 @@ private:
ASSERT_EQUALS("[test.cpp:8]: (error) After push_back or push_front, the iterator 'it' may be invalid\n", errout.str());
}
void pushback8()
{
check("void f()\n"
"{\n"
" std::vector<int> ints;\n"
" std::vector<int>::const_iterator end = ints.end();\n"
" ints.push_back(10);\n"
" std::vector<int>::iterator it;\n"
" unsigned int sum = 0;\n"
" for (it = ints.begin(); it != end; ++it)\n"
" {\n"
" sum += *it;\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:8]: (error) After push_back or push_front, the iterator 'end' may be invalid\n", errout.str());
}
void insert1()
{