-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathDangerousNonShortCircuitLogic.ql
More file actions
63 lines (58 loc) · 1.84 KB
/
DangerousNonShortCircuitLogic.ql
File metadata and controls
63 lines (58 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* @name Potentially dangerous use of non-short-circuit logic
* @description The & and | operators do not use short-circuit evaluation and can be dangerous when applied to boolean operands. In particular, their
* use can result in errors if the left-hand operand checks for cases in which it is not safe to evaluate the right-hand one.
* @kind problem
* @problem.severity error
* @precision high
* @id cs/non-short-circuit
* @tags quality
* reliability
* correctness
* external/cwe/cwe-480
* external/cwe/cwe-691
*/
import csharp
/** A use of `&` or `|` on operands of type boolean. */
class NonShortCircuit extends BinaryBitwiseOperation {
NonShortCircuit() {
(
this instanceof BitwiseAndExpr
or
this instanceof BitwiseOrExpr
) and
this.getLeftOperand().getType() instanceof BoolType and
this.getRightOperand().getType() instanceof BoolType
}
pragma[nomagic]
private predicate hasRightOperandDescendant(Expr e) {
e = this.getRightOperand()
or
exists(Expr parent |
this.hasRightOperandDescendant(parent) and
e.getParent() = parent
)
}
/**
* Holds if this non-short-circuit expression contains a qualified member access,
* a method call, or an array access inside the right operand.
*/
predicate isDangerous() {
exists(Expr e | this.hasRightOperandDescendant(e) |
exists(Expr q | q = e.(MemberAccess).getQualifier() |
not q instanceof ThisAccess and
not q instanceof BaseAccess
)
or
e instanceof MethodCall
or
e instanceof ArrayAccess
) and
not exists(Expr e | this.hasRightOperandDescendant(e) |
e.(Call).getTarget().getAParameter().isOutOrRef()
)
}
}
from NonShortCircuit e
where e.isDangerous()
select e, "Potentially dangerous use of non-short circuit logic."