Skip to content

Commit edfdfe6

Browse files
authored
Fix 11651: FP negativeIndex with for loop (#4934)
1 parent 0f47948 commit edfdfe6

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

lib/valueflow.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7042,20 +7042,26 @@ static void valueFlowForLoop(TokenList *tokenlist, SymbolDatabase* symboldatabas
70427042
for (const auto& p : mem1) {
70437043
if (!p.second.isIntValue())
70447044
continue;
7045+
if (p.second.isImpossible())
7046+
continue;
70457047
if (p.first.tok->varId() == 0)
70467048
continue;
70477049
valueFlowForLoopSimplify(bodyStart, p.first.tok, false, p.second.intvalue, tokenlist, errorLogger, settings);
70487050
}
70497051
for (const auto& p : mem2) {
70507052
if (!p.second.isIntValue())
70517053
continue;
7054+
if (p.second.isImpossible())
7055+
continue;
70527056
if (p.first.tok->varId() == 0)
70537057
continue;
70547058
valueFlowForLoopSimplify(bodyStart, p.first.tok, false, p.second.intvalue, tokenlist, errorLogger, settings);
70557059
}
70567060
for (const auto& p : memAfter) {
70577061
if (!p.second.isIntValue())
70587062
continue;
7063+
if (p.second.isImpossible())
7064+
continue;
70597065
if (p.first.tok->varId() == 0)
70607066
continue;
70617067
valueFlowForLoopSimplifyAfter(tok, p.first.getExpressionId(), p.second.intvalue, tokenlist, settings);

test/testbufferoverrun.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ class TestBufferOverrun : public TestFixture {
197197
TEST_CASE(array_index_negative5); // #10526
198198
TEST_CASE(array_index_negative6); // #11349
199199
TEST_CASE(array_index_negative7); // #5685
200+
TEST_CASE(array_index_negative8); // #11651
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
@@ -2273,6 +2274,19 @@ class TestBufferOverrun : public TestFixture {
22732274
ASSERT_EQUALS("[test.cpp:5]: (error) Array 'a[5]' accessed at index -9, which is out of bounds.\n", errout.str());
22742275
}
22752276

2277+
// #11651
2278+
void array_index_negative8()
2279+
{
2280+
check("unsigned g(char*);\n"
2281+
"void f() {\n"
2282+
" char buf[10];\n"
2283+
" unsigned u = g(buf);\n"
2284+
" for (int i = u, j = sizeof(i); --i >= 0;)\n"
2285+
" char c = buf[i];\n"
2286+
"}\n");
2287+
ASSERT_EQUALS("", errout.str());
2288+
}
2289+
22762290
void array_index_for_decr() {
22772291
check("void f()\n"
22782292
"{\n"

0 commit comments

Comments
 (0)