Skip to content

Commit 5d8da2b

Browse files
authored
Fix 11035: FP arrayIndexOutOfBoundsCond with array and vector (cppcheck-opensource#4105)
* Fix 11035: FP arrayIndexOutOfBoundsCond with array and vector * Format * Move comment
1 parent e7e8b1b commit 5d8da2b

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

lib/astutils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1666,7 +1666,7 @@ bool isConstFunctionCall(const Token* ftok, const Library& library)
16661666
if (Function::returnsVoid(f))
16671667
return false;
16681668
// Member function call
1669-
if (Token::simpleMatch(ftok->previous(), ".")) {
1669+
if (Token::simpleMatch(ftok->previous(), ".") || exprDependsOnThis(ftok->next())) {
16701670
if (f->isConst())
16711671
return true;
16721672
// Check for const overloaded function that just return the const version

test/testbufferoverrun.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ class TestBufferOverrun : public TestFixture {
243243
TEST_CASE(buffer_overrun_31);
244244
TEST_CASE(buffer_overrun_32); //#10244
245245
TEST_CASE(buffer_overrun_33); //#2019
246+
TEST_CASE(buffer_overrun_34); //#11035
246247
TEST_CASE(buffer_overrun_errorpath);
247248
TEST_CASE(buffer_overrun_bailoutIfSwitch); // ticket #2378 : bailoutIfSwitch
248249
TEST_CASE(buffer_overrun_function_array_argument);
@@ -3120,6 +3121,22 @@ class TestBufferOverrun : public TestFixture {
31203121
ASSERT_EQUALS("[test.cpp:5]: (error) Array 'z[16]' accessed at index 19, which is out of bounds.\n", errout.str());
31213122
}
31223123

3124+
// #11035
3125+
void buffer_overrun_34()
3126+
{
3127+
check("struct S {\n"
3128+
" std::vector<int> v;\n"
3129+
" int a[15] = {};\n"
3130+
" int g() const { return v.size(); }\n"
3131+
" int f(int i) const {\n"
3132+
" if (i < 0 || i >= g())\n"
3133+
" return 0;\n"
3134+
" return a[i];\n"
3135+
" }\n"
3136+
"};\n");
3137+
ASSERT_EQUALS("", errout.str());
3138+
}
3139+
31233140
void buffer_overrun_errorpath() {
31243141
setMultiline();
31253142
settings0.templateLocation = "{file}:{line}:note:{info}";

0 commit comments

Comments
 (0)