From 1f4dcff642b5e7e970ea93ddbe7b6c8bd293f4c7 Mon Sep 17 00:00:00 2001 From: Owen Mansel-Chan Date: Wed, 2 Dec 2026 16:27:34 +0000 Subject: [PATCH 1/2] Go: Preserve switch relationships in guard helpers Add match-aware guard value helpers while retaining the Boolean APIs. Migrate length-comparison and experimental range-analysis callers to valueControls, and cover switch matches, non-matches, multi-case clauses, and fallthrough. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../2026-12-02-match-aware-guard-helpers.md | 4 ++ go/ql/lib/semmle/go/controlflow/Guards.qll | 40 ++++++++++++--- .../ConstantLengthComparison.ql | 8 +-- .../LengthComparisonOffByOne.ql | 28 +++++------ .../2026-12-02-switch-length-comparisons.md | 4 ++ .../IntegerOverflow/RangeAnalysis.qll | 24 ++++----- .../go/controlflow/Guards/Guards.expected | 14 ++++++ .../semmle/go/controlflow/Guards/Guards.ql | 22 +++++++++ .../ConstantLengthComparison.expected | 2 + .../ConstantLengthComparison/tst.go | 24 +++++++++ .../LengthComparisonOffByOne.expected | 2 + .../LengthComparisonOffByOne/main.go | 49 +++++++++++++++++++ 12 files changed, 185 insertions(+), 36 deletions(-) create mode 100644 go/ql/lib/change-notes/2026-12-02-match-aware-guard-helpers.md create mode 100644 go/ql/src/change-notes/2026-12-02-switch-length-comparisons.md diff --git a/go/ql/lib/change-notes/2026-12-02-match-aware-guard-helpers.md b/go/ql/lib/change-notes/2026-12-02-match-aware-guard-helpers.md new file mode 100644 index 000000000000..1ced3fa4d93b --- /dev/null +++ b/go/ql/lib/change-notes/2026-12-02-match-aware-guard-helpers.md @@ -0,0 +1,4 @@ +--- +category: feature +--- +* Added `guardValueEnsuresEq`, `guardValueEnsuresNeq`, and `guardValueEnsuresLeq` to support switch match outcomes as well as Boolean guard values. The existing Boolean helpers remain available. diff --git a/go/ql/lib/semmle/go/controlflow/Guards.qll b/go/ql/lib/semmle/go/controlflow/Guards.qll index 7396cdfdd6d4..c599403d2192 100644 --- a/go/ql/lib/semmle/go/controlflow/Guards.qll +++ b/go/ql/lib/semmle/go/controlflow/Guards.qll @@ -480,30 +480,58 @@ overlay[caller?] pragma[inline] predicate guardEnsures(Expr e, boolean b, BasicBlock bb) { e.(Guard).controls(bb, b) } -/** Holds if `guard` evaluating to `branch` ensures that `i = j` holds. */ +/** + * Holds if `guard` evaluating to `branch` ensures that `i = j` holds. + * Use `guardValueEnsuresEq` to also support switch match outcomes. + */ predicate guardEnsuresEq(Guard guard, boolean branch, DataFlow::Node i, DataFlow::Node j) { - guard.isEquality(i.asExpr(), j.asExpr(), any(GuardValue gv | gv.asBooleanValue() = branch)) + guardValueEnsuresEq(guard, any(GuardValue value | value.asBooleanValue() = branch), i, j) +} + +/** Holds if `guard` evaluating to `value` ensures that `i = j` holds. */ +predicate guardValueEnsuresEq(Guard guard, GuardValue value, DataFlow::Node i, DataFlow::Node j) { + guard.isEquality(i.asExpr(), j.asExpr(), value) } -/** Holds if `guard` evaluating to `branch` ensures that `i != j` holds. */ +/** + * Holds if `guard` evaluating to `branch` ensures that `i != j` holds. + * Use `guardValueEnsuresNeq` to also support switch match outcomes. + */ predicate guardEnsuresNeq(Guard guard, boolean branch, DataFlow::Node i, DataFlow::Node j) { + guardValueEnsuresNeq(guard, any(GuardValue value | value.asBooleanValue() = branch), i, j) +} + +/** Holds if `guard` evaluating to `value` ensures that `i != j` holds. */ +predicate guardValueEnsuresNeq(Guard guard, GuardValue value, DataFlow::Node i, DataFlow::Node j) { exists(GuardValue eqval | guard.isEquality(i.asExpr(), j.asExpr(), eqval) and - branch = eqval.getDualValue().asBooleanValue() + value = eqval.getDualValue() ) } /** * Holds if `guard` evaluating to `branch` ensures that `lesser <= greater + bias` * holds. + * Use `guardValueEnsuresLeq` to also support switch match outcomes. */ predicate guardEnsuresLeq( Guard guard, boolean branch, DataFlow::Node lesser, DataFlow::Node greater, int bias +) { + guardValueEnsuresLeq(guard, any(GuardValue value | value.asBooleanValue() = branch), lesser, + greater, bias) +} + +/** + * Holds if `guard` evaluating to `value` ensures that `lesser <= greater + bias` + * holds. + */ +predicate guardValueEnsuresLeq( + Guard guard, GuardValue value, DataFlow::Node lesser, DataFlow::Node greater, int bias ) { exists(DataFlow::RelationalComparisonNode rel | guard = rel.asExpr() and - rel.leq(branch, lesser, greater, bias) + rel.leq(value.asBooleanValue(), lesser, greater, bias) ) or - guardEnsuresEq(guard, branch, lesser, greater) and bias = 0 + guardValueEnsuresEq(guard, value, lesser, greater) and bias = 0 } diff --git a/go/ql/src/InconsistentCode/ConstantLengthComparison.ql b/go/ql/src/InconsistentCode/ConstantLengthComparison.ql index 6b722d887ae6..0e506388bcf0 100644 --- a/go/ql/src/InconsistentCode/ConstantLengthComparison.ql +++ b/go/ql/src/InconsistentCode/ConstantLengthComparison.ql @@ -16,7 +16,7 @@ import go private import semmle.go.controlflow.Guards from - ForStmt fs, Variable i, DataFlow::ElementReadNode idx, GVN a, Guard cond, boolean branch, + ForStmt fs, Variable i, DataFlow::ElementReadNode idx, GVN a, Guard cond, GuardValue value, DataFlow::CallNode lenA where // `i` is incremented in `fs` @@ -28,10 +28,10 @@ where lenA.getArgument(0) = a.getANode() and // and is checked against a constant exists(DataFlow::Node const | exists(const.getIntValue()) | - guardEnsuresNeq(cond, branch, lenA, const) or - guardEnsuresLeq(cond, branch, const, lenA, _) + guardValueEnsuresNeq(cond, value, lenA, const) or + guardValueEnsuresLeq(cond, value, const, lenA, _) ) and - cond.controls(idx.getBasicBlock(), branch) and + cond.valueControls(idx.getBasicBlock(), value) and // and that check happens inside the loop body cond.(Expr).getParent+() = fs select cond, "This checks the length against a constant, but it $@.", idx, diff --git a/go/ql/src/InconsistentCode/LengthComparisonOffByOne.ql b/go/ql/src/InconsistentCode/LengthComparisonOffByOne.ql index 632c1afe143b..73aac49a571f 100644 --- a/go/ql/src/InconsistentCode/LengthComparisonOffByOne.ql +++ b/go/ql/src/InconsistentCode/LengthComparisonOffByOne.ql @@ -42,29 +42,29 @@ DataFlow::CallNode arrayLen(DataFlow::SsaNode array) { } /** - * Holds if `guard` evaluating to `branch` checks that `index` is less than or equal to + * Holds if `guard` evaluating to `value` checks that `index` is less than or equal to * `array.length`. */ -predicate lengthLeGuard(Guard guard, boolean branch, Index index, DataFlow::SsaNode array) { - guardEnsuresLeq(guard, branch, getAUse(index), arrayLen(array), 0) +predicate lengthLeGuard(Guard guard, GuardValue value, Index index, DataFlow::SsaNode array) { + guardValueEnsuresLeq(guard, value, getAUse(index), arrayLen(array), 0) or exists(int i, int bias | index = ConstantIndex(i) | - guardEnsuresLeq(guard, branch, getAUse(ConstantIndex(i + bias)), arrayLen(array), bias) + guardValueEnsuresLeq(guard, value, getAUse(ConstantIndex(i + bias)), arrayLen(array), bias) ) } predicate isControllingLengthLeGuard( - Guard guard, boolean branch, Index index, DataFlow::SsaNode array, BasicBlock bb + Guard guard, GuardValue value, Index index, DataFlow::SsaNode array, BasicBlock bb ) { - lengthLeGuard(guard, branch, index, array) and - guard.controls(bb, branch) + lengthLeGuard(guard, value, index, array) and + guard.valueControls(bb, value) } /** - * Holds if `guard` evaluating to `branch` checks that `index` is not equal to `array.length`. + * Holds if `guard` evaluating to `value` checks that `index` is not equal to `array.length`. */ -predicate lengthNeGuard(Guard guard, boolean branch, Index index, DataFlow::SsaNode array) { - guardEnsuresNeq(guard, branch, getAUse(index), arrayLen(array)) +predicate lengthNeGuard(Guard guard, GuardValue value, Index index, DataFlow::SsaNode array) { + guardValueEnsuresNeq(guard, value, getAUse(index), arrayLen(array)) } /** @@ -87,21 +87,21 @@ predicate isRegexpMethodCall(DataFlow::MethodCallNode c) { } from - Guard cond, boolean branch, DataFlow::SsaNode array, Index index, DataFlow::ElementReadNode ea, + Guard cond, GuardValue value, DataFlow::SsaNode array, Index index, DataFlow::ElementReadNode ea, BasicBlock bb where // there is a read from `array[index]` elementRead(ea, array, index, bb) and // and it is guarded by a comparison `index <= len(array)` - isControllingLengthLeGuard(cond, branch, index, array, bb) and + isControllingLengthLeGuard(cond, value, index, array, bb) and // and report the innermost guard that establishes the comparison not exists(Guard innerCond | isControllingLengthLeGuard(innerCond, _, index, array, bb) and innerCond.(Expr).getParent+() = cond.(Expr) ) and // but the read is not guarded by another check that `index != len(array)` - not exists(Guard ne, boolean neBranch | - lengthNeGuard(ne, neBranch, index, array) and ne.controls(bb, neBranch) + not exists(Guard ne, GuardValue neValue | + lengthNeGuard(ne, neValue, index, array) and ne.valueControls(bb, neValue) ) and // and it is not additionally guarded by a stronger index check not exists(Index index2, int i, int i2 | diff --git a/go/ql/src/change-notes/2026-12-02-switch-length-comparisons.md b/go/ql/src/change-notes/2026-12-02-switch-length-comparisons.md new file mode 100644 index 000000000000..b067ac99d5e7 --- /dev/null +++ b/go/ql/src/change-notes/2026-12-02-switch-length-comparisons.md @@ -0,0 +1,4 @@ +--- +category: minorAnalysis +--- +* The queries `go/index-out-of-bounds` and `go/constant-length-comparison` now recognize switch-derived equality and inequality checks against lengths. diff --git a/go/ql/src/experimental/IntegerOverflow/RangeAnalysis.qll b/go/ql/src/experimental/IntegerOverflow/RangeAnalysis.qll index e6dfd8a7e928..484c87a983b6 100644 --- a/go/ql/src/experimental/IntegerOverflow/RangeAnalysis.qll +++ b/go/ql/src/experimental/IntegerOverflow/RangeAnalysis.qll @@ -39,12 +39,12 @@ float getAnUpperBound(Expr expr) { if //if a condition expression exists before and one of the operand happens to be the identifier, we use this condition expression to narrow down the range. exists( - Guard n, boolean branch, DataFlow::Node lesser, DataFlow::Node greater, + Guard n, GuardValue value, DataFlow::Node lesser, DataFlow::Node greater, ReachableBasicBlock bb | - guardEnsuresLeq(n, branch, lesser, greater, _) and + guardValueEnsuresLeq(n, value, lesser, greater, _) and lesser.asExpr() = getAUse(v) and - n.controls(bb, branch) and + n.valueControls(bb, value) and bb.getANode() = IR::evalExprInstruction(identifier) and not exists(Expr e | e = getAUse(v) and @@ -53,12 +53,12 @@ float getAnUpperBound(Expr expr) { ) then exists( - Guard n, boolean branch, ReachableBasicBlock bb, DataFlow::Node lesser, + Guard n, GuardValue value, ReachableBasicBlock bb, DataFlow::Node lesser, DataFlow::Node greater, int bias | - n.controls(bb, branch) and + n.valueControls(bb, value) and bb.getANode() = IR::evalExprInstruction(identifier) and - guardEnsuresLeq(n, branch, lesser, greater, bias) and + guardValueEnsuresLeq(n, value, lesser, greater, bias) and getAUse(v) = lesser.asExpr() and not exists(Expr e | e = getAUse(v) and @@ -190,12 +190,12 @@ float getALowerBound(Expr expr) { //if exists a condition expression before this identifier if exists( - Guard n, boolean branch, DataFlow::Node greater, DataFlow::Node lesser, + Guard n, GuardValue value, DataFlow::Node greater, DataFlow::Node lesser, ReachableBasicBlock bb | - guardEnsuresLeq(n, branch, lesser, greater, _) and + guardValueEnsuresLeq(n, value, lesser, greater, _) and greater.asExpr() = getAUse(v) and - n.controls(bb, branch) and + n.valueControls(bb, value) and bb.getANode() = IR::evalExprInstruction(identifier) and not exists(Expr e | e = getAUse(v) and @@ -204,12 +204,12 @@ float getALowerBound(Expr expr) { ) then exists( - Guard n, boolean branch, ReachableBasicBlock bb, DataFlow::Node lesser, + Guard n, GuardValue value, ReachableBasicBlock bb, DataFlow::Node lesser, DataFlow::Node greater, int bias, float lbs | - n.controls(bb, branch) and + n.valueControls(bb, value) and bb.getANode() = IR::evalExprInstruction(identifier) and - guardEnsuresLeq(n, branch, lesser, greater, bias) and + guardValueEnsuresLeq(n, value, lesser, greater, bias) and getAUse(v) = greater.asExpr() and not exists(Expr e | e = getAUse(v) and diff --git a/go/ql/test/library-tests/semmle/go/controlflow/Guards/Guards.expected b/go/ql/test/library-tests/semmle/go/controlflow/Guards/Guards.expected index 12ac603beab2..124cb413999b 100644 --- a/go/ql/test/library-tests/semmle/go/controlflow/Guards/Guards.expected +++ b/go/ql/test/library-tests/semmle/go/controlflow/Guards/Guards.expected @@ -242,6 +242,20 @@ ensuresEqResult | guards.go:383:5:383:34 | ...==... | nil = call to modelNullGuard | true | | guards.go:386:5:386:37 | ...!=... | call to modelNotNullGuard = nil | false | | guards.go:386:5:386:37 | ...!=... | nil = call to modelNotNullGuard | false | +ensuresValueResult +| guards.go:23:7:23:7 | 0 | tagged one or two | 0 != value | no match | +| guards.go:23:7:23:7 | 0 | tagged one or two | value != 0 | no match | +| guards.go:23:7:23:7 | 0 | tagged other | 0 != value | no match | +| guards.go:23:7:23:7 | 0 | tagged other | value != 0 | no match | +| guards.go:23:7:23:7 | 0 | tagged zero | 0 <= value | match | +| guards.go:23:7:23:7 | 0 | tagged zero | 0 = value | match | +| guards.go:23:7:23:7 | 0 | tagged zero | value <= 0 | match | +| guards.go:23:7:23:7 | 0 | tagged zero | value = 0 | match | +| guards.go:25:7:25:7 | 1 | tagged other | 1 != value | no match | +| guards.go:25:7:25:7 | 1 | tagged other | value != 1 | no match | +| guards.go:25:10:25:10 | 2 | tagged other | 2 != value | no match | +| guards.go:25:10:25:10 | 2 | tagged other | value != 2 | no match | +| guards.go:68:5:68:15 | ...<=... | at most ten | value <= 10 | true | modelBarrierResult | guards.go:375:27:375:33 | pointer | model bool | model-bool | | guards.go:378:27:378:33 | pointer | model zero | model-zero | diff --git a/go/ql/test/library-tests/semmle/go/controlflow/Guards/Guards.ql b/go/ql/test/library-tests/semmle/go/controlflow/Guards/Guards.ql index 38a8b5c6f9ca..68db56483224 100644 --- a/go/ql/test/library-tests/semmle/go/controlflow/Guards/Guards.ql +++ b/go/ql/test/library-tests/semmle/go/controlflow/Guards/Guards.ql @@ -31,6 +31,28 @@ query predicate ensuresEqResult(Guard guard, string label, string outcome) { ) } +query predicate ensuresValueResult(Guard guard, string label, string relationship, string outcome) { + exists(DataFlow::CallNode call, GuardValue value, DataFlow::Node left, DataFlow::Node right | + sinkCall(call, label) and + label in [ + "tagged zero", "tagged one or two", "tagged other", "fallthrough default", "at most ten", + "above ten" + ] and + guard.valueControls(call.getBasicBlock(), value) and + ( + guardValueEnsuresEq(guard, value, left, right) and + relationship = left.toString() + " = " + right.toString() + or + guardValueEnsuresNeq(guard, value, left, right) and + relationship = left.toString() + " != " + right.toString() + or + guardValueEnsuresLeq(guard, value, left, right, 0) and + relationship = left.toString() + " <= " + right.toString() + ) and + outcome = value.toString() + ) +} + query predicate modelBarrierResult(DataFlow::Node node, string label, string kind) { exists(DataFlow::CallNode call | call.getTarget().getName() = "valueSink" and diff --git a/go/ql/test/query-tests/InconsistentCode/ConstantLengthComparison/ConstantLengthComparison.expected b/go/ql/test/query-tests/InconsistentCode/ConstantLengthComparison/ConstantLengthComparison.expected index b3e042e30cee..d7b083579994 100644 --- a/go/ql/test/query-tests/InconsistentCode/ConstantLengthComparison/ConstantLengthComparison.expected +++ b/go/ql/test/query-tests/InconsistentCode/ConstantLengthComparison/ConstantLengthComparison.expected @@ -1 +1,3 @@ | ConstantLengthComparison.go:5:6:5:17 | ...==... | This checks the length against a constant, but it $@. | ConstantLengthComparison.go:5:31:5:35 | index expression | is indexed using a variable | +| tst.go:19:8:19:8 | 1 | This checks the length against a constant, but it $@. | tst.go:20:20:20:24 | index expression | is indexed using a variable | +| tst.go:29:8:29:8 | 0 | This checks the length against a constant, but it $@. | tst.go:32:16:32:20 | index expression | is indexed using a variable | diff --git a/go/ql/test/query-tests/InconsistentCode/ConstantLengthComparison/tst.go b/go/ql/test/query-tests/InconsistentCode/ConstantLengthComparison/tst.go index c891795d558b..bc676f6dd6db 100644 --- a/go/ql/test/query-tests/InconsistentCode/ConstantLengthComparison/tst.go +++ b/go/ql/test/query-tests/InconsistentCode/ConstantLengthComparison/tst.go @@ -12,3 +12,27 @@ func isPrefixOfGood2(xs, ys []int) bool { } return true } + +func switchConstantLengthMatch(xs, ys []int) bool { + for i := 0; i < len(xs); i++ { + switch len(ys) { + case 1: // $ Alert + return xs[i] == ys[i] + } + } + return false +} + +func switchConstantLengthNonMatch(xs, ys []int) bool { + for i := 0; i < len(xs); i++ { + switch len(ys) { + case 0: // $ Alert + return false + default: + if xs[i] != ys[i] { + return false + } + } + } + return true +} diff --git a/go/ql/test/query-tests/InconsistentCode/LengthComparisonOffByOne/LengthComparisonOffByOne.expected b/go/ql/test/query-tests/InconsistentCode/LengthComparisonOffByOne/LengthComparisonOffByOne.expected index 497e6fbf8e0e..08c286f45305 100644 --- a/go/ql/test/query-tests/InconsistentCode/LengthComparisonOffByOne/LengthComparisonOffByOne.expected +++ b/go/ql/test/query-tests/InconsistentCode/LengthComparisonOffByOne/LengthComparisonOffByOne.expected @@ -2,3 +2,5 @@ | main.go:6:5:6:15 | ...<=... | Off-by-one index comparison against length may lead to out-of-bounds $@. | main.go:7:10:7:13 | index expression | read | | main.go:29:5:29:14 | ...>... | Off-by-one index comparison against length may lead to out-of-bounds $@. | main.go:30:10:30:13 | index expression | read | | main.go:87:7:87:17 | ...>=... | Off-by-one index comparison against length may lead to out-of-bounds $@. | main.go:93:9:93:12 | index expression | read | +| main.go:100:7:100:17 | call to len | Off-by-one index comparison against length may lead to out-of-bounds $@. | main.go:101:10:101:18 | index expression | read | +| main.go:124:5:124:20 | ...<=... | Off-by-one index comparison against length may lead to out-of-bounds $@. | main.go:129:11:129:19 | index expression | read | diff --git a/go/ql/test/query-tests/InconsistentCode/LengthComparisonOffByOne/main.go b/go/ql/test/query-tests/InconsistentCode/LengthComparisonOffByOne/main.go index f0f1d6168638..73ec7dd6d59c 100644 --- a/go/ql/test/query-tests/InconsistentCode/LengthComparisonOffByOne/main.go +++ b/go/ql/test/query-tests/InconsistentCode/LengthComparisonOffByOne/main.go @@ -94,3 +94,52 @@ func f11(a []int) int { } func main() {} + +func switchLengthMatch(values []int, i int) int { + switch i { + case len(values): // $ Alert + return values[i] // $ Source + } + return 0 +} + +func switchLengthMultiCase(values []int, i int) int { + switch i { + case 0, len(values): + return values[i] // No single equality controls this read. + } + return 0 +} + +func switchLengthFallthrough(values []int, i int) int { + switch i { + case len(values): + fallthrough + default: + return values[i] // Reaching default does not establish inequality. + } +} + +func switchLengthFallthroughUnsuppressed(values []int, i int) int { + if i <= len(values) { // $ Alert + switch i { + case len(values): + fallthrough + default: + return values[i] // $ Source + } + } + return 0 +} + +func switchLengthNonMatch(values []int, i int) int { + if i <= len(values) { + switch i { + case len(values): + return 0 + default: + return values[i] // The non-match excludes i == len(values). + } + } + return 0 +} From 22ee35a6ea46ad171a875b20f697ef74b921ad6d Mon Sep 17 00:00:00 2001 From: Owen Mansel-Chan Date: Wed, 2 Dec 2026 16:38:37 +0000 Subject: [PATCH 2/2] Go: Deprecate Boolean guard relationship helpers Keep the Boolean helpers as compatibility wrappers and direct callers to the match-aware helpers with Guard.valueControls. Update older migration guidance and migrate the equality-helper test to cover Boolean and switch match outcomes. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- ...6-12-02-deprecated-boolean-guard-helpers.md | 4 ++++ .../2026-12-02-match-aware-guard-helpers.md | 2 +- .../semmle/go/controlflow/ControlFlowGraph.qll | 12 ++++++------ go/ql/lib/semmle/go/controlflow/Guards.qll | 15 +++++++++------ .../go/controlflow/Guards/Guards.expected | 18 ++++++++++++++++++ .../semmle/go/controlflow/Guards/Guards.ql | 6 +++--- 6 files changed, 41 insertions(+), 16 deletions(-) create mode 100644 go/ql/lib/change-notes/2026-12-02-deprecated-boolean-guard-helpers.md diff --git a/go/ql/lib/change-notes/2026-12-02-deprecated-boolean-guard-helpers.md b/go/ql/lib/change-notes/2026-12-02-deprecated-boolean-guard-helpers.md new file mode 100644 index 000000000000..42e7ea9c6224 --- /dev/null +++ b/go/ql/lib/change-notes/2026-12-02-deprecated-boolean-guard-helpers.md @@ -0,0 +1,4 @@ +--- +category: deprecated +--- +* Deprecated `guardEnsuresEq`, `guardEnsuresNeq`, and `guardEnsuresLeq`. Use `guardValueEnsuresEq`, `guardValueEnsuresNeq`, and `guardValueEnsuresLeq`, respectively, with a `GuardValue` and `Guard.valueControls` to also support switch match outcomes. diff --git a/go/ql/lib/change-notes/2026-12-02-match-aware-guard-helpers.md b/go/ql/lib/change-notes/2026-12-02-match-aware-guard-helpers.md index 1ced3fa4d93b..da8ebbdc2a02 100644 --- a/go/ql/lib/change-notes/2026-12-02-match-aware-guard-helpers.md +++ b/go/ql/lib/change-notes/2026-12-02-match-aware-guard-helpers.md @@ -1,4 +1,4 @@ --- category: feature --- -* Added `guardValueEnsuresEq`, `guardValueEnsuresNeq`, and `guardValueEnsuresLeq` to support switch match outcomes as well as Boolean guard values. The existing Boolean helpers remain available. +* Added `guardValueEnsuresEq`, `guardValueEnsuresNeq`, and `guardValueEnsuresLeq` to support switch match outcomes as well as Boolean guard values. diff --git a/go/ql/lib/semmle/go/controlflow/ControlFlowGraph.qll b/go/ql/lib/semmle/go/controlflow/ControlFlowGraph.qll index acbebcc87449..1b34643271b0 100644 --- a/go/ql/lib/semmle/go/controlflow/ControlFlowGraph.qll +++ b/go/ql/lib/semmle/go/controlflow/ControlFlowGraph.qll @@ -292,8 +292,8 @@ module ControlFlow { } /** - * DEPRECATED: Use `guardEnsuresLeq` from `semmle.go.controlflow.Guards` - * instead. + * DEPRECATED: Use `guardValueEnsuresLeq` from `semmle.go.controlflow.Guards` + * together with `Guard.valueControls` instead. * * Holds if this guard ensures that `lesser <= greater + bias` holds. */ @@ -311,8 +311,8 @@ module ControlFlow { } /** - * DEPRECATED: Use `guardEnsuresEq` from `semmle.go.controlflow.Guards` - * instead. + * DEPRECATED: Use `guardValueEnsuresEq` from `semmle.go.controlflow.Guards` + * together with `Guard.valueControls` instead. * * Holds if this guard ensures that `i = j` holds. */ @@ -324,8 +324,8 @@ module ControlFlow { } /** - * DEPRECATED: Use `guardEnsuresNeq` from `semmle.go.controlflow.Guards` - * instead. + * DEPRECATED: Use `guardValueEnsuresNeq` from `semmle.go.controlflow.Guards` + * together with `Guard.valueControls` instead. * * Holds if this guard ensures that `i != j` holds. */ diff --git a/go/ql/lib/semmle/go/controlflow/Guards.qll b/go/ql/lib/semmle/go/controlflow/Guards.qll index c599403d2192..c9cf0f9978bf 100644 --- a/go/ql/lib/semmle/go/controlflow/Guards.qll +++ b/go/ql/lib/semmle/go/controlflow/Guards.qll @@ -481,10 +481,11 @@ pragma[inline] predicate guardEnsures(Expr e, boolean b, BasicBlock bb) { e.(Guard).controls(bb, b) } /** + * DEPRECATED: Use `guardValueEnsuresEq` with a `GuardValue` and `Guard.valueControls` instead. + * * Holds if `guard` evaluating to `branch` ensures that `i = j` holds. - * Use `guardValueEnsuresEq` to also support switch match outcomes. */ -predicate guardEnsuresEq(Guard guard, boolean branch, DataFlow::Node i, DataFlow::Node j) { +deprecated predicate guardEnsuresEq(Guard guard, boolean branch, DataFlow::Node i, DataFlow::Node j) { guardValueEnsuresEq(guard, any(GuardValue value | value.asBooleanValue() = branch), i, j) } @@ -494,10 +495,11 @@ predicate guardValueEnsuresEq(Guard guard, GuardValue value, DataFlow::Node i, D } /** + * DEPRECATED: Use `guardValueEnsuresNeq` with a `GuardValue` and `Guard.valueControls` instead. + * * Holds if `guard` evaluating to `branch` ensures that `i != j` holds. - * Use `guardValueEnsuresNeq` to also support switch match outcomes. */ -predicate guardEnsuresNeq(Guard guard, boolean branch, DataFlow::Node i, DataFlow::Node j) { +deprecated predicate guardEnsuresNeq(Guard guard, boolean branch, DataFlow::Node i, DataFlow::Node j) { guardValueEnsuresNeq(guard, any(GuardValue value | value.asBooleanValue() = branch), i, j) } @@ -510,11 +512,12 @@ predicate guardValueEnsuresNeq(Guard guard, GuardValue value, DataFlow::Node i, } /** + * DEPRECATED: Use `guardValueEnsuresLeq` with a `GuardValue` and `Guard.valueControls` instead. + * * Holds if `guard` evaluating to `branch` ensures that `lesser <= greater + bias` * holds. - * Use `guardValueEnsuresLeq` to also support switch match outcomes. */ -predicate guardEnsuresLeq( +deprecated predicate guardEnsuresLeq( Guard guard, boolean branch, DataFlow::Node lesser, DataFlow::Node greater, int bias ) { guardValueEnsuresLeq(guard, any(GuardValue value | value.asBooleanValue() = branch), lesser, diff --git a/go/ql/test/library-tests/semmle/go/controlflow/Guards/Guards.expected b/go/ql/test/library-tests/semmle/go/controlflow/Guards/Guards.expected index 124cb413999b..b67aa8ba4e44 100644 --- a/go/ql/test/library-tests/semmle/go/controlflow/Guards/Guards.expected +++ b/go/ql/test/library-tests/semmle/go/controlflow/Guards/Guards.expected @@ -190,6 +190,18 @@ valueControlsResult ensuresEqResult | guards.go:14:7:14:16 | ...==... | 0 = value | true | | guards.go:14:7:14:16 | ...==... | value = 0 | true | +| guards.go:23:7:23:7 | 0 | 0 = value | match | +| guards.go:23:7:23:7 | 0 | value = 0 | match | +| guards.go:25:7:25:7 | 1 | 1 = value | match | +| guards.go:25:7:25:7 | 1 | value = 1 | match | +| guards.go:25:10:25:10 | 2 | 2 = value | match | +| guards.go:25:10:25:10 | 2 | value = 2 | match | +| guards.go:34:7:34:11 | value | false = value | match | +| guards.go:34:7:34:11 | value | value = false | match | +| guards.go:38:7:38:11 | value | true = value | match | +| guards.go:38:7:38:11 | value | value = true | match | +| guards.go:42:7:42:17 | ...<... | ...<... = false | match | +| guards.go:42:7:42:17 | ...<... | false = ...<... | match | | guards.go:56:5:56:20 | ...==... | 0 = type conversion | true | | guards.go:56:5:56:20 | ...==... | type conversion = 0 | true | | guards.go:59:5:59:19 | ...==... | 0 = type conversion | true | @@ -212,6 +224,12 @@ ensuresEqResult | guards.go:122:5:122:32 | ...==... | value = call to Errorf | true | | guards.go:126:5:126:28 | ...==... | call to returnsNonNil = value | true | | guards.go:126:5:126:28 | ...==... | value = call to returnsNonNil | true | +| guards.go:137:7:137:7 | 0 | 0 = value | match | +| guards.go:137:7:137:7 | 0 | value = 0 | match | +| guards.go:148:7:148:7 | 0 | 0 = value | match | +| guards.go:148:7:148:7 | 0 | value = 0 | match | +| guards.go:171:7:171:7 | 0 | 0 = value | match | +| guards.go:171:7:171:7 | 0 | value = 0 | match | | guards.go:179:9:179:22 | ...!=... | nil = pointer | false | | guards.go:179:9:179:22 | ...!=... | pointer = nil | false | | guards.go:189:10:189:23 | ...!=... | nil = pointer | false | diff --git a/go/ql/test/library-tests/semmle/go/controlflow/Guards/Guards.ql b/go/ql/test/library-tests/semmle/go/controlflow/Guards/Guards.ql index 68db56483224..7e0879d1cace 100644 --- a/go/ql/test/library-tests/semmle/go/controlflow/Guards/Guards.ql +++ b/go/ql/test/library-tests/semmle/go/controlflow/Guards/Guards.ql @@ -24,10 +24,10 @@ query predicate valueControlsResult(Guard guard, string label, string outcome) { } query predicate ensuresEqResult(Guard guard, string label, string outcome) { - exists(boolean branch, DataFlow::Node left, DataFlow::Node right | - guardEnsuresEq(guard, branch, left, right) and + exists(GuardValue value, DataFlow::Node left, DataFlow::Node right | + guardValueEnsuresEq(guard, value, left, right) and label = left.toString() + " = " + right.toString() and - outcome = branch.toString() + outcome = value.toString() ) }