diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 313f15c38..264ecab0d 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -153,7 +153,10 @@ void CheckStl::outOfBounds() return false; if (v.intvalue < 0) return false; - const Token* containerTok = getContainerFromSize(container, v.tokvalue); + const Token* sizeTok = v.tokvalue; + if (sizeTok && sizeTok->isCast()) + sizeTok = sizeTok->astOperand1(); + const Token* containerTok = getContainerFromSize(container, sizeTok); if (!containerTok) return false; return containerTok->exprId() == tok->exprId(); diff --git a/test/teststl.cpp b/test/teststl.cpp index 985458e25..0d27837cd 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -682,6 +682,16 @@ private: " return h(&v[0], v.size()); \n" "}\n"); ASSERT_EQUALS("", errout.str()); + + checkNormal("void f(int i, std::vector v) {\n" // #9157 + " if (i <= (int)v.size()) {\n" + " if (v[i]) {}\n" + " }\n" + "}\n"); + ASSERT_EQUALS("test.cpp:3:warning:Either the condition 'i<=(int)v.size()' is redundant or 'i' can have the value v.size(). Expression 'v[i]' cause access out of bounds.\n" + "test.cpp:2:note:condition 'i<=(int)v.size()'\n" + "test.cpp:3:note:Access out of bounds\n", + errout.str()); } void outOfBoundsSymbolic()