diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 76a9392ec..e6dc0fde3 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -381,21 +381,28 @@ void CheckStl::pushback() if (Token::Match(tok2, "%varid% =", iteratorid)) { if (Token::Match(tok2->tokAt(2), "%var% . begin|end|rbegin|rend ( )")) + { vectorname = tok2->strAt(2); + tok2 = tok2->tokAt(6); + } else vectorname = ""; invalidIterator = ""; } // push_back on vector.. - if (vectorname.size() && Token::Match(tok2, (vectorname + " . push_front|push_back|insert").c_str())) + if (vectorname.size() && Token::Match(tok2, (vectorname + " . push_front|push_back|insert (").c_str())) { + if (!invalidIterator.empty() && Token::Match(tok2->tokAt(2), "insert ( %varid% ,", iteratorid)) + { + invalidIteratorError(tok2, invalidIterator, tok2->strAt(4)); + break; + } + invalidIterator = tok2->strAt(2); if (!iteratorDeclaredInsideLoop) { - while (tok2 && tok2->str() != "(") - tok2 = tok2->next(); - tok2 = tok2 ? tok2->link() : 0; + tok2 = tok2->tokAt(3)->link(); if (!tok2) break; } diff --git a/test/teststl.cpp b/test/teststl.cpp index 2983cda8e..092abff4f 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -495,10 +495,20 @@ private: check("void f()\n" "{\n" + " std::vector ints;\n" " std::vector::iterator iter = ints.begin();\n" " ints.insert(iter, 1);\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("void f()\n" + "{\n" + " std::vector ints;\n" + " std::vector::iterator iter = ints.begin();\n" + " ints.insert(iter, 1);\n" + " ints.insert(iter, 2);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:6]: (error) After insert, the iterator 'iter' may be invalid\n", errout.str()); }