This is an archive of the discontinued LLVM Phabricator instance.

[analyzer] A fix for substraction of an integer from a pointer.
ClosedPublic

Authored by NoQ on Sep 9 2015, 4:46 AM.

Details

Summary

In Clang Static Analyzer, there's a typo in evalBinOpLN() which leads to incorrect modeling of pointer arithmetic: when substracting an integral value from a pointer value (assuming the latter is a SubRegion that is not an ElementRegion), the index value of the newly produced ElementRegion has incorrect sign.

The patch fixes the problem and adds relevant regression tests.

Diff Detail

Repository
rL LLVM

Event Timeline

NoQ updated this revision to Diff 34318.Sep 9 2015, 4:46 AM
NoQ retitled this revision from to [analyzer] A fix for substraction of an integer from a pointer..
NoQ updated this object.
NoQ added reviewers: zaks.anna, jordan_rose, krememek.
NoQ set the repository for this revision to rL LLVM.
NoQ added a subscriber: dergachev.a.
NoQ updated this revision to Diff 34320.Sep 9 2015, 4:59 AM
NoQ removed rL LLVM as the repository for this revision.
NoQ added a subscriber: cfe-commits.
zaks.anna edited edge metadata.Sep 9 2015, 3:34 PM

Let's make the test more explicit about what is being tested; for example,
void negativeIndex(char *str) {

char *ptr = str + 1;
*ptr = 'a';
clang_analyzer_eval(*ptr == 'a'); // expected-warning{{TRUE}}
ptr = str - 1;
clang_analyzer_eval(*ptr == 'a'); // expected-warning{{UNKNOWN}}
clang_analyzer_eval(*(str + 1) == 'a'); // expected-warning{{TRUE}}
ptr = str;
ptr -= 1;
clang_analyzer_eval(*ptr == 'a'); // expected-warning{{UNKNOWN}}
ptr = str;
--ptr;
clang_analyzer_eval(*ptr == 'a'); // expected-warning{{UNKNOWN}}

}

Otherwise, LGTM

NoQ updated this revision to Diff 34423.Sep 10 2015, 2:58 AM
NoQ edited edge metadata.

Make the tests easier to understand.

NoQ added a comment.Sep 10 2015, 3:54 AM

Thanks, fixed :)

zaks.anna accepted this revision.Sep 10 2015, 6:54 PM
zaks.anna edited edge metadata.
This revision is now accepted and ready to land.Sep 10 2015, 6:54 PM
In D12725#243150, @NoQ wrote:

Thanks, fixed :)

Can you commit this or do you need someone to commit this for you?

NoQ added a comment.Sep 18 2015, 11:28 AM

I've got no commit access yet, sorry, that's my first patch here actually :)

This revision was automatically updated to reflect the committed changes.
In D12725#248931, @NoQ wrote:

I've got no commit access yet, sorry, that's my first patch here actually :)

No problem, I committed it in r248021. Thank you for your contribution!