This is an archive of the discontinued LLVM Phabricator instance.

[analyzer] Do not infer nullability inside function-like macros, even when macro is explicitly returning NULL
ClosedPublic

Authored by george.karpenkov on Jan 22 2018, 6:50 PM.

Details

Summary

We already suppress such reports for inlined functions, we should then get the same behavior for macros.
The underlying reason is that the same macro, can be called from many different contexts, and nullability can only be expected in _some_ of them.
Assuming that the macro can return null in _all_ of them sometimes leads to a large number of false positives.

E.g. consider the test case for the dynamic cast implementation in macro: in such cases, the bug report is unwanted.

Tracked in rdar://36304776

Diff Detail

Repository
rC Clang

Event Timeline

NoQ added inline comments.Jan 26 2018, 3:49 PM
lib/StaticAnalyzer/Core/BugReporterVisitors.cpp
218

This hides the other local variable.

241–254

I wonder if at least some of this code can be re-used across visitors. Finding assignments sounds very common.

NoQ added a comment.Jan 26 2018, 3:52 PM

Also, hmm, for plain functions we only make this suppression when the value is a null pointer, not when it's a null integer (see ReturnVisitor::addVisitorIfNecessary). I suspect it we might want to behave identically(?)

lib/StaticAnalyzer/Core/BugReporterVisitors.cpp
218

yeah other one is not doing anything, should be removed.

NoQ added inline comments.Jan 27 2018, 11:51 AM
lib/StaticAnalyzer/Core/BugReporterVisitors.cpp
242

Actually i suspect that it'd always be a single decl. Because for the purposes of analysis, during CFG construction, we modify the AST to split out declstmts of multiple variables into multiple single-variable declstmts. Could you see if dyn_cast<VarDecl>(DS->getSingleDecl()) works?

george.karpenkov marked 2 inline comments as done.Feb 2 2018, 1:10 PM
george.karpenkov added inline comments.
lib/StaticAnalyzer/Core/BugReporterVisitors.cpp
241–254

@NoQ haven't really seen anything useful apart from isInitializationOfVar, but that requires knowing in advance the required region (assuming you mean matching assignment together with declaration)

One issue not done is *not* suppressing reports for division by zero in same scenarios.
I don't see an easy way to do this without adding quite a bit of complexity (additional parsing of statement...)
Do we really want to do this? Is the complexity worse it? Why do we even want a different behavior for those?

NoQ accepted this revision.Feb 2 2018, 4:40 PM

This sounds like a tiny loss of coverage for division by zero checker. I guess it's an unfortunate side effect that indeed rather deserves a FIXME than blocking the patch on it.

This revision is now accepted and ready to land.Feb 2 2018, 4:40 PM

Updated a fix for division-by-zero

This revision was automatically updated to reflect the committed changes.
NoQ added a comment.Feb 2 2018, 4:59 PM

Woohoo even better.