Skip to content

Commit bcbc29a

Browse files
Fix #10526 FP: negativeIndex if function call is made after index check (danmar#4052)
* Fix #10526 FP: negativeIndex if function call is made after index check * Improve container check * Format
1 parent 6df0f9b commit bcbc29a

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

lib/valueflow.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2253,10 +2253,13 @@ struct ValueFlowAnalyzer : Analyzer {
22532253
} else if (getSettings()->library.getFunction(tok)) {
22542254
// Assume library function doesn't modify user-global variables
22552255
return Action::None;
2256-
// Function cast does not modify global variables
2256+
} else if (Token::simpleMatch(tok->astParent(), ".") && astIsContainer(tok->astParent()->astOperand1())) {
2257+
// Assume container member function doesn't modify user-global variables
2258+
return Action::None;
22572259
} else if (tok->tokType() == Token::eType && astIsPrimitive(tok->next())) {
2260+
// Function cast does not modify global variables
22582261
return Action::None;
2259-
} else if (Token::Match(tok, "%name% (")) {
2262+
} else if (!tok->isKeyword() && Token::Match(tok, "%name% (")) {
22602263
return Action::Invalid;
22612264
}
22622265
return Action::None;

test/testbufferoverrun.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ class TestBufferOverrun : public TestFixture {
197197
TEST_CASE(array_index_negative2); // ticket #3063
198198
TEST_CASE(array_index_negative3);
199199
TEST_CASE(array_index_negative4);
200+
TEST_CASE(array_index_negative5); // #10526
200201
TEST_CASE(array_index_for_decr);
201202
TEST_CASE(array_index_varnames); // FP: struct member #1576, FN: #1586
202203
TEST_CASE(array_index_for_continue); // for,continue
@@ -2093,6 +2094,24 @@ class TestBufferOverrun : public TestFixture {
20932094
ASSERT_EQUALS("", errout.str());
20942095
}
20952096

2097+
void array_index_negative5() // #10526
2098+
{
2099+
check("int i;\n"
2100+
"std::vector<int> v;\n"
2101+
"bool f() {\n"
2102+
" if (i != 0) {\n"
2103+
" if (v.begin() != v.end()) {\n"
2104+
" if (i < 0)\n"
2105+
" return false;\n"
2106+
" const int a[4] = { 0, 1, 2, 3 };\n"
2107+
" return a[i - 1] > 0;\n"
2108+
" }\n"
2109+
" }\n"
2110+
" return false;\n"
2111+
"}\n");
2112+
ASSERT_EQUALS("", errout.str());
2113+
}
2114+
20962115
void array_index_for_decr() {
20972116
check("void f()\n"
20982117
"{\n"

0 commit comments

Comments
 (0)