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,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()); | ||
|
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 |
||
| } | ||
|
|
||
| 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.