Repository navigation
Fix FP arrayIndexOutOfBounds for a for loop whose condition changes in its body #9110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added as a TODO assert in db68cfa. The 10 comes from |
||
|
|
||
| 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() { | ||
|
|
||
There was a problem hiding this comment.
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:This still gives
Array 's[4]' accessed at index 99(same as base). Droppingmemory2wheneverfindExpressionChanged(secondExpression, ...)is true, whateverresultis, would also cover this case.memoryAftercould still be kept whenresult == 0.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather keep
memory2there. Withi < 100in the condition, 99 is the real last iteration whenever the body doesn't setdoneearlier, while 9998 is only where the simulation stopped. Main reports the same index 99 when this loop is written withbreak, withdonedeclared before the loop, or as awhileloop (asarrayIndexOutOfBoundsCond), so droppingmemory2would 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 withbreak: