Skip to content

Commit 078dc7b

Browse files
committed
C#: Fix false positives in cs/dereferenced-value-may-be-null
1 parent 287ce4e commit 078dc7b

File tree

6 files changed

+14
-5
lines changed

6 files changed

+14
-5
lines changed

csharp/ql/src/semmle/code/csharp/controlflow/Guards.qll

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,8 +578,12 @@ module Internal {
578578
*/
579579
Expr getNullEquivParent(Expr e) {
580580
result = any(QualifiableExpr qe |
581-
qe.getQualifier() = e and
582581
qe.isConditional() and
582+
(
583+
e = qe.getQualifier()
584+
or
585+
e = qe.(ExtensionMethodCall).getArgument(0)
586+
) and
583587
(
584588
// The accessed declaration must have a value type in order
585589
// for `only if` to hold

csharp/ql/src/semmle/code/csharp/dataflow/Nullness.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ private predicate defMaybeNull(Ssa::Definition def, string msg, Element reason)
155155
reason = de.getANullCheck(_, true) and
156156
msg = "as suggested by $@ null check" and
157157
not def instanceof Ssa::PseudoDefinition and
158+
strictcount(Location l |
159+
l = any(Ssa::Definition def0 | de = def0.getARead()).getLocation()
160+
) = 1 and
158161
not nonNullDef(def) and
159162
// Don't use a check as reason if there is a `null` assignment
160163
not def.(Ssa::ExplicitDefinition).getADefinition().getSource() instanceof MaybeNullExpr

csharp/ql/test/query-tests/Nullness/E.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public void Ex22()
267267
try
268268
{
269269
o = Make();
270-
o.ToString(); // GOOD (false positive)
270+
o.ToString(); // GOOD
271271
}
272272
finally
273273
{
@@ -292,7 +292,7 @@ public void Ex24(bool b)
292292
string s = b ? null : "";
293293
if (s?.M2() == 0)
294294
{
295-
s.ToString(); // GOOD (false positive)
295+
s.ToString(); // GOOD
296296
}
297297
}
298298

csharp/ql/test/query-tests/Nullness/Implications.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,9 @@
14531453
| E.cs:293:13:293:13 | access to local variable s | non-null | E.cs:292:20:292:32 | ... ? ... : ... | non-null |
14541454
| E.cs:293:13:293:13 | access to local variable s | null | E.cs:292:20:292:20 | access to parameter b | true |
14551455
| E.cs:293:13:293:13 | access to local variable s | null | E.cs:292:20:292:32 | ... ? ... : ... | null |
1456+
| E.cs:293:13:293:24 | ... == ... | true | E.cs:293:15:293:19 | call to method M2 | non-null |
1457+
| E.cs:293:15:293:19 | call to method M2 | non-null | E.cs:293:13:293:13 | access to local variable s | non-null |
1458+
| E.cs:293:15:293:19 | call to method M2 | null | E.cs:293:13:293:13 | access to local variable s | null |
14561459
| E.cs:295:13:295:13 | access to local variable s | non-null | E.cs:292:20:292:32 | ... ? ... : ... | non-null |
14571460
| E.cs:295:13:295:13 | access to local variable s | null | E.cs:292:20:292:32 | ... ? ... : ... | null |
14581461
| E.cs:300:22:300:38 | ... ? ... : ... | non-null | E.cs:300:22:300:26 | access to field Field | false |

csharp/ql/test/query-tests/Nullness/NullCheck.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@
199199
| E.cs:284:9:284:9 | access to local variable o | E.cs:284:9:284:9 | access to local variable o | null | true |
200200
| E.cs:293:13:293:13 | access to local variable s | E.cs:293:13:293:13 | access to local variable s | non-null | false |
201201
| E.cs:293:13:293:13 | access to local variable s | E.cs:293:13:293:13 | access to local variable s | null | true |
202+
| E.cs:293:13:293:24 | ... == ... | E.cs:293:15:293:19 | call to method M2 | true | false |
202203
| Forwarding.cs:9:14:9:30 | call to method IsNullOrEmpty | Forwarding.cs:9:14:9:14 | access to local variable s | false | false |
203204
| Forwarding.cs:14:13:14:32 | call to method IsNotNullOrEmpty | Forwarding.cs:14:13:14:13 | access to local variable s | true | false |
204205
| Forwarding.cs:19:14:19:23 | call to method IsNull | Forwarding.cs:19:14:19:14 | access to local variable s | false | false |

csharp/ql/test/query-tests/Nullness/NullMaybe.expected

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@
6868
| E.cs:230:9:230:9 | access to local variable x | Variable $@ may be null here because of $@ assignment. | E.cs:225:13:225:13 | x | x | E.cs:227:13:227:20 | ... = ... | this |
6969
| E.cs:235:16:235:16 | access to parameter i | Variable $@ may be null here because it has a nullable type. | E.cs:233:26:233:26 | i | i | E.cs:233:26:233:26 | i | this |
7070
| E.cs:240:21:240:21 | access to parameter i | Variable $@ may be null here because it has a nullable type. | E.cs:238:26:238:26 | i | i | E.cs:238:26:238:26 | i | this |
71-
| E.cs:270:13:270:13 | access to local variable o | Variable $@ may be null here as suggested by $@ null check. | E.cs:266:16:266:16 | o | o | E.cs:274:17:274:25 | ... != ... | this |
7271
| E.cs:285:9:285:9 | access to local variable o | Variable $@ may be null here as suggested by $@ null check. | E.cs:283:13:283:13 | o | o | E.cs:284:9:284:9 | access to local variable o | this |
73-
| E.cs:295:13:295:13 | access to local variable s | Variable $@ may be null here because of $@ assignment. | E.cs:292:16:292:16 | s | s | E.cs:292:16:292:32 | String s = ... | this |
7472
| GuardedString.cs:35:31:35:31 | access to local variable s | Variable $@ may be null here because of $@ assignment. | GuardedString.cs:7:16:7:16 | s | s | GuardedString.cs:7:16:7:32 | String s = ... | this |
7573
| NullMaybeBad.cs:7:27:7:27 | access to parameter o | Variable $@ may be null here because of $@ null argument. | NullMaybeBad.cs:5:25:5:25 | o | o | NullMaybeBad.cs:13:17:13:20 | null | this |
7674
| StringConcatenation.cs:16:17:16:17 | access to local variable s | Variable $@ may be null here because of $@ assignment. | StringConcatenation.cs:14:16:14:16 | s | s | StringConcatenation.cs:14:16:14:23 | String s = ... | this |

0 commit comments

Comments
 (0)