Skip to content

Commit 54f8d3e

Browse files
committed
C#: Update guards library as assign operations are not expanded.
1 parent 13267bc commit 54f8d3e

File tree

1 file changed

+22
-12
lines changed
  • csharp/ql/lib/semmle/code/csharp/controlflow

1 file changed

+22
-12
lines changed

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

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,10 @@ private module LogicInput implements GuardsImpl::LogicInputSig {
296296
v1.isNullValue() and
297297
v2 = v1
298298
or
299+
g2 = g1.(AssignCoalesceExpr).getAnOperand() and
300+
v1.isNullValue() and
301+
v2 = v1
302+
or
299303
exists(Assertion assert, AssertMethod target, int i |
300304
assert.getAssertMethod() = target and
301305
g1 = assert and
@@ -841,13 +845,17 @@ module Internal {
841845
e1 = e2.(Cast).getExpr()
842846
or
843847
e2 = e1.(NullCoalescingExpr).getAnOperand()
848+
or
849+
e2 = e1.(AssignCoalesceExpr).getAnOperand()
844850
}
845851

846852
/** Holds if expression `e3` is a `null` value whenever `e1` and `e2` are. */
847853
predicate nullValueImpliedBinary(Expr e1, Expr e2, Expr e3) {
848854
e3 = any(ConditionalExpr ce | e1 = ce.getThen() and e2 = ce.getElse())
849855
or
850856
e3 = any(NullCoalescingExpr nce | e1 = nce.getLeftOperand() and e2 = nce.getRightOperand())
857+
or
858+
e3 = any(AssignCoalesceExpr ace | e1 = ace.getLeftOperand() and e2 = ace.getRightOperand())
851859
}
852860

853861
predicate nullValueImplied(Expr e) {
@@ -922,11 +930,10 @@ module Internal {
922930

923931
/** Holds if expression `e2` is a non-`null` value whenever `e1` is. */
924932
predicate nonNullValueImpliedUnary(Expr e1, Expr e2) {
925-
e1 = e2.(CastExpr).getExpr()
926-
or
927-
e1 = e2.(AssignExpr).getRValue()
928-
or
929-
e1 = e2.(NullCoalescingExpr).getAnOperand()
933+
e1 = e2.(CastExpr).getExpr() or
934+
e1 = e2.(AssignExpr).getRValue() or
935+
e1 = e2.(NullCoalescingExpr).getAnOperand() or
936+
e1 = e2.(AssignCoalesceExpr).getAnOperand()
930937
}
931938

932939
/**
@@ -953,10 +960,13 @@ module Internal {
953960
)
954961
or
955962
// In C#, `null + 1` has type `int?` with value `null`
956-
exists(BinaryArithmeticOperation bao, Expr o |
957-
result = bao and
958-
bao.getAnOperand() = e and
959-
bao.getAnOperand() = o and
963+
exists(BinaryOperation bo, Expr o |
964+
bo instanceof BinaryArithmeticOperation or
965+
bo instanceof AssignArithmeticOperation
966+
|
967+
result = bo and
968+
bo.getAnOperand() = e and
969+
bo.getAnOperand() = o and
960970
// The other operand must be provably non-null in order
961971
// for `only if` to hold
962972
nonNullValueImplied(o) and
@@ -972,10 +982,10 @@ module Internal {
972982
any(QualifiableExpr qe |
973983
qe.isConditional() and
974984
result = qe.getQualifier()
975-
)
976-
or
985+
) or
977986
// In C#, `null + 1` has type `int?` with value `null`
978-
e = any(BinaryArithmeticOperation bao | result = bao.getAnOperand())
987+
e = any(BinaryArithmeticOperation bao | result = bao.getAnOperand()) or
988+
e = any(AssignArithmeticOperation aao | result = aao.getAnOperand())
979989
}
980990

981991
deprecated predicate isGuard(Expr e, GuardValue val) {

0 commit comments

Comments
 (0)