Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5299,7 +5299,11 @@ static bool valueFlowForLoop2(const Token *tok,

memory1.swap(startMemory);
if (!error) {
memory2.swap(endMemory);
// When the limit of iterations was reached because only the loop body changes the condition,
// the last iteration is not known either
const Token* bodyStart = tok->linkAt(1)->next();
if (result == 0 || !findExpressionChanged(secondExpression, bodyStart, bodyStart->link(), settings))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

The check is skipped when result == 0, but the comment's reasoning applies there too: if the body changes the condition, the loop can end before the header-only simulation does. Example:

const char s[] = "abc";
for (int i = 0, done = 0; !done && i < 100; i++) {
    if (s[i] == '\0') done = 1;
}

This still gives Array 's[4]' accessed at index 99 (same as base). Dropping memory2 whenever findExpressionChanged(secondExpression, ...) is true, whatever result is, would also cover this case. memoryAfter could still be kept when result == 0.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather keep memory2 there. With i < 100 in the condition, 99 is the real last iteration whenever the body doesn't set done earlier, while 9998 is only where the simulation stopped. Main reports the same index 99 when this loop is written with break, with done declared before the loop, or as a while loop (as arrayIndexOutOfBoundsCond), so dropping memory2 would only change the form with two variables in the init statement. In that form it would also hide real overruns like this one, which main and this PR report at index 99, just as they do when it is written with break:

int next(void);
void f(void) {
    char line[50];
    for (int i = 0, done = 0; !done && i < 100; i++) {
        line[i] = (char)next();
        if (line[i] == '\n') done = 1;
    }
}

memory2.swap(endMemory);
// The state after the loop is only known when the condition became false. When the limit of
// iterations was reached the loop runs on, or exits later in a state that is not known.
if (result == 0)
Expand Down
23 changes: 23 additions & 0 deletions test/testbufferoverrun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2708,6 +2708,29 @@ class TestBufferOverrun : public TestFixture {
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:14]: (error) Array 'a[10]' accessed at index 9998, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str());

check("int f() {\n"
" const char s[] = \"abc\";\n"
" int n = 0;\n"
" for (int i = 0, done = 0; done == 0; i++) {\n"
" if (s[i] == '\\0')\n"
" done = 1;\n"
" else\n"
" n++;\n"
" }\n"
" return n;\n"
"}\n");
ASSERT_EQUALS("", errout_str());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

A variant of the new test is still reported:

void g1(void) {
    int a[10];
    for (int i = 0, done = 0; done == 0; i++) {
        a[i] = 0;
        if (i == 9) done = 1;
    }
}

The PR reports Either the condition 'i==9' is redundant or the array 'a[10]' is accessed at index 10 (base reported index 9998 instead). i == 9 ends the loop, so 10 is never used as an index. The value comes from forwarding i==9 through i++ back into the body, ignoring that done = 1 ends the loop. In base it was hidden behind the 9998 error. Please consider adding this case, at least as a TODO assert.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added as a TODO assert in db68cfa. The 10 comes from i == 9 being forwarded after the if and through i++ into the next iteration, without taking into account that done = 1 ends the loop. Written with break instead of the flag, the loop gives no warning. Fixing it would mean teaching the forward analysis that the loop doesn't go on after done = 1, which I'd keep out of this PR


check("void f() {\n"
" int a[10];\n"
" for (int i = 0, done = 0; done == 0; i++) {\n"
" a[i] = 0;\n"
" if (i == 9)\n"
" done = 1;\n"
" }\n"
"}\n");
TODO_ASSERT_EQUALS("", "[test.cpp:5:15] -> [test.cpp:4:10]: (warning) Either the condition 'i==9' is redundant or the array 'a[10]' is accessed at index 10, which is out of bounds. [arrayIndexOutOfBoundsCond]\n", errout_str());
}

void array_index_for_neq() {
Expand Down