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
19 changes: 5 additions & 14 deletions lib/checkstl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,20 +259,11 @@ void CheckStlImpl::outOfBoundsError(const Token *tok, const std::string &contain
return;
}

ErrorPath errorPath;
if (!indexValue)
errorPath = getErrorPath(tok, containerSize, "Access out of bounds");
else {
ErrorPath errorPath1 = getErrorPath(tok, containerSize, "Access out of bounds");
ErrorPath errorPath2 = getErrorPath(tok, indexValue, "Access out of bounds");
if (errorPath1.size() <= 1)
errorPath = std::move(errorPath2);
else if (errorPath2.size() <= 1)
errorPath = std::move(errorPath1);
else {
errorPath = std::move(errorPath1);
errorPath.splice(errorPath.end(), errorPath2);
}
ErrorPath errorPath = getErrorPath(tok, containerSize, "Access out of bounds");
if (indexValue) {
ErrorPath errorPathIdx = getErrorPath(tok, indexValue, "Access out of bounds");
if (errorPathIdx.size() >= errorPath.size())
errorPath = std::move(errorPathIdx);
}

reportError(std::move(errorPath),
Expand Down
18 changes: 18 additions & 0 deletions test/teststl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class TestStl : public TestFixture {
TEST_CASE(outOfBoundsSymbolic);
TEST_CASE(outOfBoundsIndexExpression);
TEST_CASE(outOfBoundsIterator);
TEST_CASE(outOfBoundsErrorPath);

TEST_CASE(iterator1);
TEST_CASE(iterator2);
Expand Down Expand Up @@ -1124,6 +1125,23 @@ class TestStl : public TestFixture {
errout_str());
}

void outOfBoundsErrorPath() {
setMultiline();
Settings s = settings;
s.templateLocation = "{file}:{line}:note:{info}";

check("int f(int i) {\n"
" std::string s = \"abc\";\n"
" if (i > 5)\n"
" return 0;\n"
" return s[i];\n"
"}\n", s);
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"
"[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());
}

void iterator1() {
check("void f()\n"
"{\n"
Expand Down
Loading