Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

False Negative: ConstantLoopCondition.ql misses loops whose exit condition stays constant after being wrapped in helpers.

未关闭
#21,537 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
冷清
技术栈
java
领域
devtools, security

调研方向

从 Likely Bugs/Termination/ConstantLoopCondition.ql 开始,检查现有测试如何覆盖常量循环条件。在 PosCase1_Var5.java、PosCase2_Var4.java 和 PosCase5_Var5.java 中复现未检测到的情况,然后运行相关的查询测试。完成的标准是:检测出全部三个由 helper 包装或由 guard 保护的非终止循环,同时不丢失现有覆盖率。

由索引模型根据 Issue 内容生成。

描述

Version
codeql 2.24.3

Checker

  • Checker id: Likely Bugs/Termination/ConstantLoopCondition.ql
  • Checker description: This checker detects loop conditions that are constant within the loop body, potentially leading to non-terminating loops.

Description of the false negative

These loops are still non-terminating for the same reason as the direct patterns: the value that controls exit never changes inside the loop. The code only wraps that check in a helper or rewrites the exit test into an equivalent form.

That should not move the sample outside the scope of Likely Bugs/Termination/ConstantLoopCondition.ql.

Affected test cases

PosCase1_Var5.java

x is never updated, so check(x) never changes. The helper only obscures an otherwise obvious infinite loop.

// while loop with condition variable defined outside and no updates in body should be flagged as infinite loop
package scensct.var.pos;

public class PosCase1_Var5 {
    private static boolean check(int val) {
        return val > 0;
    }
    
    public static void main(String[] args) {
        int x = 5;
        // condition via helper method, x not updated
        while (check(x)) {
            System.out.println("stuck via method");
        }
    }
}
PosCase2_Var4.java

conditionHolds(y) is stable because y is never modified in the loop body. This is still a constant loop condition.

// while loop with condition using final field and variable defined outside should be flagged as infinite loop
package scensct.var.pos;

public class PosCase2_Var4 {
    private static final int LIMIT = 10;
    public static void main(String[] args) {
        int y = 3;
        // Extract condition evaluation to a helper method
        while (conditionHolds(y)) {
            System.out.println("stuck");
        }
    }
    private static boolean conditionHolds(int val) {
        return val < LIMIT;
    }
}
PosCase5_Var5.java

The loop exit is written as a guarded return, but counter never changes, so shouldExit(counter) remains false forever and the loop still does not terminate.

// while true loop with if condition using variable defined outside controlling all exits should be flagged as infinite loop
package scensct.var.pos;

public class PosCase5_Var5 {
    // Helper method to compute constant condition
    private static boolean shouldExit(int c) {
        return c == 1;
    }

    public static void main(String[] args) {
        int counter = 0;
        while (true) {
            // Exit condition hidden in method call
            if (shouldExit(counter)) {
                return;
            }
            System.out.println("stuck");
        }
    }
}

Cause analysis

The misses point to a query that is matching specific loop syntax rather than the underlying stability of the condition. Once the condition is computed in a helper or turned into an equivalent exit guard, the analysis seems to stop following it.

That leaves a real blind spot. Developers often factor loop predicates into helpers, and those helpers do not make a non-terminating loop any safer.

References

None known.

主要语言
CodeQL
星标
10.1k
派生
2.1k
平均合并
2 天 16 小时
30 天内合并 PR
143

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

github/codeql 的其他 Issue

查看 github/codeql 的全部 Issue

相似的 Issue

更多 DevTools Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。