Fixed #877 (False positive: After insert, iterator may be invalid)
This commit is contained in:
parent
344fdc1d7d
commit
395ce30d81
|
@ -295,8 +295,9 @@ void CheckStl::pushback()
|
|||
// Iterator becomes invalid after push_back or push_front..
|
||||
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
||||
{
|
||||
if (Token::simpleMatch(tok, "vector <"))
|
||||
{
|
||||
if (!Token::simpleMatch(tok, "vector <"))
|
||||
continue;
|
||||
|
||||
// if iterator declaration inside for() loop
|
||||
bool iteratorDeclaredInsideLoop = false;
|
||||
if ((tok->tokAt(-2) && Token::simpleMatch(tok->tokAt(-2), "for (")) ||
|
||||
|
@ -309,8 +310,9 @@ void CheckStl::pushback()
|
|||
tok = tok->next();
|
||||
if (!tok)
|
||||
break;
|
||||
if (Token::Match(tok, "> :: iterator|const_iterator %var% =|;"))
|
||||
{
|
||||
if (!Token::Match(tok, "> :: iterator|const_iterator %var% =|;"))
|
||||
continue;
|
||||
|
||||
const unsigned int iteratorid(tok->tokAt(3)->varId());
|
||||
if (iteratorid == 0)
|
||||
continue;
|
||||
|
@ -387,7 +389,17 @@ void CheckStl::pushback()
|
|||
|
||||
// push_back on vector..
|
||||
if (vectorname.size() && Token::Match(tok2, (vectorname + " . push_front|push_back|insert").c_str()))
|
||||
{
|
||||
invalidIterator = tok2->strAt(2);
|
||||
if (!iteratorDeclaredInsideLoop)
|
||||
{
|
||||
while (tok2 && tok2->str() != "(")
|
||||
tok2 = tok2->next();
|
||||
tok2 = tok2 ? tok2->link() : 0;
|
||||
if (!tok2)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Using invalid iterator..
|
||||
if (!invalidIterator.empty())
|
||||
|
@ -398,8 +410,8 @@ void CheckStl::pushback()
|
|||
invalidIteratorError(tok2, invalidIterator, tok2->str());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -489,6 +489,13 @@ private:
|
|||
" ++iter;\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:5]: (error) After insert, the iterator 'iter' may be invalid\n", errout.str());
|
||||
|
||||
check("void f()\n"
|
||||
"{\n"
|
||||
" std::vector<int>::iterator iter = ints.begin();\n"
|
||||
" ints.insert(iter, 1);\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue