diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 637f5e1e8b8..d99716d433a 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -3338,8 +3338,8 @@ void CheckStlImpl::eraseIteratorOutOfBoundsError(const Token *ftok, const Token* } const Severity severity = isConditional ? Severity::warning : Severity::error; - const std::string id = isConditional ? "eraseIteratorOutOfBoundsCond" : "eraseIteratorOutOfBounds"; - reportError(ftok, severity, + const char* id = isConditional ? "eraseIteratorOutOfBoundsCond" : "eraseIteratorOutOfBounds"; + reportError(getErrorPath(ftok, val, msg), severity, id, msg, CWE628, Certainty::normal); } diff --git a/test/teststl.cpp b/test/teststl.cpp index 197cc3fafa7..e7b77119321 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -1140,6 +1140,20 @@ class TestStl : public TestFixture { "[test.cpp:3:11]: note: Assuming that condition 'i>5' is not redundant\n" "[test.cpp:5:13]: note: Access out of bounds\n", errout_str()); + + check("void f(std::vector& v) {\n" + " std::vector::iterator it;\n" + " for (it = v.begin(); it != v.end(); ++it) {\n" + " if (*it == 0)\n" + " break;\n" + " }\n" + " v.erase(it);\n" + "}\n", s); + ASSERT_EQUALS("[test.cpp:4:13]: style: Consider using std::find_if algorithm instead of a raw loop. [useStlAlgorithm]\n" + "[test.cpp:7:7]: warning: Either the condition is redundant or function 'erase()' is called on the iterator 'it' which is out of bounds. [eraseIteratorOutOfBoundsCond]\n" + "[test.cpp:3:29]: note: Assuming that condition 'it!=v.end()' is not redundant\n" + "[test.cpp:7:7]: note: Either the condition is redundant or function 'erase()' is called on the iterator 'it' which is out of bounds.\n", + errout_str()); } void iterator1() { @@ -2441,7 +2455,7 @@ class TestStl : public TestFixture { " if (it == v.end()) {}\n" " v.erase(it);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3:7]: (warning) Either the condition 'it==v.end()' is redundant or function 'erase()' is called on the iterator 'it' which is out of bounds. [eraseIteratorOutOfBoundsCond]\n", + ASSERT_EQUALS("[test.cpp:2:12] -> [test.cpp:3:7]: (warning) Either the condition 'it==v.end()' is redundant or function 'erase()' is called on the iterator 'it' which is out of bounds. [eraseIteratorOutOfBoundsCond]\n", errout_str()); check("void f() {\n"