Fixed #4197 - False negative: invalidIterator2 not detected

This commit is contained in:
Jose Roquette 2012-11-10 15:59:31 +00:00 committed by PKEuS
parent 4ee955fc8c
commit e8d3a4300d
2 changed files with 19 additions and 0 deletions

View File

@ -602,6 +602,7 @@ void CheckStl::pushback()
// Using push_back or push_front inside a loop..
if (Token::simpleMatch(tok2, "for (")) {
tok2 = tok2->tokAt(2);
++indent;
}
if (Token::Match(tok2, "%varid% = %var% . begin|rbegin|cbegin|crbegin ( ) ; %varid% != %var% . end|rend|cend|crend ( ) ; ++| %varid% ++| ) {", iteratorid)) {

View File

@ -82,6 +82,7 @@ private:
TEST_CASE(pushback9);
TEST_CASE(pushback10);
TEST_CASE(pushback11);
TEST_CASE(pushback12);
TEST_CASE(insert1);
TEST_CASE(insert2);
@ -1099,6 +1100,23 @@ private:
ASSERT_EQUALS("", errout.str());
}
void pushback12() {
// #4197
check("void foo(double s)\n"
"{\n"
" std::vector<double> vec;\n"
" for( std::vector<double>::iterator it = vec.begin(); it != vec.end(); ++it )\n"
" {\n"
" vec.insert( it, s );\n"
" for(unsigned int i = 0; i < 42; i++)\n"
" {}\n"
" *it;\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:6]: (error) After insert(), the iterator 'it' may be invalid.\n"
"[test.cpp:9]: (error) After insert(), the iterator 'it' may be invalid.\n", errout.str());
}
void insert1() {
check("void f(std::vector<int> &ints)\n"
"{\n"