diff --git a/csharp/ql/lib/change-notes/2026-11-19-guards-isequality.md b/csharp/ql/lib/change-notes/2026-11-19-guards-isequality.md new file mode 100644 index 000000000000..9970ba804424 --- /dev/null +++ b/csharp/ql/lib/change-notes/2026-11-19-guards-isequality.md @@ -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`. diff --git a/csharp/ql/lib/semmle/code/csharp/dataflow/internal/rangeanalysis/RangeUtils.qll b/csharp/ql/lib/semmle/code/csharp/dataflow/internal/rangeanalysis/RangeUtils.qll index b85f68883ab6..d1da165d8337 100644 --- a/csharp/ql/lib/semmle/code/csharp/dataflow/internal/rangeanalysis/RangeUtils.qll +++ b/csharp/ql/lib/semmle/code/csharp/dataflow/internal/rangeanalysis/RangeUtils.qll @@ -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)) ) } } diff --git a/csharp/ql/test/library-tests/dataflow/barrier-guards/barrier-flow.ql b/csharp/ql/test/library-tests/dataflow/barrier-guards/barrier-flow.ql index 08579f1528f2..2f73d2175d62 100644 --- a/csharp/ql/test/library-tests/dataflow/barrier-guards/barrier-flow.ql +++ b/csharp/ql/test/library-tests/dataflow/barrier-guards/barrier-flow.ql @@ -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 { diff --git a/csharp/ql/test/query-tests/Nullness/EqualityCheck.ql b/csharp/ql/test/query-tests/Nullness/EqualityCheck.ql index 1140d4e9a9dd..54e364cdb335 100644 --- a/csharp/ql/test/query-tests/Nullness/EqualityCheck.ql +++ b/csharp/ql/test/query-tests/Nullness/EqualityCheck.ql @@ -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 diff --git a/go/ql/lib/semmle/go/controlflow/Guards.qll b/go/ql/lib/semmle/go/controlflow/Guards.qll index 7f84f525ce36..3577f81ebe8b 100644 --- a/go/ql/lib/semmle/go/controlflow/Guards.qll +++ b/go/ql/lib/semmle/go/controlflow/Guards.qll @@ -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() ) } diff --git a/java/ql/lib/change-notes/2026-11-19-guards-isequality.md b/java/ql/lib/change-notes/2026-11-19-guards-isequality.md new file mode 100644 index 000000000000..9970ba804424 --- /dev/null +++ b/java/ql/lib/change-notes/2026-11-19-guards-isequality.md @@ -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`. diff --git a/java/ql/lib/semmle/code/java/dataflow/RangeAnalysis.qll b/java/ql/lib/semmle/code/java/dataflow/RangeAnalysis.qll index 75aa3bdbf31b..eeb17fecc1a8 100644 --- a/java/ql/lib/semmle/code/java/dataflow/RangeAnalysis.qll +++ b/java/ql/lib/semmle/code/java/dataflow/RangeAnalysis.qll @@ -208,6 +208,10 @@ module Sem implements Semantic { 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; diff --git a/java/ql/lib/semmle/code/java/dataflow/internal/rangeanalysis/ModulusAnalysisSpecific.qll b/java/ql/lib/semmle/code/java/dataflow/internal/rangeanalysis/ModulusAnalysisSpecific.qll index e0968c9cf175..8bba028d26cf 100644 --- a/java/ql/lib/semmle/code/java/dataflow/internal/rangeanalysis/ModulusAnalysisSpecific.qll +++ b/java/ql/lib/semmle/code/java/dataflow/internal/rangeanalysis/ModulusAnalysisSpecific.qll @@ -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; diff --git a/java/ql/lib/semmle/code/java/dataflow/internal/rangeanalysis/SignAnalysisSpecific.qll b/java/ql/lib/semmle/code/java/dataflow/internal/rangeanalysis/SignAnalysisSpecific.qll index a10cca653ce2..2783924b29dc 100644 --- a/java/ql/lib/semmle/code/java/dataflow/internal/rangeanalysis/SignAnalysisSpecific.qll +++ b/java/ql/lib/semmle/code/java/dataflow/internal/rangeanalysis/SignAnalysisSpecific.qll @@ -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; diff --git a/java/ql/lib/semmle/code/java/os/OSCheck.qll b/java/ql/lib/semmle/code/java/os/OSCheck.qll index 97ad27c83dfb..1b4f311c9f83 100644 --- a/java/ql/lib/semmle/code/java/os/OSCheck.qll +++ b/java/ql/lib/semmle/code/java/os/OSCheck.qll @@ -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 { diff --git a/java/ql/lib/semmle/code/java/security/AndroidIntentRedirection.qll b/java/ql/lib/semmle/code/java/security/AndroidIntentRedirection.qll index 6b8ea861049e..a380a8486d41 100644 --- a/java/ql/lib/semmle/code/java/security/AndroidIntentRedirection.qll +++ b/java/ql/lib/semmle/code/java/security/AndroidIntentRedirection.qll @@ -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) ) } } diff --git a/java/ql/lib/semmle/code/java/security/ArithmeticCommon.qll b/java/ql/lib/semmle/code/java/security/ArithmeticCommon.qll index 42b7ec7b02d8..d2141cca508e 100644 --- a/java/ql/lib/semmle/code/java/security/ArithmeticCommon.qll +++ b/java/ql/lib/semmle/code/java/security/ArithmeticCommon.qll @@ -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::getABarrierNode() = n + DataFlow::BarrierGuardValue::getABarrierNode() = n or negative(n.asExpr()) or @@ -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::getABarrierNode() = n + DataFlow::BarrierGuardValue::getABarrierNode() = n or positive(n.asExpr()) or diff --git a/java/ql/src/Likely Bugs/Comparison/UselessComparisonTest.ql b/java/ql/src/Likely Bugs/Comparison/UselessComparisonTest.ql index 88a956bb24c3..b97791863388 100644 --- a/java/ql/src/Likely Bugs/Comparison/UselessComparisonTest.ql +++ b/java/ql/src/Likely Bugs/Comparison/UselessComparisonTest.ql @@ -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 @@ -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() ) } diff --git a/java/ql/test/library-tests/guards12/guard.ql b/java/ql/test/library-tests/guards12/guard.ql index 39f9fdbe9394..d4d134d81863 100644 --- a/java/ql/test/library-tests/guards12/guard.ql +++ b/java/ql/test/library-tests/guards12/guard.ql @@ -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) diff --git a/shared/controlflow/codeql/controlflow/Guards.qll b/shared/controlflow/codeql/controlflow/Guards.qll index d2852a4c227b..7550363f0ea2 100644 --- a/shared/controlflow/codeql/controlflow/Guards.qll +++ b/shared/controlflow/codeql/controlflow/Guards.qll @@ -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 } /** @@ -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