From e8d3a4300d3bb854d93ab3390cd921d4b7410bd7 Mon Sep 17 00:00:00 2001 From: Jose Roquette Date: Sat, 10 Nov 2012 15:59:31 +0000 Subject: [PATCH] Fixed #4197 - False negative: invalidIterator2 not detected --- lib/checkstl.cpp | 1 + test/teststl.cpp | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 3f3ee1e88..6629d5f94 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -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)) { diff --git a/test/teststl.cpp b/test/teststl.cpp index 07109f66a..0f9561576 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -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 vec;\n" + " for( std::vector::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 &ints)\n" "{\n"