Proposed patch implements undefbehavior.ZeroAllocDereference checker as part of unix.Malloc and cplusplus.NewDelete checkers.
The patch doesn't handle zero-size realloc()s due to specifics of current handling of realloc() by unix.Malloc that need to be discussed.
Please review!
Details
Diff Detail
Event Timeline
**As a rule of thumb, checkers should be stateless.
http://clang-analyzer.llvm.org/checker_dev_manual.html
When you introduce mutable members you are most likely making a mistake. The state should track properties of symbols; specifically to check with symbol corresponds to a '0' allocation.
The specific example that might break with your patch (depending on the order in which the states are being explored) is something along these lines:
if (b)
s= 10;
else
s = 0;
p = malloc(s);
if (b)
*p = 1;
When the checker explores "malloc(s)" along the "s=0" path, the expression will be added to the set. If "*p = 1" along the "s=10" path is explored later on, we are going to produce a false positive.
Please, provide better testing so the cases like this one are exposed.
lib/StaticAnalyzer/Checkers/MallocChecker.cpp | ||
---|---|---|
67–68 | I think you could just fold it into the Kind, by adding AllocatedOfSizeZero or do we think that Relinquished or Escaped should be treated differently if they were zero allocated..? | |
850 | "ProcessZeroAllocation" ? We are not checking anything here. | |
895 | It should not be possible to have non allocated symbol here.. Is it? Maybe we should assert? | |
1860 | I's call this "Use of zero allocated" or "Zero allocation" | |
2311 | this seems unrelated to the patch. Can it be submitted separately with a testcase that it is trying to address? |
Thanks for review!
lib/StaticAnalyzer/Checkers/MallocChecker.cpp | ||
---|---|---|
67–68 | Implemented with a new AllocatedOfSizeZero kind. Theoretically Relinquished may be treated differently if we would like to track usage of zero-allocated memory after relinquish but there are no cases yet. | |
850 | Fixed! | |
895 | Agree, done! | |
1860 | Done! | |
2311 | Cleaned. |
.
lib/StaticAnalyzer/Checkers/MallocChecker.cpp | ||
---|---|---|
895 | Pardon, currently zero-allocated realloc do not attach a RefState so it is still early to assert for now. |
Hi Anton,
Have you tested the patch on anything but the regression tests? If yes,
what are the results? Did this catch any issues? Are there any false
positives? Since this will be a turned on by default new warning, I'd like
make sure we test on real code before committing.
Other than testing, this looks good to me. Thank you!
Anna.
I think you could just fold it into the Kind, by adding AllocatedOfSizeZero or do we think that Relinquished or Escaped should be treated differently if they were zero allocated..?