From 7e2208b5cc93a5bc9dab787074a26b8f0fd62aba Mon Sep 17 00:00:00 2001 From: Slava Semushin Date: Sun, 27 Sep 2009 15:10:21 +0700 Subject: [PATCH] Detects invalid iterator inside for() when postfix form of increment used. --- src/checkstl.cpp | 2 +- test/teststl.cpp | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/checkstl.cpp b/src/checkstl.cpp index 4ab3e5883..90057641a 100644 --- a/src/checkstl.cpp +++ b/src/checkstl.cpp @@ -318,7 +318,7 @@ void CheckStl::pushback() tok2 = tok2->tokAt(2); } - if (Token::Match(tok2, "%varid% = %var% . begin ( ) ; %varid% != %var% . end ( ) ; ++ %varid% ) {", iteratorid)) + if (Token::Match(tok2, "%varid% = %var% . begin ( ) ; %varid% != %var% . end ( ) ; ++| %varid% ++| ) {", iteratorid)) { const unsigned int vectorid(tok2->tokAt(2)->varId()); if (vectorid == 0) diff --git a/test/teststl.cpp b/test/teststl.cpp index 211fc9773..bf5753abf 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -57,6 +57,7 @@ private: TEST_CASE(pushback4); TEST_CASE(pushback5); TEST_CASE(pushback6); + TEST_CASE(pushback7); TEST_CASE(insert1); @@ -421,6 +422,21 @@ private: ASSERT_EQUALS("[test.cpp:9]: (error) After push_back or push_front, the iterator 'it' may be invalid\n", errout.str()); } + void pushback7() + { + check("void f()\n" + "{\n" + " std::vector foo;\n" + " foo.push_back(10);\n" + " std::vector::iterator it;\n" + " for (it = foo.begin(); it != foo.end(); it++)\n" + " {\n" + " foo.push_back(123);\n" + " }\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:8]: (error) After push_back or push_front, the iterator 'it' may be invalid\n", errout.str()); + } + void insert1() {