-
Notifications
You must be signed in to change notification settings - Fork 1.7k
C#: Rewrite nullness queries #593
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
Conversation
Port many of the nullness test from Java, as well as add new tests.
Port the SSA-based logic from the Java nullness analyses.
For example, in ``` void M(object x) { var y = x != null ? "" : null; if (y != null) x.ToString(); } ``` the guard `y != null` implies that the guard `x != null` must be true.
For example, in ``` void M(object x) { var y = x == null ? 1 : 2; if (y == 2) x.ToString(); } ``` the guard `y == 2` implies that the guard `x == null` must be false, as the assignment of `2` to `y` is unique.
86084d0
to
d25bd59
Compare
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.
The 'Changes to existing queries' table in the change-note isn't formatted correctly at the moment. Otherwise, the text and the qhelp files LGTM.
@@ -11,6 +11,8 @@ | |||
|
|||
| *@name of query (Query ID)*| *Impact on results* | *How/why the query has changed* | | |||
| Off-by-one comparison against container length (cs/index-out-of-bounds) | Fewer false positives | Results have been removed when there are additional guards on the index. | |
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.
This table needs a row of pipes and dashes (e.g. |--------|----------|---------|) beneath the column headings.
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 have to say this is extremely awesome! A few minor comments that's all.
csharp/ql/src/CSI/NullAlways.qhelp
Outdated
<overview> | ||
<p>If a variable is dereferenced, and the variable has a null value on all possible execution paths | ||
leading to the dereferencing, it is guaranteed to result in a <code>NullReferenceException</code>. | ||
<p>If a variable is dereferenced, and the variable has a <code>null</code> |
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.
Might be worth explaining what "dereference" means.
return !(b1 == b2); | ||
} | ||
|
||
public struct CoOrds |
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.
Coords
{ | ||
public int x, y; | ||
|
||
public CoOrds(int p1, int p2) |
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.
Coords
csharp/ql/src/CSI/NullAlways.ql
Outdated
select access, "Variable $@ is always null here.", var, var.getName() | ||
from Dereference d, Ssa::SourceVariable v | ||
where d.isFirstAlwaysNull(v) | ||
select d, "Variable '$@' is always null here.", v, v.toString() |
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.
Single quotes are not needed around substitutions.
This PR rewrites the nullness queries from scratch, very much based on a similar rewrite for Java done by @aschackmull. Most of the work is done in 055351a, and in addition to reviewing commit-by-commit, I suggest reviewing
Nullness.qll
on that commit as-is, rather than looking at the diff.The maybe-
null
query still has some false positives, exemplified via tests ported from Java. My plan is to address those FPs (using control flow graph splitting) in follow-up pull requests.Review: @calumgrant (QL), @jf205 (qhelp + change notes).