Skip to content

Commit dc4abc9

Browse files
committed
C#: Replace references to getANullCheck.
1 parent 46fc006 commit dc4abc9

File tree

5 files changed

+18
-7
lines changed

5 files changed

+18
-7
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,17 @@ class DereferenceableExpr extends Expr {
776776
or
777777
result = this.getANullnessNullCheck(v, isNull)
778778
}
779+
780+
/** Holds if `guard` suggests that this expression may be `null`. */
781+
predicate guardSuggestsMaybeNull(Guards::Guard guard) {
782+
guard = this.getANullnessNullCheck(_, true)
783+
or
784+
LogicInput::additionalNullCheck(guard, _, this, true)
785+
or
786+
guard.isEquality(this, any(Expr n | nullValueImplied(n)), _)
787+
or
788+
Guards::nullGuard(guard, any(GuardValue v | exists(v.asBooleanValue())), this, true)
789+
}
779790
}
780791

781792
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ private predicate defMaybeNull(
193193
(
194194
// A variable compared to `null` might be `null`
195195
exists(G::DereferenceableExpr de | de = def.getARead() |
196-
reason = de.getANullCheck(_, true) and
196+
de.guardSuggestsMaybeNull(reason) and
197197
msg = "as suggested by $@ null check" and
198198
node = def.getControlFlowNode() and
199199
not de = any(Ssa::PhiNode phi).getARead() and

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
| C.cs:45:22:45:30 | ... != ... | C.cs:45:22:45:22 | access to local variable s |
3434
| C.cs:56:23:56:24 | access to local variable o2 | C.cs:56:23:56:24 | access to local variable o2 |
3535
| C.cs:71:26:71:27 | access to local variable o3 | C.cs:71:26:71:27 | access to local variable o3 |
36-
| C.cs:78:13:78:24 | call to method IsNotNull | C.cs:78:23:78:23 | access to local variable o |
37-
| C.cs:81:14:81:22 | call to method IsNull | C.cs:81:21:81:21 | access to local variable o |
3836
| C.cs:113:22:113:36 | ... == ... | C.cs:113:22:113:28 | access to local variable colours |
3937
| C.cs:113:22:113:36 | ... == ... | C.cs:113:33:113:36 | null |
4038
| C.cs:120:13:120:28 | ... == ... | C.cs:120:13:120:20 | access to local variable children |
@@ -144,8 +142,7 @@
144142
| E.cs:422:13:422:22 | access to property HasValue | E.cs:422:13:422:13 | access to parameter i |
145143
| E.cs:429:13:429:22 | access to property HasValue | E.cs:429:13:429:13 | access to parameter i |
146144
| E.cs:437:23:437:31 | ... is ... | E.cs:437:23:437:23 | access to parameter s |
147-
| Forwarding.cs:19:14:19:23 | call to method IsNull | Forwarding.cs:19:14:19:14 | access to local variable s |
148-
| Forwarding.cs:24:13:24:25 | call to method IsNotNull | Forwarding.cs:24:13:24:13 | access to local variable s |
145+
| E.cs:447:23:447:35 | ... is ... | E.cs:447:23:447:23 | access to parameter s |
149146
| Forwarding.cs:45:66:45:75 | call to method IsNull | Forwarding.cs:45:66:45:66 | access to parameter o |
150147
| Forwarding.cs:59:13:59:21 | ... == ... | Forwarding.cs:59:13:59:13 | access to parameter o |
151148
| Forwarding.cs:78:16:78:39 | call to method ReferenceEquals | Forwarding.cs:78:32:78:32 | access to parameter o |
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import csharp
22
import semmle.code.csharp.controlflow.Guards
33

4-
from DereferenceableExpr de
5-
select de.getANullCheck(_, true), de
4+
from DereferenceableExpr de, Guards::Guard reason
5+
where de.guardSuggestsMaybeNull(reason)
6+
select reason, de

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@
5757
| E.cs:423:38:423:38 | access to parameter i | Variable $@ may be null at this access because it has a nullable type. | E.cs:420:27:420:27 | i | i | E.cs:420:27:420:27 | i | this |
5858
| E.cs:430:39:430:39 | access to parameter i | Variable $@ may be null at this access because it has a nullable type. | E.cs:427:27:427:27 | i | i | E.cs:427:27:427:27 | i | this |
5959
| E.cs:444:13:444:13 | access to parameter s | Variable $@ may be null at this access as suggested by $@ null check. | E.cs:435:29:435:29 | s | s | E.cs:437:23:437:31 | ... is ... | this |
60+
| E.cs:444:13:444:13 | access to parameter s | Variable $@ may be null at this access as suggested by $@ null check. | E.cs:435:29:435:29 | s | s | E.cs:447:23:447:35 | ... is ... | this |
6061
| E.cs:459:13:459:13 | access to parameter s | Variable $@ may be null at this access as suggested by $@ null check. | E.cs:435:29:435:29 | s | s | E.cs:437:23:437:31 | ... is ... | this |
62+
| E.cs:459:13:459:13 | access to parameter s | Variable $@ may be null at this access as suggested by $@ null check. | E.cs:435:29:435:29 | s | s | E.cs:447:23:447:35 | ... is ... | this |
6163
| GuardedString.cs:35:31:35:31 | access to local variable s | Variable $@ may be null at this access because of $@ assignment. | GuardedString.cs:7:16:7:16 | s | s | GuardedString.cs:7:16:7:32 | String s = ... | this |
6264
| NullMaybeBad.cs:7:27:7:27 | access to parameter o | Variable $@ may be null at this access because of $@ null argument. | NullMaybeBad.cs:5:25:5:25 | o | o | NullMaybeBad.cs:13:17:13:20 | null | this |
6365
| Params.cs:14:17:14:20 | access to parameter args | Variable $@ may be null at this access because of $@ null argument. | Params.cs:12:36:12:39 | args | args | Params.cs:20:12:20:15 | null | this |

0 commit comments

Comments
 (0)