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.

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
13 changes: 13 additions & 0 deletions test/testbufferoverrun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2708,6 +2708,19 @@ 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.

}

void array_index_for_neq() {
Expand Down
Loading