Skip to content

Commit 64caae5

Browse files
committed
Guards: Refactor representation of false.
1 parent 402d58b commit 64caae5

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

shared/controlflow/codeql/controlflow/Guards.qll

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,14 @@ module Make<
202202

203203
private newtype TAbstractSingleValue =
204204
TValueNull() or
205-
TValueTrue() or
205+
TValueBool(Boolean b) or
206206
TValueInt(int i) { exists(ConstantExpr c | c.asIntegerValue() = i) or i = 0 } or
207207
TValueConstant(ConstantValue c) { exists(ConstantExpr ce | ce.asConstantValue() = c) }
208208

209209
private newtype TGuardValue =
210-
TValue(TAbstractSingleValue val, Boolean isVal) or
210+
TValue(TAbstractSingleValue val, Boolean isVal) {
211+
val instanceof TValueBool implies isVal = true
212+
} or
211213
TIntRange(int bound, Boolean upper) {
212214
exists(ConstantExpr c | c.asIntegerValue() + [-1, 0, 1] = bound) and
213215
// exclude edge cases to avoid overflow issues when computing duals
@@ -221,7 +223,9 @@ module Make<
221223
string toString() {
222224
result = "null" and this instanceof TValueNull
223225
or
224-
result = "true" and this instanceof TValueTrue
226+
result = "true" and this = TValueBool(true)
227+
or
228+
result = "false" and this = TValueBool(false)
225229
or
226230
exists(int i | result = i.toString() and this = TValueInt(i))
227231
or
@@ -244,6 +248,11 @@ module Make<
244248
result = TValue(val, isVal.booleanNot())
245249
)
246250
or
251+
exists(boolean b |
252+
this = TValue(TValueBool(b), true) and
253+
result = TValue(TValueBool(b.booleanNot()), true)
254+
)
255+
or
247256
exists(int bound, int d, boolean upper |
248257
upper = true and d = 1
249258
or
@@ -275,7 +284,7 @@ module Make<
275284
int asIntValue() { this = TValue(TValueInt(result), true) }
276285

277286
/** Gets the boolean that this value represents, if any. */
278-
boolean asBooleanValue() { this = TValue(TValueTrue(), result) }
287+
boolean asBooleanValue() { this = TValue(TValueBool(result), true) }
279288

280289
/** Gets the constant that this value represents, if any. */
281290
ConstantValue asConstantValue() { this = TValue(TValueConstant(result), true) }
@@ -293,9 +302,7 @@ module Make<
293302

294303
/** Gets a textual representation of this value. */
295304
string toString() {
296-
result = this.asBooleanValue().toString()
297-
or
298-
exists(AbstractSingleValue val | not val instanceof TValueTrue |
305+
exists(AbstractSingleValue val |
299306
this = TValue(val, true) and result = val.toString()
300307
or
301308
this = TValue(val, false) and result = "not " + val.toString()
@@ -365,15 +372,10 @@ module Make<
365372
}
366373

367374
private predicate branchEdge(BasicBlock bb1, BasicBlock bb2, GuardValue v) {
368-
exists(ConditionalSuccessor s |
369-
bb1.getASuccessor(s) = bb2 and
370-
exists(AbstractSingleValue val |
371-
s instanceof NullnessSuccessor and val = TValueNull()
372-
or
373-
s instanceof BooleanSuccessor and val = TValueTrue()
374-
|
375-
v = TValue(val, s.getValue())
376-
)
375+
exists(ConditionalSuccessor s | bb1.getASuccessor(s) = bb2 |
376+
s instanceof NullnessSuccessor and v = TValue(TValueNull(), s.getValue())
377+
or
378+
s instanceof BooleanSuccessor and v = TValue(TValueBool(s.getValue()), true)
377379
)
378380
or
379381
exceptionBranchPoint(bb1, bb2, _) and v = TException(false)

0 commit comments

Comments
 (0)