Skip to content

Commit acd6f41

Browse files
committed
C#: Avoid computing full TC in DangerousNonShortCircuitLogic.ql
1 parent e22d3a1 commit acd6f41

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

csharp/ql/src/Likely Bugs/DangerousNonShortCircuitLogic.ql

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,6 @@
1515

1616
import csharp
1717

18-
/** An expression containing a qualified member access, a method call, or an array access. */
19-
class DangerousExpression extends Expr {
20-
DangerousExpression() {
21-
exists(Expr e | this = e.getParent*() |
22-
exists(Expr q | q = e.(MemberAccess).getQualifier() |
23-
not q instanceof ThisAccess and
24-
not q instanceof BaseAccess
25-
)
26-
or
27-
e instanceof MethodCall
28-
or
29-
e instanceof ArrayAccess
30-
) and
31-
not exists(Expr e | this = e.getParent*() | e.(Call).getTarget().getAParameter().isOutOrRef())
32-
}
33-
}
34-
3518
/** A use of `&` or `|` on operands of type boolean. */
3619
class NonShortCircuit extends BinaryBitwiseOperation {
3720
NonShortCircuit() {
@@ -42,10 +25,40 @@ class NonShortCircuit extends BinaryBitwiseOperation {
4225
) and
4326
not exists(AssignBitwiseOperation abo | abo.getExpandedAssignment().getRValue() = this) and
4427
this.getLeftOperand().getType() instanceof BoolType and
45-
this.getRightOperand().getType() instanceof BoolType and
46-
this.getRightOperand() instanceof DangerousExpression
28+
this.getRightOperand().getType() instanceof BoolType
29+
}
30+
31+
pragma[nomagic]
32+
private predicate hasRightOperandDescendant(Expr e) {
33+
e = this.getRightOperand()
34+
or
35+
exists(Expr parent |
36+
this.hasRightOperandDescendant(parent) and
37+
e.getParent() = parent
38+
)
39+
}
40+
41+
/**
42+
* Holds if this non-short-circuit expression contains a qualified member access,
43+
* a method call, or an array access inside the right operand.
44+
*/
45+
predicate isDangerous() {
46+
exists(Expr e | this.hasRightOperandDescendant(e) |
47+
exists(Expr q | q = e.(MemberAccess).getQualifier() |
48+
not q instanceof ThisAccess and
49+
not q instanceof BaseAccess
50+
)
51+
or
52+
e instanceof MethodCall
53+
or
54+
e instanceof ArrayAccess
55+
) and
56+
not exists(Expr e | this.hasRightOperandDescendant(e) |
57+
e.(Call).getTarget().getAParameter().isOutOrRef()
58+
)
4759
}
4860
}
4961

5062
from NonShortCircuit e
63+
where e.isDangerous()
5164
select e, "Potentially dangerous use of non-short circuit logic."

0 commit comments

Comments
 (0)