-
Notifications
You must be signed in to change notification settings - Fork 88
Fix memOutOfBounds handling of negative pointer offsets
#2017
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
Changes from all commits
321fe15
f57838e
8ad5fb3
5447075
8400643
08f01ee
9c3cf67
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 |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // PARAM: --set ana.activated[+] memOutOfBounds --enable ana.int.interval | ||
| #include <stdlib.h> | ||
|
|
||
| int main() { | ||
| int *b = malloc(2 * sizeof(int)); | ||
| int x; | ||
|
|
||
| *b++ = 0; //NOWARN | ||
|
|
||
| x = *(b - 2); //WARN | ||
|
|
||
| free(b - 1); | ||
| return x; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // PARAM: --set ana.activated[+] memOutOfBounds --enable ana.int.interval | ||
| #include <stdlib.h> | ||
|
|
||
| int main() { | ||
| int *b = malloc(2 * sizeof(int)); | ||
|
|
||
| *b++ = 0; //NOWARN | ||
|
|
||
| if (b[-2]) //WARN | ||
| return 1; | ||
|
|
||
| free(b - 1); | ||
| return 0; | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,12 @@ | ||||||||
| // PARAM: --set ana.activated[+] memOutOfBounds --enable ana.int.interval | ||||||||
| #include <stdlib.h> | ||||||||
| #include <stdio.h> | ||||||||
|
|
||||||||
| int main(void) { | ||||||||
| char *buf = malloc(4); | ||||||||
| char *end; | ||||||||
| end = buf + 4; //NOWARN | ||||||||
| printf("%p", (void *) end); //NOWARN | ||||||||
|
Member
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.
Suggested change
So we also see that we do indeed warn if this pointer is dereferenced.
Member
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. ha! it seems you just uncovered an unrelated unsoundness with this ;) I suggest we merge this as is and I will add this suggestion on another PR with an appropriate fix.
Member
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. Yeah, sounds good! |
||||||||
| free(buf); | ||||||||
| return 0; | ||||||||
| } | ||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // PARAM: --set ana.activated[+] memOutOfBounds --enable ana.int.interval | ||
| #include <stdlib.h> | ||
|
|
||
| struct S { | ||
| unsigned char a; | ||
| unsigned char b:2; | ||
| unsigned char c:2; | ||
| unsigned char d; | ||
| } __attribute__((packed)); | ||
|
|
||
| int main(void) { | ||
| struct S *p = malloc(2); | ||
| p->d = 1; //WARN | ||
| free(p); | ||
| return 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.
Could you elaborate on why this needs to be handled differently from the things above?
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.
First I didn't give it much thought, I just refactored out the parts that were in the original code, but with the help of Codex, I now know the difference and we also added regtests that would catch a change in these in both directions:
The unsoundness by changing the deref case to
ltis also caught by an existing test 74/13, but the other way around was not detectable by the current tests.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.
Thanks for checking!
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.
But this is still strange. The memOutOfBounds analysis should only consider dereferencing. If a pointer points one or more past the end should make no difference if it's never dereferenced. It would only matter for the valid-memtrack subproperty which is handled by memLeak (which has many other problems).
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'll merge this so we can see if it makes any difference in the knightly.
This strangeness already existed before and could be fixed separately.