Skip to content

Commit 62c8f28

Browse files
committed
C#: Bugfix for nullguards for complex patterns.
1 parent 7670a2b commit 62c8f28

File tree

1 file changed

+26
-2
lines changed
  • csharp/ql/lib/semmle/code/csharp/controlflow

1 file changed

+26
-2
lines changed

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,31 @@ module AbstractValues {
273273

274274
private import AbstractValues
275275

276+
/** Gets the value resulting from matching `null` against `pat`. */
277+
private boolean patternContainsNull(PatternExpr pat) {
278+
pat instanceof NullLiteral and result = true
279+
or
280+
not pat instanceof NullLiteral and
281+
not pat instanceof NotPatternExpr and
282+
not pat instanceof OrPatternExpr and
283+
not pat instanceof AndPatternExpr and
284+
result = false
285+
or
286+
result = patternContainsNull(pat.(NotPatternExpr).getPattern()).booleanNot()
287+
or
288+
exists(OrPatternExpr ope | pat = ope |
289+
result =
290+
patternContainsNull(ope.getLeftOperand())
291+
.booleanOr(patternContainsNull(ope.getRightOperand()))
292+
)
293+
or
294+
exists(AndPatternExpr ape | pat = ape |
295+
result =
296+
patternContainsNull(ape.getLeftOperand())
297+
.booleanAnd(patternContainsNull(ape.getRightOperand()))
298+
)
299+
}
300+
276301
pragma[nomagic]
277302
private predicate typePattern(PatternMatch pm, TypePatternExpr tpe, Type t) {
278303
tpe = pm.getPattern() and
@@ -362,8 +387,7 @@ class DereferenceableExpr extends Expr {
362387
isNull = branch
363388
or
364389
// E.g. `x is string` or `x is ""`
365-
not pm.getPattern() instanceof NullLiteral and
366-
branch = true and
390+
branch.booleanNot() = patternContainsNull(pm.getPattern()) and
367391
isNull = false
368392
or
369393
exists(TypePatternExpr tpe |

0 commit comments

Comments
 (0)