Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions csharp/ql/lib/change-notes/2026-11-19-guards-isequality.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
category: breaking
---
* Updated `Guard.isEquality` to use `GuardValue` instead of `boolean`. To adapt
to this api change either use `GuardValue.asBooleanValue()`, or preferably, if
the guard is used in conjunction with `Guard.controls`, switch to use
`Guard.valueControls`.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private module Impl {
exists(Expr e1_, Expr e2_ |
e1 = unique(ExprNode cfn | hasChild(this, e1_, _, cfn) | cfn) and
e2 = unique(ExprNode cfn | hasChild(this, e2_, _, cfn) | cfn) and
super.isEquality(e1_, e2_, polarity)
super.isEquality(e1_, e2_, any(G::GuardValue gv | gv.asBooleanValue() = polarity))
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import csharp
import semmle.code.csharp.controlflow.Guards

private predicate stringConstCompare(Guard guard, Expr testedNode, GuardValue value) {
guard.isEquality(any(StringLiteral lit), testedNode, value.asBooleanValue())
guard.isEquality(any(StringLiteral lit), testedNode, value)
}

class StringConstCompareBarrier extends DataFlow::Node {
Expand Down
2 changes: 1 addition & 1 deletion csharp/ql/test/query-tests/Nullness/EqualityCheck.ql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import csharp
import semmle.code.csharp.controlflow.Guards

from Guard guard, Expr e1, Expr e2, boolean eqval
from Guard guard, Expr e1, Expr e2, GuardValue eqval
where guard.isEquality(e1, e2, eqval)
select guard, eqval, e1, e2
6 changes: 3 additions & 3 deletions go/ql/lib/semmle/go/controlflow/Guards.qll
Original file line number Diff line number Diff line change
Expand Up @@ -545,14 +545,14 @@ predicate guardEnsures(Expr e, boolean b, BasicBlock bb) { e.(Guard).controls(bb

/** Holds if `guard` evaluating to `branch` ensures that `i = j` holds. */
predicate guardEnsuresEq(Guard guard, boolean branch, DataFlow::Node i, DataFlow::Node j) {
guard.isEquality(i.asExpr(), j.asExpr(), branch)
guard.isEquality(i.asExpr(), j.asExpr(), any(GuardValue gv | gv.asBooleanValue() = branch))
}

/** Holds if `guard` evaluating to `branch` ensures that `i != j` holds. */
predicate guardEnsuresNeq(Guard guard, boolean branch, DataFlow::Node i, DataFlow::Node j) {
exists(boolean eqval |
exists(GuardValue eqval |
guard.isEquality(i.asExpr(), j.asExpr(), eqval) and
branch = eqval.booleanNot()
branch = eqval.getDualValue().asBooleanValue()
)
}

Expand Down
7 changes: 7 additions & 0 deletions java/ql/lib/change-notes/2026-11-19-guards-isequality.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
category: breaking
---
* Updated `Guard.isEquality` to use `GuardValue` instead of `boolean`. To adapt
to this api change either use `GuardValue.asBooleanValue()`, or preferably, if
the guard is used in conjunction with `Guard.controls`, switch to use
`Guard.valueControls`.
4 changes: 4 additions & 0 deletions java/ql/lib/semmle/code/java/dataflow/RangeAnalysis.qll
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ module Sem implements Semantic<Location> {

class Guard extends G::Guards_v2::Guard {
Expr asExpr() { result = this }

predicate isEquality(Expr e1, Expr e2, boolean eqval) {
super.isEquality(e1, e2, any(G::GuardValue gv | gv.asBooleanValue() = eqval))
}
}

class Type = J::Type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ module Private {

class Expr = J::Expr;

class Guard = G::Guards_v2::Guard;
class Guard extends G::Guards_v2::Guard {
predicate isEquality(Expr e1, Expr e2, boolean eqval) {
super.isEquality(e1, e2, any(G::GuardValue gv | gv.asBooleanValue() = eqval))
}
}

class ConstantIntegerExpr = RU::ConstantIntegerExpr;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ module Private {

class ConstantIntegerExpr = RU::ConstantIntegerExpr;

class Guard = G::Guards_v2::Guard;
class Guard extends G::Guards_v2::Guard {
predicate isEquality(Expr e1, Expr e2, boolean eqval) {
super.isEquality(e1, e2, any(G::GuardValue gv | gv.asBooleanValue() = eqval))
}
}

class SsaVariable = Ssa::SsaDefinition;

Expand Down
2 changes: 1 addition & 1 deletion java/ql/lib/semmle/code/java/os/OSCheck.qll
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private Guard isOsFromSystemPropertyEqualityCheck(
any(Literal literal |
(literal instanceof CharacterLiteral or literal instanceof StringLiteral) and
literal.getValue() = compareToLiteral
), branch)
), any(GuardValue gv | gv.asBooleanValue() = branch))
}

private class IsWindowsFromPathSeparator extends IsWindowsGuard {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,19 @@ private class ExternalIntentRedirectionSanitizer extends IntentRedirectionSaniti
private class DefaultIntentRedirectionSanitizer extends IntentRedirectionSanitizer {
DefaultIntentRedirectionSanitizer() {
this.getType() instanceof TypeIntent and
exists(MethodCall ma, Method m, Guard g, boolean branch |
exists(MethodCall ma, Method m, Guard g, GuardValue branch |
ma.getMethod() = m and
m.getDeclaringType() instanceof TypeComponentName and
m.hasName("getPackageName") and
g.isEquality(ma, _, branch) and
g.controls(this.asExpr().getBasicBlock(), branch)
g.valueControls(this.asExpr().getBasicBlock(), branch)
) and
exists(MethodCall ma, Method m, Guard g, boolean branch |
exists(MethodCall ma, Method m, Guard g, GuardValue branch |
ma.getMethod() = m and
m.getDeclaringType() instanceof TypeComponentName and
m.hasName("getClassName") and
g.isEquality(ma, _, branch) and
g.controls(this.asExpr().getBasicBlock(), branch)
g.valueControls(this.asExpr().getBasicBlock(), branch)
)
}
}
19 changes: 10 additions & 9 deletions java/ql/lib/semmle/code/java/security/ArithmeticCommon.qll
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,25 @@ private Guard sizeGuard(Expr e, boolean branch, boolean upper) {
branch = true
)
)
or
result.isEquality(e, _, branch) and
(upper = true or upper = false)
}

private predicate sizeGuardLessThan(Guard g, Expr e, boolean branch) {
g = sizeGuard(e, branch, true)
private predicate sizeGuardLessThan(Guard g, Expr e, GuardValue gv) {
g = sizeGuard(e, gv.asBooleanValue(), true)
or
g.isEquality(e, _, gv)
}

private predicate sizeGuardGreaterThan(Guard g, Expr e, boolean branch) {
g = sizeGuard(e, branch, false)
private predicate sizeGuardGreaterThan(Guard g, Expr e, GuardValue gv) {
g = sizeGuard(e, gv.asBooleanValue(), false)
or
g.isEquality(e, _, gv)
}

/**
* Holds if `n` is bounded in a way that is likely to prevent overflow.
*/
predicate guardedLessThanSomething(DataFlow::Node n) {
DataFlow::BarrierGuard<sizeGuardLessThan/3>::getABarrierNode() = n
DataFlow::BarrierGuardValue<sizeGuardLessThan/3>::getABarrierNode() = n
or
negative(n.asExpr())
or
Expand All @@ -92,7 +93,7 @@ predicate guardedLessThanSomething(DataFlow::Node n) {
* Holds if `e` is bounded in a way that is likely to prevent underflow.
*/
predicate guardedGreaterThanSomething(DataFlow::Node n) {
DataFlow::BarrierGuard<sizeGuardGreaterThan/3>::getABarrierNode() = n
DataFlow::BarrierGuardValue<sizeGuardGreaterThan/3>::getABarrierNode() = n
or
positive(n.asExpr())
or
Expand Down
15 changes: 9 additions & 6 deletions java/ql/src/Likely Bugs/Comparison/UselessComparisonTest.ql
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ predicate concurrentModificationTest(BinaryExpr test) {
* Holds if `test` and `guard` are equality tests of the same integral variable v with constants `c1` and `c2`.
*/
pragma[nomagic]
predicate guardedTest(EqualityTest test, Guard guard, boolean isEq, int i1, int i2) {
predicate guardedTest(EqualityTest test, Guard guard, GuardValue eqval, int i1, int i2) {
exists(SsaDefinition v, CompileTimeConstantExpr c1, CompileTimeConstantExpr c2 |
guard.isEquality(v.getARead(), c1, isEq) and
guard.isEquality(v.getARead(), c1, eqval) and
test.hasOperands(v.getARead(), c2) and
i1 = c1.getIntValue() and
i2 = c2.getIntValue() and
Expand All @@ -201,10 +201,13 @@ predicate guardedTest(EqualityTest test, Guard guard, boolean isEq, int i1, int
* Holds if `guard` implies that `test` always has the value `testIsTrue`.
*/
predicate uselessEqTest(EqualityTest test, boolean testIsTrue, Guard guard) {
exists(boolean guardIsTrue, boolean guardpolarity, int i |
guardedTest(test, guard, guardpolarity, i, i) and
guard.controls(test.getBasicBlock(), guardIsTrue) and
testIsTrue = guardIsTrue.booleanXor(guardpolarity.booleanXor(test.polarity()))
exists(GuardValue branch, GuardValue eqval, int i |
guardedTest(test, guard, eqval, i, i) and
guard.valueControls(test.getBasicBlock(), branch)
|
branch = eqval and testIsTrue = test.polarity()
or
branch = eqval.getDualValue() and testIsTrue = test.polarity().booleanNot()
)
}

Expand Down
8 changes: 4 additions & 4 deletions java/ql/test/library-tests/guards12/guard.ql
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ query predicate hasBranchEdge(
)
}

from Guard g, BasicBlock bb, boolean branch, Expr e1, Expr e2, boolean pol
from Guard g, BasicBlock bb, GuardValue branch, Expr e1, Expr e2, GuardValue eqval
where
g.controls(bb, branch) and
g.isEquality(e1, e2, pol) and
g.valueControls(bb, branch) and
g.isEquality(e1, e2, eqval) and
not e1 instanceof Literal
select g, e1, e2, pol, branch, getFirstAstNodeOrSynth(bb)
select g, e1, e2, eqval, branch, getFirstAstNodeOrSynth(bb)
10 changes: 5 additions & 5 deletions shared/controlflow/codeql/controlflow/Guards.qll
Original file line number Diff line number Diff line change
Expand Up @@ -507,12 +507,12 @@ module Make<
* Holds if this guard tests equality between `e1` and `e2` upon evaluating
* to `eqval`.
*/
predicate isEquality(Expr e1, Expr e2, boolean eqval) {
equalityTestSymmetric(this, e1, e2, eqval)
predicate isEquality(Expr e1, Expr e2, GuardValue eqval) {
equalityTestSymmetric(this, e1, e2, eqval.asBooleanValue())
or
constcaseEquality(this, e1, e2) and eqval = true
constcaseEquality(this, e1, e2) and eqval.asBooleanValue() = true
or
constcaseEquality(this, e2, e1) and eqval = true
constcaseEquality(this, e2, e1) and eqval.asBooleanValue() = true
}

/**
Expand Down Expand Up @@ -541,7 +541,7 @@ module Make<
g1.(NotExpr).getOperand() = g2 and v1.asBooleanValue().booleanNot() = v2.asBooleanValue()
or
exists(GuardValue eqval, ConstantExpr constant, GuardValue cv |
g1.isEquality(g2, constant, eqval.asBooleanValue()) and
g1.isEquality(g2, constant, eqval) and
constantHasValue(constant, cv)
|
v1 = eqval and v2 = cv
Expand Down
Loading