Fix 11035: FP arrayIndexOutOfBoundsCond with array and vector (#4105)

* Fix 11035: FP arrayIndexOutOfBoundsCond with array and vector

* Format

* Move comment
This commit is contained in:
Paul Fultz II 2022-05-11 23:21:33 -05:00 committed by GitHub
parent e7e8b1baf9
commit 5d8da2b83c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View File

@ -1666,7 +1666,7 @@ bool isConstFunctionCall(const Token* ftok, const Library& library)
if (Function::returnsVoid(f))
return false;
// Member function call
if (Token::simpleMatch(ftok->previous(), ".")) {
if (Token::simpleMatch(ftok->previous(), ".") || exprDependsOnThis(ftok->next())) {
if (f->isConst())
return true;
// Check for const overloaded function that just return the const version

View File

@ -243,6 +243,7 @@ private:
TEST_CASE(buffer_overrun_31);
TEST_CASE(buffer_overrun_32); //#10244
TEST_CASE(buffer_overrun_33); //#2019
TEST_CASE(buffer_overrun_34); //#11035
TEST_CASE(buffer_overrun_errorpath);
TEST_CASE(buffer_overrun_bailoutIfSwitch); // ticket #2378 : bailoutIfSwitch
TEST_CASE(buffer_overrun_function_array_argument);
@ -3120,6 +3121,22 @@ private:
ASSERT_EQUALS("[test.cpp:5]: (error) Array 'z[16]' accessed at index 19, which is out of bounds.\n", errout.str());
}
// #11035
void buffer_overrun_34()
{
check("struct S {\n"
" std::vector<int> v;\n"
" int a[15] = {};\n"
" int g() const { return v.size(); }\n"
" int f(int i) const {\n"
" if (i < 0 || i >= g())\n"
" return 0;\n"
" return a[i];\n"
" }\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
void buffer_overrun_errorpath() {
setMultiline();
settings0.templateLocation = "{file}:{line}:note:{info}";