-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathConstantCondition.ql
More file actions
221 lines (189 loc) · 7.06 KB
/
ConstantCondition.ql
File metadata and controls
221 lines (189 loc) · 7.06 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
* @name Constant condition
* @description A condition that always evaluates to 'true' or always evaluates to 'false'
* should be removed, and if the condition is a loop condition, the condition
* is likely to cause an infinite loop.
* @kind problem
* @problem.severity warning
* @precision very-high
* @id cs/constant-condition
* @tags quality
* maintainability
* readability
* external/cwe/cwe-835
*/
import csharp
import semmle.code.csharp.commons.Assertions
import semmle.code.csharp.commons.Constants
import semmle.code.csharp.controlflow.BasicBlocks
import semmle.code.csharp.controlflow.Guards as Guards
import codeql.controlflow.queries.ConstantCondition as ConstCond
module ConstCondInput implements ConstCond::InputSig<ControlFlow::BasicBlock> {
class SsaDefinition = Ssa::Definition;
class GuardValue = Guards::GuardValue;
class Guard = Guards::Guards::Guard;
predicate ssaControlsBranchEdge(SsaDefinition def, BasicBlock bb1, BasicBlock bb2, GuardValue v) {
Guards::Guards::ssaControlsBranchEdge(def, bb1, bb2, v)
}
predicate ssaControls(SsaDefinition def, BasicBlock bb, GuardValue v) {
Guards::Guards::ssaControls(def, bb, v)
}
import Guards::Guards::InternalUtil
}
module ConstCondImpl = ConstCond::Make<Location, Cfg, ConstCondInput>;
predicate nullCheck(Expr e, boolean direct) {
exists(QualifiableExpr qe | qe.isConditional() and qe.getQualifier() = e and direct = true)
or
exists(NullCoalescingExpr nce | nce.getLeftOperand() = e and direct = true)
or
exists(ConditionalExpr ce | ce.getThen() = e or ce.getElse() = e |
nullCheck(ce, _) and direct = false
)
}
predicate constantGuard(
Guards::Guards::Guard g, string msg, Guards::Guards::Guard reason, string reasonMsg
) {
ConstCondImpl::problems(g, msg, reason, reasonMsg) and
// if a logical connective is constant, one of its operands is constant, so
// we report that instead
not g instanceof LogicalNotExpr and
not g instanceof LogicalAndExpr and
not g instanceof LogicalOrExpr and
// if a logical connective is a reason for another condition to be constant,
// then one of its operands is a more precise reason
not reason instanceof LogicalNotExpr and
not reason instanceof LogicalAndExpr and
not reason instanceof LogicalOrExpr and
// don't report double-checked locking
not exists(LockStmt ls, BasicBlock bb |
bb = ls.getBasicBlock() and
reason.getBasicBlock().strictlyDominates(bb) and
bb.dominates(g.getBasicBlock())
) and
// exclude indirect null checks like `x` in `(b ? x : null)?.Foo()`
not nullCheck(g, false)
}
/** A constant condition. */
abstract class ConstantCondition extends Guards::Guards::Guard {
/** Gets the alert message for this constant condition. */
abstract string getMessage();
predicate hasReason(Guards::Guards::Guard reason, string reasonMsg) {
// dummy value, overridden when message has a placeholder
reason = this and reasonMsg = "dummy"
}
/** Holds if this constant condition is white-listed. */
predicate isWhiteListed() { none() }
}
/** A constant guard. */
class ConstantGuard extends ConstantCondition {
ConstantGuard() { constantGuard(this, _, _, _) }
override string getMessage() { constantGuard(this, result, _, _) }
override predicate hasReason(Guards::Guards::Guard reason, string reasonMsg) {
constantGuard(this, _, reason, reasonMsg)
}
}
/** A constant Boolean condition. */
class ConstantBooleanCondition extends ConstantCondition {
boolean b;
ConstantBooleanCondition() { isConstantCondition(this, b) }
override string getMessage() { result = "Condition always evaluates to '" + b + "'." }
override predicate isWhiteListed() {
// E.g. `x ?? false`
this.(BoolLiteral) = any(NullCoalescingExpr nce).getRightOperand() or
// No need to flag logical operations when the operands are constant
isConstantCondition(this.(LogicalNotExpr).getOperand(), _) or
this =
any(LogicalAndExpr lae |
isConstantCondition(lae.getAnOperand(), false)
or
isConstantCondition(lae.getLeftOperand(), true) and
isConstantCondition(lae.getRightOperand(), true)
) or
this =
any(LogicalOrExpr loe |
isConstantCondition(loe.getAnOperand(), true)
or
isConstantCondition(loe.getLeftOperand(), false) and
isConstantCondition(loe.getRightOperand(), false)
)
}
}
/** A constant condition in an `if` statement or a conditional expression. */
class ConstantIfCondition extends ConstantBooleanCondition {
ConstantIfCondition() {
this = any(IfStmt is).getCondition().getAChildExpr*() or
this = any(ConditionalExpr ce).getCondition().getAChildExpr*()
}
override predicate isWhiteListed() {
ConstantBooleanCondition.super.isWhiteListed()
or
// It is a common pattern to use a local constant/constant field to control
// whether code parts must be executed or not
this instanceof AssignableRead and
not this instanceof ParameterRead
}
}
/** A constant loop condition. */
class ConstantLoopCondition extends ConstantBooleanCondition {
ConstantLoopCondition() { this = any(LoopStmt ls).getCondition() }
override predicate isWhiteListed() {
// Clearly intentional infinite loops are allowed
this.(BoolLiteral).getBoolValue() = true
}
}
/** A constant nullness condition. */
class ConstantNullnessCondition extends ConstantCondition {
boolean b;
ConstantNullnessCondition() {
forex(ControlFlow::Node cfn | cfn = this.getAControlFlowNode() |
exists(ControlFlow::NullnessSuccessor t, ControlFlow::Node s |
s = cfn.getASuccessorByType(t)
|
b = t.getValue() and
not s.isJoin()
) and
strictcount(ControlFlow::SuccessorType t | exists(cfn.getASuccessorByType(t))) = 1
)
}
override string getMessage() {
if b = true
then result = "Expression is always 'null'."
else result = "Expression is never 'null'."
}
}
/** A constant matching condition. */
class ConstantMatchingCondition extends ConstantCondition {
boolean b;
ConstantMatchingCondition() {
this instanceof Expr and
forex(ControlFlow::Node cfn | cfn = this.getAControlFlowNode() |
exists(ControlFlow::MatchingSuccessor t | exists(cfn.getASuccessorByType(t)) |
b = t.getValue()
) and
strictcount(ControlFlow::SuccessorType t | exists(cfn.getASuccessorByType(t))) = 1
)
}
override predicate isWhiteListed() {
exists(Switch se, Case c, int i |
c = se.getCase(i) and
c.getPattern() = this.(DiscardExpr)
|
i > 0
or
i = 0 and
exists(Expr cond | c.getCondition() = cond and not isConstantCondition(cond, true))
)
or
this = any(PositionalPatternExpr ppe).getPattern(_)
}
override string getMessage() {
if b = true then result = "Pattern always matches." else result = "Pattern never matches."
}
}
from ConstantCondition c, string msg, Guards::Guards::Guard reason, string reasonMsg
where
msg = c.getMessage() and
c.hasReason(reason, reasonMsg) and
not c.isWhiteListed() and
not isExprInAssertion(c)
select c, msg, reason, reasonMsg