Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/checkstl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
16 changes: 15 additions & 1 deletion test/teststl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>& v) {\n"
" std::vector<int>::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() {
Expand Down Expand Up @@ -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"
Expand Down
Loading