Skip to content

Commit a039b8b

Browse files
Fix #12421 eraseIteratorOutOfBoundsCond: value is missing condition (#8883)
1 parent d412fe2 commit a039b8b

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

‎lib/checkstl.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3340,8 +3340,8 @@ void CheckStlImpl::eraseIteratorOutOfBoundsError(const Token *ftok, const Token*
33403340
}
33413341

33423342
const Severity severity = isConditional ? Severity::warning : Severity::error;
3343-
const std::string id = isConditional ? "eraseIteratorOutOfBoundsCond" : "eraseIteratorOutOfBounds";
3344-
reportError(ftok, severity,
3343+
const char* id = isConditional ? "eraseIteratorOutOfBoundsCond" : "eraseIteratorOutOfBounds";
3344+
reportError(getErrorPath(ftok, val, msg), severity,
33453345
id,
33463346
msg, CWE628, Certainty::normal);
33473347
}

‎test/teststl.cpp‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,20 @@ class TestStl : public TestFixture {
11401140
"[test.cpp:3:11]: note: Assuming that condition 'i>5' is not redundant\n"
11411141
"[test.cpp:5:13]: note: Access out of bounds\n",
11421142
errout_str());
1143+
1144+
check("void f(std::vector<int>& v) {\n"
1145+
" std::vector<int>::iterator it;\n"
1146+
" for (it = v.begin(); it != v.end(); ++it) {\n"
1147+
" if (*it == 0)\n"
1148+
" break;\n"
1149+
" }\n"
1150+
" v.erase(it);\n"
1151+
"}\n", s);
1152+
ASSERT_EQUALS("[test.cpp:4:13]: style: Consider using std::find_if algorithm instead of a raw loop. [useStlAlgorithm]\n"
1153+
"[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"
1154+
"[test.cpp:3:29]: note: Assuming that condition 'it!=v.end()' is not redundant\n"
1155+
"[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",
1156+
errout_str());
11431157
}
11441158

11451159
void iterator1() {
@@ -2441,7 +2455,7 @@ class TestStl : public TestFixture {
24412455
" if (it == v.end()) {}\n"
24422456
" v.erase(it);\n"
24432457
"}\n");
2444-
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",
2458+
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",
24452459
errout_str());
24462460

24472461
check("void f() {\n"

0 commit comments

Comments
 (0)