Partial fix for #9157 False negative: stlOutOfBounds, cast (#3699)

This commit is contained in:
chrchr-github 2022-01-13 08:03:24 +01:00 committed by GitHub
parent 34317f86ab
commit af0a585a70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 1 deletions

View File

@ -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();

View File

@ -682,6 +682,16 @@ private:
" return h(&v[0], v.size()); \n"
"}\n");
ASSERT_EQUALS("", errout.str());
checkNormal("void f(int i, std::vector<int> 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()