From 96550f39087ea6de5d68dc87ad4f0ef6fca1d9c9 Mon Sep 17 00:00:00 2001 From: aschackmull <28296824+aschackmull@users.noreply.github.com> Date: Fri, 4 Dec 2026 10:49:29 +0000 Subject: [PATCH] Java: Deprecate condition node and block Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> --- ...026-12-04-deprecate-condition-node-block.md | 4 ++++ .../lib/semmle/code/java/ControlFlowGraph.qll | 8 ++++++-- .../semmle/code/java/controlflow/Guards.qll | 4 +++- .../java/controlflow/UnreachableBlocks.qll | 13 ++++++------- .../lib/semmle/code/java/dataflow/Nullness.qll | 10 +++++----- .../lib/semmle/code/java/dataflow/TypeFlow.qll | 2 +- .../java/security/ConditionalBypassQuery.qll | 12 ++++++------ .../java/security/NumericCastTaintedQuery.qll | 6 +++--- .../java/security/internal/BoundingChecks.qll | 5 +---- .../Comparison/UselessComparisonTest.qll | 9 ++++----- .../Likely Bugs/Concurrency/UnreleasedLock.ql | 18 +++++------------- .../Termination/ConstantLoopCondition.ql | 6 +++--- java/ql/src/Security/CWE/CWE-367/TOCTOURace.ql | 6 +++++- .../src/Security/CWE/CWE-421/SocketAuthRace.ql | 6 +++--- .../2026-12-04-guard-reasoning-loop-exits.md | 4 ++++ .../CWE/CWE-200/SensitiveAndroidFileLeak.ql | 9 +++------ java/ql/test/library-tests/guards/guards.ql | 8 ++++---- .../CWE-367/semmle/tests/TOCTOURace.expected | 1 + .../security/CWE-367/semmle/tests/Test.java | 12 ++++++++++++ .../CWE-835/semmle/tests/InfiniteLoop.expected | 1 + .../CWE-835/semmle/tests/InfiniteLoop.java | 14 ++++++++++++++ 21 files changed, 94 insertions(+), 64 deletions(-) create mode 100644 java/ql/lib/change-notes/2026-12-04-deprecate-condition-node-block.md create mode 100644 java/ql/src/change-notes/2026-12-04-guard-reasoning-loop-exits.md diff --git a/java/ql/lib/change-notes/2026-12-04-deprecate-condition-node-block.md b/java/ql/lib/change-notes/2026-12-04-deprecate-condition-node-block.md new file mode 100644 index 000000000000..ef6a6687cb26 --- /dev/null +++ b/java/ql/lib/change-notes/2026-12-04-deprecate-condition-node-block.md @@ -0,0 +1,4 @@ +--- +category: deprecated +--- +* The classes `ConditionNode` and `ConditionBlock` have been deprecated. Use `Guard` from `semmle.code.java.controlflow.Guards` instead, for example `Guard.controls`, `Guard.directlyControls`, or `Guard.hasBranchEdge`. diff --git a/java/ql/lib/semmle/code/java/ControlFlowGraph.qll b/java/ql/lib/semmle/code/java/ControlFlowGraph.qll index d8cdfe63ae84..34ad0b1c44e8 100644 --- a/java/ql/lib/semmle/code/java/ControlFlowGraph.qll +++ b/java/ql/lib/semmle/code/java/ControlFlowGraph.qll @@ -676,8 +676,12 @@ private module Input implements InputSig1, InputSig2 { } } -/** A control-flow node that branches based on a boolean condition. */ -class ConditionNode extends ControlFlowNode { +/** + * DEPRECATED: Use `Guard` from `semmle.code.java.controlflow.Guards` instead. + * + * A control-flow node that branches based on a boolean condition. + */ +deprecated class ConditionNode extends ControlFlowNode { ConditionNode() { exists(this.getASuccessor(any(BooleanSuccessor t))) } /** Gets a true- or false-successor of the `ConditionNode`. */ diff --git a/java/ql/lib/semmle/code/java/controlflow/Guards.qll b/java/ql/lib/semmle/code/java/controlflow/Guards.qll index ba38e7fe91e7..ed7308b05b04 100644 --- a/java/ql/lib/semmle/code/java/controlflow/Guards.qll +++ b/java/ql/lib/semmle/code/java/controlflow/Guards.qll @@ -13,9 +13,11 @@ private import codeql.controlflow.Guards as SharedGuards private import codeql.controlflow.SuccessorType /** + * DEPRECATED: Use `Guard` instead. + * * A basic block that terminates in a condition, splitting the subsequent control flow. */ -class ConditionBlock extends BasicBlock { +deprecated class ConditionBlock extends BasicBlock { ConditionBlock() { this.getLastNode() instanceof ConditionNode } /** Gets the last node of this basic block. */ diff --git a/java/ql/lib/semmle/code/java/controlflow/UnreachableBlocks.qll b/java/ql/lib/semmle/code/java/controlflow/UnreachableBlocks.qll index 6ac7668266ea..473495be4f7d 100644 --- a/java/ql/lib/semmle/code/java/controlflow/UnreachableBlocks.qll +++ b/java/ql/lib/semmle/code/java/controlflow/UnreachableBlocks.qll @@ -199,14 +199,13 @@ class ConstSwitchStmt extends SwitchStmt { */ class UnreachableBasicBlock extends BasicBlock { UnreachableBasicBlock() { - // Condition blocks with a constant condition that causes a true/false successor to be - // unreachable. Note: conditions including a single boolean literal e.g. if (false) are not - // modeled as a ConditionBlock - this case is covered by the blocks-without-a-predecessor - // check below. - exists(ConditionBlock conditionBlock, boolean constant | - constant = conditionBlock.getCondition().(ConstantExpr).getBooleanValue() + // Guards with a constant condition that causes a true/false successor to be + // unreachable. Note: conditions including a single boolean literal e.g. if (false) do not + // branch - this case is covered by the blocks-without-a-predecessor check below. + exists(Guard guard, boolean constant | + constant = guard.(ConstantExpr).getBooleanValue() | - conditionBlock.controls(this, constant.booleanNot()) + guard.directlyControls(this, constant.booleanNot()) ) or // This block is not reachable in the CFG, and is not the entrypoint in a callable, an diff --git a/java/ql/lib/semmle/code/java/dataflow/Nullness.qll b/java/ql/lib/semmle/code/java/dataflow/Nullness.qll index 99ca17162dde..b4541e9e18f3 100644 --- a/java/ql/lib/semmle/code/java/dataflow/Nullness.qll +++ b/java/ql/lib/semmle/code/java/dataflow/Nullness.qll @@ -68,7 +68,7 @@ private predicate unboxed(Expr e) { or exists(ChooseExpr cond | cond.getType() instanceof PrimitiveType | cond.getAResultExpr() = e) or - exists(ConditionNode cond | cond.getCondition() = e) + exists(Guard cond | cond = e and cond.hasBranchEdge(_, _, _)) or exists(Parameter p | p.getType() instanceof PrimitiveType and p.getAnArgument() = e) or @@ -188,9 +188,9 @@ private Expr nonEmptyExpr() { v.(SsaExplicitWrite).getValue() = nonEmptyExpr() or // ...or it is guarded by a condition proving its length to be non-zero. - exists(ConditionBlock cond, boolean branch, FieldAccess length | + exists(Guard cond, boolean branch, FieldAccess length | cond.controls(result.getBasicBlock(), branch) and - cond.getCondition() = nonZeroGuard(length, branch) and + cond = nonZeroGuard(length, branch) and length.getField().hasName("length") and length.getQualifier() = v.getARead() ) @@ -200,7 +200,7 @@ private Expr nonEmptyExpr() { // A use of a Collection variable is non-empty if... result = v.getARead() and v.getSourceVariable().getType() instanceof CollectionType and - exists(ConditionBlock cond, boolean branch, Expr c | + exists(Guard cond, boolean branch, Expr c | // ...it is guarded by a condition... cond.controls(result.getBasicBlock(), branch) and // ...and it isn't modified in the scope of the condition... @@ -211,7 +211,7 @@ private Expr nonEmptyExpr() { | m instanceof CollectionQueryMethod ) and - cond.getCondition() = c + cond = c | // ...and the condition proves that it is non-empty, either by using the `isEmpty` method... c.(MethodCall).getMethod().hasName("isEmpty") and diff --git a/java/ql/lib/semmle/code/java/dataflow/TypeFlow.qll b/java/ql/lib/semmle/code/java/dataflow/TypeFlow.qll index 9661255431b4..9296593bf4bc 100644 --- a/java/ql/lib/semmle/code/java/dataflow/TypeFlow.qll +++ b/java/ql/lib/semmle/code/java/dataflow/TypeFlow.qll @@ -362,7 +362,7 @@ private module Input implements TypeFlowInput { /** Holds if `ioe` checks `v` and its true-successor is `bb`. */ private predicate instanceofTrueSuccessor(InstanceOfExpr ioe, BasicBlock bb, Base::SsaDefinition v) { ioe.getExpr() = v.getARead() and - exists(ConditionBlock cb | cb.getCondition() = ioe and cb.getTestSuccessor(true) = bb) + ioe.(Guard).hasBranchEdge(_, bb, true) } /** diff --git a/java/ql/lib/semmle/code/java/security/ConditionalBypassQuery.qll b/java/ql/lib/semmle/code/java/security/ConditionalBypassQuery.qll index babf129f19e6..51f4d5c3682d 100644 --- a/java/ql/lib/semmle/code/java/security/ConditionalBypassQuery.qll +++ b/java/ql/lib/semmle/code/java/security/ConditionalBypassQuery.qll @@ -12,14 +12,14 @@ import semmle.code.java.controlflow.Guards * Holds if `ma` is controlled by the condition expression `e`. */ predicate conditionControlsMethod(MethodCall ma, Expr e) { - exists(ConditionBlock cb, SensitiveExecutionMethod m, boolean cond | + exists(Guard g, SensitiveExecutionMethod m, boolean cond | ma.getMethod() = m and - cb.controls(ma.getBasicBlock(), cond) and - not cb.controls(any(SensitiveExecutionMethod sem).getAReference().getBasicBlock(), + g.controls(ma.getBasicBlock(), cond) and + not g.controls(any(SensitiveExecutionMethod sem).getAReference().getBasicBlock(), cond.booleanNot()) and - not cb.controls(any(ThrowStmt t).getBasicBlock(), cond.booleanNot()) and - not cb.controls(any(ReturnStmt r).getBasicBlock(), cond.booleanNot()) and - e = cb.getCondition() + not g.controls(any(ThrowStmt t).getBasicBlock(), cond.booleanNot()) and + not g.controls(any(ReturnStmt r).getBasicBlock(), cond.booleanNot()) and + e = g ) } diff --git a/java/ql/lib/semmle/code/java/security/NumericCastTaintedQuery.qll b/java/ql/lib/semmle/code/java/security/NumericCastTaintedQuery.qll index a4c3785feead..616c5fae62e6 100644 --- a/java/ql/lib/semmle/code/java/security/NumericCastTaintedQuery.qll +++ b/java/ql/lib/semmle/code/java/security/NumericCastTaintedQuery.qll @@ -43,10 +43,10 @@ class RightShiftOp extends Expr { } private predicate boundedRead(VarRead read) { - exists(SsaDefinition v, ConditionBlock cb, ComparisonExpr comp, boolean testIsTrue | + exists(SsaDefinition v, Guard g, ComparisonExpr comp, boolean testIsTrue | read = v.getARead() and - cb.controls(read.getBasicBlock(), testIsTrue) and - cb.getCondition() = comp + g.controls(read.getBasicBlock(), testIsTrue) and + g = comp | comp.getLesserOperand() = v.getARead() and testIsTrue = true or diff --git a/java/ql/lib/semmle/code/java/security/internal/BoundingChecks.qll b/java/ql/lib/semmle/code/java/security/internal/BoundingChecks.qll index 81bf8727e4fd..64729f712cef 100644 --- a/java/ql/lib/semmle/code/java/security/internal/BoundingChecks.qll +++ b/java/ql/lib/semmle/code/java/security/internal/BoundingChecks.qll @@ -12,10 +12,7 @@ private import semmle.code.java.controlflow.Guards * Holds if the given `ComparisonExpr` is thought to be true when `VarAccess` is accessed. */ private predicate conditionHolds(ComparisonExpr ce, VarAccess va) { - exists(ConditionBlock cond | - cond.getCondition() = ce and - cond.controls(va.getBasicBlock(), true) - ) + ce.(Guard).controls(va.getBasicBlock(), true) } /** diff --git a/java/ql/src/Likely Bugs/Comparison/UselessComparisonTest.qll b/java/ql/src/Likely Bugs/Comparison/UselessComparisonTest.qll index 787cda448073..cd04079a9bda 100644 --- a/java/ql/src/Likely Bugs/Comparison/UselessComparisonTest.qll +++ b/java/ql/src/Likely Bugs/Comparison/UselessComparisonTest.qll @@ -25,19 +25,18 @@ class BoundKind extends string { /** * The information from `s1` implies that `test` always has the value `testIsTrue`. */ -predicate uselessTest(ConditionNode s1, BinaryExpr test, boolean testIsTrue) { +predicate uselessTest(Guard s1, BinaryExpr test, boolean testIsTrue) { exists( - ConditionBlock cb, SsaDefinition v, BinaryExpr cond, boolean condIsTrue, int k1, int k2, + SsaDefinition v, BinaryExpr cond, boolean condIsTrue, int k1, int k2, CompileTimeConstantExpr c1, CompileTimeConstantExpr c2 | - s1.getCondition() = cond and - cb.getCondition() = cond and + s1 = cond and cond.hasOperands(v.getARead(), c1) and c1.getIntValue() = k1 and test.hasOperands(v.getARead(), c2) and c2.getIntValue() = k2 and v.getSourceVariable().getVariable() instanceof LocalScopeVariable and - cb.controls(test.getBasicBlock(), condIsTrue) and + s1.controls(test.getBasicBlock(), condIsTrue) and v.getSourceVariable().getType() instanceof IntegralType and exists(BoundKind boundKind, int bound | // Simple range analysis. We infer a bound based on `cond` being diff --git a/java/ql/src/Likely Bugs/Concurrency/UnreleasedLock.ql b/java/ql/src/Likely Bugs/Concurrency/UnreleasedLock.ql index 4a4be748a283..50b38618d6be 100644 --- a/java/ql/src/Likely Bugs/Concurrency/UnreleasedLock.ql +++ b/java/ql/src/Likely Bugs/Concurrency/UnreleasedLock.ql @@ -60,7 +60,7 @@ predicate failedLock(LockType t, BasicBlock lockblock, BasicBlock exblock) { ) and ( lock.getAnExceptionSuccessor() = exblock.getFirstNode() or - lock.(ConditionNode).getAFalseSuccessor() = exblock.getFirstNode() + lock.asExpr().(Guard).hasBranchEdge(lockblock, exblock, false) ) ) } @@ -70,12 +70,7 @@ predicate failedLock(LockType t, BasicBlock lockblock, BasicBlock exblock) { * successor. */ predicate heldByCurrentThreadCheck(LockType t, BasicBlock checkblock, BasicBlock falsesucc) { - exists(ConditionBlock conditionBlock | - conditionBlock.getCondition() = t.getIsHeldByCurrentThreadAccess() - | - conditionBlock = checkblock and - conditionBlock.getTestSuccessor(false) = falsesucc - ) + t.getIsHeldByCurrentThreadAccess().(Guard).hasBranchEdge(checkblock, falsesucc, false) } /** @@ -85,16 +80,13 @@ predicate heldByCurrentThreadCheck(LockType t, BasicBlock checkblock, BasicBlock * the true successor of `checkblock` must contain an unlock access. */ predicate variableLockStateCheck(LockType t, BasicBlock checkblock, BasicBlock falsesucc) { - exists(ConditionBlock conditionBlock, VarAccess v | + exists(VarAccess v | v.getType() instanceof BooleanType and // Ensure that a lock access is assigned to the variable v.getVariable().getAnAssignedValue() = t.getLockAccess() and // Ensure that the `true` successor of the condition block contains an unlock access - conditionBlock.getTestSuccessor(true) = t.getUnlockAccess().getBasicBlock() and - conditionBlock.getCondition() = v - | - conditionBlock = checkblock and - conditionBlock.getTestSuccessor(false) = falsesucc + v.(Guard).hasBranchEdge(checkblock, t.getUnlockAccess().getBasicBlock(), true) and + v.(Guard).hasBranchEdge(checkblock, falsesucc, false) ) } diff --git a/java/ql/src/Likely Bugs/Termination/ConstantLoopCondition.ql b/java/ql/src/Likely Bugs/Termination/ConstantLoopCondition.ql index edc800405f72..ab1553b458a6 100644 --- a/java/ql/src/Likely Bugs/Termination/ConstantLoopCondition.ql +++ b/java/ql/src/Likely Bugs/Termination/ConstantLoopCondition.ql @@ -43,10 +43,10 @@ predicate loopExit(LoopStmt loop, Stmt exit) { * `break` statements that can exit the loop. */ predicate loopExitGuard(LoopStmt loop, Expr cond) { - exists(ConditionBlock cb, boolean branch | - cond = cb.getCondition() and + exists(Guard g, boolean branch | + cond = g and cond.getEnclosingStmt().getEnclosingStmt*() = loop.getBody() and - forex(Stmt exit | loopExit(loop, exit) | cb.controls(exit.getBasicBlock(), branch)) + forex(Stmt exit | loopExit(loop, exit) | g.controls(exit.getBasicBlock(), branch)) ) } diff --git a/java/ql/src/Security/CWE/CWE-367/TOCTOURace.ql b/java/ql/src/Security/CWE/CWE-367/TOCTOURace.ql index 521c7a1ca909..59af7d400e72 100644 --- a/java/ql/src/Security/CWE/CWE-367/TOCTOURace.ql +++ b/java/ql/src/Security/CWE/CWE-367/TOCTOURace.ql @@ -109,7 +109,11 @@ where not call1 = call2 and // The use is controlled by one of the branches of the condition, i.e. whether it // is reached actually depends on that condition. - call1.getBasicBlock().(ConditionBlock).controls(call2.getBasicBlock(), _) and + exists(Guard g | + // The branch must be reached through call1, even in a short-circuit expression. + g.hasBranchEdge(call1.getBasicBlock(), _, _) and + g.directlyControls(call2.getBasicBlock(), _) + ) and // Two calls to synchronized methods on the same variable. synchCallOn(call1, r) and synchCallOn(call2, r) and diff --git a/java/ql/src/Security/CWE/CWE-421/SocketAuthRace.ql b/java/ql/src/Security/CWE/CWE-421/SocketAuthRace.ql index 6e6e885c812a..79b91e811909 100644 --- a/java/ql/src/Security/CWE/CWE-421/SocketAuthRace.ql +++ b/java/ql/src/Security/CWE/CWE-421/SocketAuthRace.ql @@ -50,9 +50,9 @@ class ServerSocketChannelAcceptMethod extends ConnectionMethod { } predicate controlledByAuth(Expr controlled, Expr condition) { - exists(ConditionBlock b | - condition = b.getCondition() and - b.controls(controlled.getBasicBlock(), _) and + exists(Guard g | + condition = g and + g.controls(controlled.getBasicBlock(), _) and condition.(MethodCall).getMethod() instanceof AuthMethod ) } diff --git a/java/ql/src/change-notes/2026-12-04-guard-reasoning-loop-exits.md b/java/ql/src/change-notes/2026-12-04-guard-reasoning-loop-exits.md new file mode 100644 index 000000000000..341da56d4f2c --- /dev/null +++ b/java/ql/src/change-notes/2026-12-04-guard-reasoning-loop-exits.md @@ -0,0 +1,4 @@ +--- +category: minorAnalysis +--- +* The `java/unreachable-exit-in-loop` query now recognizes guards whose values flow through boolean variables. This may result in additional alerts. \ No newline at end of file diff --git a/java/ql/src/experimental/Security/CWE/CWE-200/SensitiveAndroidFileLeak.ql b/java/ql/src/experimental/Security/CWE/CWE-200/SensitiveAndroidFileLeak.ql index 5bfd5c194349..aee8582cc91e 100644 --- a/java/ql/src/experimental/Security/CWE/CWE-200/SensitiveAndroidFileLeak.ql +++ b/java/ql/src/experimental/Security/CWE/CWE-200/SensitiveAndroidFileLeak.ql @@ -34,15 +34,12 @@ deprecated module AndroidFileLeakConfig implements DataFlow::ConfigSig { */ predicate isSource(DataFlow::Node src) { exists( - OnActivityForResultMethod oafr, ConditionBlock cb, CompileTimeConstantExpr cc, - VarAccess intentVar + OnActivityForResultMethod oafr, Guard g, CompileTimeConstantExpr cc, VarAccess intentVar | - cb.getCondition() - .(ValueOrReferenceEqualsExpr) - .hasOperands(oafr.getParameter(0).getAnAccess(), cc) and + g.(ValueOrReferenceEqualsExpr).hasOperands(oafr.getParameter(0).getAnAccess(), cc) and cc.getIntValue() = any(AndroidFileIntentInput fi).getRequestCode() and intentVar.getType() instanceof TypeIntent and - cb.controls(intentVar.getBasicBlock(), true) and + g.controls(intentVar.getBasicBlock(), true) and src.asExpr() = intentVar ) } diff --git a/java/ql/test/library-tests/guards/guards.ql b/java/ql/test/library-tests/guards/guards.ql index 21a23534ed66..618ae73fc9da 100644 --- a/java/ql/test/library-tests/guards/guards.ql +++ b/java/ql/test/library-tests/guards/guards.ql @@ -2,8 +2,8 @@ import java import semmle.code.java.controlflow.Guards import utils.test.BasicBlock -from ConditionBlock cb, boolean testIsTrue, BasicBlock controlled +from Guard g, boolean testIsTrue, BasicBlock controlled where - cb.controls(controlled, testIsTrue) and - cb.getEnclosingCallable().getDeclaringType().hasName("Test") -select cb.getCondition(), testIsTrue, getFirstAstNode(controlled) + g.directlyControls(controlled, testIsTrue) and + g.getEnclosingCallable().getDeclaringType().hasName("Test") +select g, testIsTrue, getFirstAstNode(controlled) diff --git a/java/ql/test/query-tests/security/CWE-367/semmle/tests/TOCTOURace.expected b/java/ql/test/query-tests/security/CWE-367/semmle/tests/TOCTOURace.expected index f2dc9f8aa957..f4cbd00adae5 100644 --- a/java/ql/test/query-tests/security/CWE-367/semmle/tests/TOCTOURace.expected +++ b/java/ql/test/query-tests/security/CWE-367/semmle/tests/TOCTOURace.expected @@ -4,3 +4,4 @@ | Test.java:20:4:20:10 | act(...) | This uses the state of $@ which $@. But these are not jointly synchronized. | Test.java:17:32:17:42 | r | r | Test.java:19:7:19:18 | getState(...) | is checked at a previous call | | Test.java:27:4:27:10 | act(...) | This uses the state of $@ which $@. But these are not jointly synchronized. | Test.java:24:19:24:28 | r | r | Test.java:26:7:26:18 | getState(...) | is checked at a previous call | | Test.java:35:5:35:11 | act(...) | This uses the state of $@ which $@. But these are not jointly synchronized. | Test.java:31:19:31:28 | r | r | Test.java:34:8:34:19 | getState(...) | is checked at a previous call | +| Test.java:137:4:137:10 | act(...) | This uses the state of $@ which $@. But these are not jointly synchronized. | Test.java:135:43:135:52 | r | r | Test.java:136:17:136:28 | getState(...) | is checked at a previous call | diff --git a/java/ql/test/query-tests/security/CWE-367/semmle/tests/Test.java b/java/ql/test/query-tests/security/CWE-367/semmle/tests/Test.java index 11287896bbe4..12bcc5facf51 100644 --- a/java/ql/test/query-tests/security/CWE-367/semmle/tests/Test.java +++ b/java/ql/test/query-tests/security/CWE-367/semmle/tests/Test.java @@ -125,4 +125,16 @@ public void act() { public void sideEffect() { } } + + public synchronized void shortCircuitOr(Resource r) { + if (aField || r.getState()) { + r.act(); + } + } + + public synchronized void shortCircuitAnd(Resource r) { + if (aField && r.getState()) { + r.act(); // $ Alert + } + } } diff --git a/java/ql/test/query-tests/security/CWE-835/semmle/tests/InfiniteLoop.expected b/java/ql/test/query-tests/security/CWE-835/semmle/tests/InfiniteLoop.expected index eba0754a821a..f532f71f29e2 100644 --- a/java/ql/test/query-tests/security/CWE-835/semmle/tests/InfiniteLoop.expected +++ b/java/ql/test/query-tests/security/CWE-835/semmle/tests/InfiniteLoop.expected @@ -1 +1,2 @@ | InfiniteLoop.java:4:13:4:36 | for (...;...;...) | Loop might not terminate, as termination depends in part on $@ being false but it is always true. | InfiniteLoop.java:4:27:4:30 | ... < ... | this test | +| InfiniteLoop.java:17:13:17:25 | while (...) | Loop might not terminate, as termination depends in part on $@ being false but it is always true. | InfiniteLoop.java:17:20:17:24 | ... > ... | this test | diff --git a/java/ql/test/query-tests/security/CWE-835/semmle/tests/InfiniteLoop.java b/java/ql/test/query-tests/security/CWE-835/semmle/tests/InfiniteLoop.java index 75c540162670..bf7055170d63 100644 --- a/java/ql/test/query-tests/security/CWE-835/semmle/tests/InfiniteLoop.java +++ b/java/ql/test/query-tests/security/CWE-835/semmle/tests/InfiniteLoop.java @@ -10,4 +10,18 @@ public void bad() { private boolean shouldBreak() { return Math.random() < 0.5; } + + void indirectGuard(int x) { + boolean positive = x > 0; + if (positive) { + while (x > 0) { // $ Alert + if (shouldBreak()) break; + } + } + if (!positive) { + while (x > 0) { + if (shouldBreak()) break; + } + } + } }