From 69a98d81fc285357cef0c94946a1a1c22dacbbce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 18 Dec 2009 20:55:51 +0100 Subject: [PATCH] Fixed #1107 (False positive: Dangerous usage of erase, when using std::list) --- lib/checkstl.cpp | 6 ++++-- test/teststl.cpp | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 6bfd40d95..8bb595fa8 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -220,7 +220,7 @@ void CheckStl::eraseCheckLoop(const Token *it) return; // Parse loop.. - // Error if it contains "erase(it)" but neither "break;" nor "it=" + // Error if it contains "erase(it)" but neither "break;", "=it" nor "it=" indentlevel = 0; const Token *tok2 = 0; while (0 != (tok = tok->next())) @@ -233,7 +233,9 @@ void CheckStl::eraseCheckLoop(const Token *it) if (indentlevel <= 0) break; } - else if (Token::Match(tok, "break|return|goto") || Token::simpleMatch(tok, (it->str() + " =").c_str())) + else if (Token::Match(tok, "break|return|goto") || + Token::simpleMatch(tok, (it->str() + " =").c_str()) || + Token::simpleMatch(tok, ("= " + it->str()).c_str())) { tok2 = 0; break; diff --git a/test/teststl.cpp b/test/teststl.cpp index 58ea3e2c5..5ee14dae1 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -46,6 +46,7 @@ private: TEST_CASE(STLSize); TEST_CASE(STLSizeNoErr); TEST_CASE(erase); + TEST_CASE(erase2); TEST_CASE(eraseBreak); TEST_CASE(eraseReturn); TEST_CASE(eraseGoto); @@ -289,6 +290,20 @@ private: } + void erase2() + { + check("static void f()\n" + "{\n" + " for (it = foo.begin(); it != foo.end(); it = next)\n" + " {\n" + " next = it;\n" + " next++;\n" + " foo.erase(it);\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void eraseBreak() { check("void f()\n"