Skip to content

Commit c6109ba

Browse files
Fix #14968 Redundant error path for containerOutOfBounds warning (#8785)
1 parent 8a92e99 commit c6109ba

2 files changed

Lines changed: 23 additions & 14 deletions

File tree

lib/checkstl.cpp

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -259,20 +259,11 @@ void CheckStlImpl::outOfBoundsError(const Token *tok, const std::string &contain
259259
return;
260260
}
261261

262-
ErrorPath errorPath;
263-
if (!indexValue)
264-
errorPath = getErrorPath(tok, containerSize, "Access out of bounds");
265-
else {
266-
ErrorPath errorPath1 = getErrorPath(tok, containerSize, "Access out of bounds");
267-
ErrorPath errorPath2 = getErrorPath(tok, indexValue, "Access out of bounds");
268-
if (errorPath1.size() <= 1)
269-
errorPath = std::move(errorPath2);
270-
else if (errorPath2.size() <= 1)
271-
errorPath = std::move(errorPath1);
272-
else {
273-
errorPath = std::move(errorPath1);
274-
errorPath.splice(errorPath.end(), errorPath2);
275-
}
262+
ErrorPath errorPath = getErrorPath(tok, containerSize, "Access out of bounds");
263+
if (indexValue) {
264+
ErrorPath errorPathIdx = getErrorPath(tok, indexValue, "Access out of bounds");
265+
if (errorPathIdx.size() >= errorPath.size())
266+
errorPath = std::move(errorPathIdx);
276267
}
277268

278269
reportError(std::move(errorPath),

test/teststl.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class TestStl : public TestFixture {
4242
TEST_CASE(outOfBoundsSymbolic);
4343
TEST_CASE(outOfBoundsIndexExpression);
4444
TEST_CASE(outOfBoundsIterator);
45+
TEST_CASE(outOfBoundsErrorPath);
4546

4647
TEST_CASE(iterator1);
4748
TEST_CASE(iterator2);
@@ -1124,6 +1125,23 @@ class TestStl : public TestFixture {
11241125
errout_str());
11251126
}
11261127

1128+
void outOfBoundsErrorPath() {
1129+
setMultiline();
1130+
Settings s = settings;
1131+
s.templateLocation = "{file}:{line}:note:{info}";
1132+
1133+
check("int f(int i) {\n"
1134+
" std::string s = \"abc\";\n"
1135+
" if (i > 5)\n"
1136+
" return 0;\n"
1137+
" return s[i];\n"
1138+
"}\n", s);
1139+
ASSERT_EQUALS("[test.cpp:5:13]: warning: Either the condition 'i>5' is redundant or 'i' can have the value 5. Expression 's[i]' causes access out of bounds. [containerOutOfBounds]\n"
1140+
"[test.cpp:3:11]: note: Assuming that condition 'i>5' is not redundant\n"
1141+
"[test.cpp:5:13]: note: Access out of bounds\n",
1142+
errout_str());
1143+
}
1144+
11271145
void iterator1() {
11281146
check("void f()\n"
11291147
"{\n"

0 commit comments

Comments
 (0)