From a33b4744a3909b0a2ed33bcb9e7634338031c02c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 13 Oct 2009 20:55:33 +0200 Subject: [PATCH] stl: better checking for invalid iterator after push_back --- src/checkstl.cpp | 4 ++-- test/teststl.cpp | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/checkstl.cpp b/src/checkstl.cpp index 5aa3b9607..d4272f7ea 100644 --- a/src/checkstl.cpp +++ b/src/checkstl.cpp @@ -354,7 +354,7 @@ void CheckStl::pushback() // Assigning iterator.. if (Token::Match(tok2, "%varid% =", iteratorid)) { - if (Token::Match(tok2->tokAt(2), "%var% . begin ( )")) + if (Token::Match(tok2->tokAt(2), "%var% . begin|end|rbegin|rend ( )")) vectorname = tok2->strAt(2); else vectorname = ""; @@ -368,7 +368,7 @@ void CheckStl::pushback() // Using invalid iterator.. if (invalidIterator) { - if (Token::Match(tok2, "++|--|*|+|-|(|, %varid%", iteratorid)) + if (Token::Match(tok2, "++|--|*|+|-|(|,|=|!= %varid%", iteratorid)) pushbackError(tok2, tok2->strAt(1)); if (Token::Match(tok2, "%varid% ++|--|+|-", iteratorid)) pushbackError(tok2, tok2->str()); diff --git a/test/teststl.cpp b/test/teststl.cpp index 08b4085f3..a8da29004 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -58,6 +58,7 @@ private: TEST_CASE(pushback5); TEST_CASE(pushback6); TEST_CASE(pushback7); + TEST_CASE(pushback8); TEST_CASE(insert1); @@ -449,6 +450,23 @@ private: ASSERT_EQUALS("[test.cpp:8]: (error) After push_back or push_front, the iterator 'it' may be invalid\n", errout.str()); } + void pushback8() + { + check("void f()\n" + "{\n" + " std::vector ints;\n" + " std::vector::const_iterator end = ints.end();\n" + " ints.push_back(10);\n" + " std::vector::iterator it;\n" + " unsigned int sum = 0;\n" + " for (it = ints.begin(); it != end; ++it)\n" + " {\n" + " sum += *it;\n" + " }\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:8]: (error) After push_back or push_front, the iterator 'end' may be invalid\n", errout.str()); + } + void insert1() {