Skip to content

Commit 9d7c9e0

Browse files
committed
C#: Default parameter values are maybe null
C#: Update test output
1 parent 631b424 commit 9d7c9e0

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,17 @@ private predicate isMaybeNullArgument(Ssa::ExplicitDefinition def, MaybeNullExpr
176176
pdef = def.getADefinition()
177177
|
178178
p = pdef.getParameter().getSourceDeclaration() and
179-
p.getAnAssignedArgument() = arg and
179+
arg = p.getAnAssignedArgument() and
180+
not arg.getEnclosingCallable().getEnclosingCallable*() instanceof TestMethod
181+
)
182+
}
183+
184+
private predicate isNullDefaultArgument(Ssa::ExplicitDefinition def, MaybeNullExpr arg) {
185+
exists(AssignableDefinitions::ImplicitParameterDefinition pdef, Parameter p |
186+
pdef = def.getADefinition()
187+
|
188+
p = pdef.getParameter().getSourceDeclaration() and
189+
arg = p.getDefaultValue() and
180190
not arg.getEnclosingCallable().getEnclosingCallable*() instanceof TestMethod
181191
)
182192
}
@@ -204,6 +214,8 @@ private predicate defMaybeNull(Ssa::Definition def, string msg, Element reason)
204214
else msg = "because of $@ potential null argument"
205215
)
206216
or
217+
isNullDefaultArgument(def, reason) and msg = "because the parameter has a null default value"
218+
or
207219
// If the source of a variable is `null` then the variable may be `null`
208220
exists(AssignableDefinition adef | adef = def.(Ssa::ExplicitDefinition).getADefinition() |
209221
adef.getSource() instanceof MaybeNullExpr and

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ static void Ex33(string s, object o)
363363
x.ToString(); // GOOD
364364
}
365365

366-
static int Ex34(string s = null) => s.Length; // BAD (maybe) False negative
366+
static int Ex34(string s = null) => s.Length; // BAD (maybe)
367367
}
368368

369369
public static class Extensions

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,10 +1130,10 @@
11301130
| E.cs:199:9:199:9 | access to local variable o | non-empty | E.cs:198:17:198:29 | ... ? ... : ... | non-empty |
11311131
| E.cs:199:9:199:9 | access to local variable o | non-null | E.cs:198:17:198:29 | ... ? ... : ... | non-null |
11321132
| E.cs:199:9:199:9 | access to local variable o | null | E.cs:198:17:198:29 | ... ? ... : ... | null |
1133-
| E.cs:201:11:201:11 | access to local variable o | non-null | E.cs:198:17:198:29 | ... ? ... : ... | non-null |
1134-
| E.cs:201:11:201:11 | access to local variable o | null | E.cs:198:17:198:29 | ... ? ... : ... | null |
1135-
| E.cs:203:11:203:11 | access to local variable o | non-null | E.cs:198:17:198:29 | ... ? ... : ... | non-null |
1136-
| E.cs:203:11:203:11 | access to local variable o | null | E.cs:198:17:198:29 | ... ? ... : ... | null |
1133+
| E.cs:201:13:201:13 | access to local variable o | non-null | E.cs:198:17:198:29 | ... ? ... : ... | non-null |
1134+
| E.cs:201:13:201:13 | access to local variable o | null | E.cs:198:17:198:29 | ... ? ... : ... | null |
1135+
| E.cs:203:13:203:13 | access to local variable o | non-null | E.cs:198:17:198:29 | ... ? ... : ... | non-null |
1136+
| E.cs:203:13:203:13 | access to local variable o | null | E.cs:198:17:198:29 | ... ? ... : ... | null |
11371137
| E.cs:208:13:208:23 | ... is ... | false | E.cs:208:13:208:13 | access to parameter s | null |
11381138
| E.cs:208:13:208:23 | ... is ... | true | E.cs:208:13:208:13 | access to parameter s | non-null |
11391139
| E.cs:220:13:220:13 | access to local variable x | non-null | E.cs:217:17:217:20 | null | non-null |

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,8 @@ nodes
351351
| E.cs:343:9:343:9 | access to local variable x |
352352
| E.cs:348:17:348:36 | SSA def(x) |
353353
| E.cs:349:9:349:9 | access to local variable x |
354+
| E.cs:366:28:366:28 | SSA param(s) |
355+
| E.cs:366:41:366:41 | access to parameter s |
354356
| Forwarding.cs:7:16:7:23 | SSA def(s) |
355357
| Forwarding.cs:14:9:17:9 | if (...) ... |
356358
| Forwarding.cs:19:9:22:9 | if (...) ... |
@@ -682,6 +684,7 @@ edges
682684
| E.cs:330:13:330:36 | SSA def(x) | E.cs:331:9:331:9 | access to local variable x |
683685
| E.cs:342:13:342:32 | SSA def(x) | E.cs:343:9:343:9 | access to local variable x |
684686
| E.cs:348:17:348:36 | SSA def(x) | E.cs:349:9:349:9 | access to local variable x |
687+
| E.cs:366:28:366:28 | SSA param(s) | E.cs:366:41:366:41 | access to parameter s |
685688
| Forwarding.cs:7:16:7:23 | SSA def(s) | Forwarding.cs:14:9:17:9 | if (...) ... |
686689
| Forwarding.cs:14:9:17:9 | if (...) ... | Forwarding.cs:19:9:22:9 | if (...) ... |
687690
| Forwarding.cs:19:9:22:9 | if (...) ... | Forwarding.cs:24:9:27:9 | if (...) ... |
@@ -782,6 +785,7 @@ edges
782785
| E.cs:302:9:302:9 | access to local variable s | E.cs:301:13:301:27 | SSA def(s) | E.cs:302:9:302:9 | access to local variable s | Variable $@ may be null here because of $@ assignment. | E.cs:301:13:301:13 | s | s | E.cs:301:13:301:27 | String s = ... | this |
783786
| E.cs:343:9:343:9 | access to local variable x | E.cs:342:13:342:32 | SSA def(x) | E.cs:343:9:343:9 | access to local variable x | Variable $@ may be null here because of $@ assignment. | E.cs:342:13:342:13 | x | x | E.cs:342:13:342:32 | String x = ... | this |
784787
| E.cs:349:9:349:9 | access to local variable x | E.cs:348:17:348:36 | SSA def(x) | E.cs:349:9:349:9 | access to local variable x | Variable $@ may be null here because of $@ assignment. | E.cs:348:17:348:17 | x | x | E.cs:348:17:348:36 | dynamic x = ... | this |
788+
| E.cs:366:41:366:41 | access to parameter s | E.cs:366:28:366:28 | SSA param(s) | E.cs:366:41:366:41 | access to parameter s | Variable $@ may be null here because the parameter has a null default value. | E.cs:366:28:366:28 | s | s | E.cs:366:32:366:35 | null | this |
785789
| GuardedString.cs:35:31:35:31 | access to local variable s | GuardedString.cs:7:16:7:32 | SSA def(s) | 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 |
786790
| NullMaybeBad.cs:7:27:7:27 | access to parameter o | NullMaybeBad.cs:13:17:13:20 | null | 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 |
787791
| StringConcatenation.cs:16:17:16:17 | access to local variable s | StringConcatenation.cs:14:16:14:23 | SSA def(s) | 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)