diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index de1d82792ac..a5f1f582c4d 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -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), diff --git a/test/teststl.cpp b/test/teststl.cpp index 2570059c19e..677147bf0ce 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -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); @@ -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"