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

False Negative: IterableIterator.ql misses `iterator() == this` implementations once the guard logic is hidden behind helpers or trivial control flow.

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

还没有人认领这个 Issue。

评估

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

调研方向

从 Language Abuse/IterableIterator.ql checker 开始,将其匹配逻辑与 PosCase4_Var2.java、PosCase4_Var3.java 和 PosCase4_Var4.java 进行比较。更新 checker 和回归覆盖范围,使三个自迭代实现即使存在 helper 调用或平凡的控制流也都会被标记,同时保留预期的 guard 行为。

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

描述

False Negative: IterableIterator.ql misses iterator() == this implementations once the guard logic is hidden behind helpers or trivial control flow.

Version
codeql 2.24.3

Checker

  • Checker id: Language Abuse/IterableIterator.ql
  • Checker description: This checker detects classes that implement Iterable by returning themselves as the Iterator but lack a guard to prevent multiple concurrent iterations.

Description of the false negative

All three samples still implement the same dangerous pattern: iterator() returns this, the object is also its own Iterator, and hasNext() does not provide a real guard against repeated or concurrent iteration. The only changes are that this is returned through a helper or ternary expression, and hasNext() is dressed up with extra control flow.

Affected test cases

PosCase4_Var2.java

The class still returns itself as the iterator and still lacks a real reuse guard. The helper only hides that.

// A class implements Iterable, declares an iterator() method that returns "this", and any declared method named "hasNext" has a body with exactly one statement, which is not a return statement returning the boolean literal false should be flagged as lacking a guard against multiple concurrent iterations.
package scensct.var.pos;

import java.util.Iterator;

public class PosCase4_Var2 implements Iterable<Object>, Iterator<Object> {
    // Keep iterator returning this
    public Iterator<Object> iterator() {
        Iterator<Object> it = this;
        return it;
    }

    // hasNext with a try-catch that doesn't affect the single return statement
    public boolean hasNext() {
        try {
            return 1 < 2;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    // next with a dummy operation
    public Object next() {
        System.gc();
        return null;
    }
}
PosCase4_Var3.java

This is the same unsafe self-iterable pattern with one extra layer of indirection.

// A class implements Iterable, declares an iterator() method that returns "this", and any declared method named "hasNext" has a body with exactly one statement, which is not a return statement returning the boolean literal false should be flagged as lacking a guard against multiple concurrent iterations.
package scensct.var.pos;

import java.util.Iterator;

public class PosCase4_Var3 implements Iterable<Object>, Iterator<Object> {
    // Inline a helper method call in iterator
    public Iterator<Object> iterator() {
        return getSelf();
    }

    private Iterator<Object> getSelf() {
        return this;
    }

    // hasNext with a single statement that computes true via method call
    public boolean hasNext() {
        return checkHasNext();
    }

    private boolean checkHasNext() {
        return true;
    }

    public Object next() {
        return "dummy";
    }
}
PosCase4_Var4.java

The control-flow refactoring does not change the fact that repeated iteration is still unsafe.

// A class implements Iterable, declares an iterator() method that returns "this", and any declared method named "hasNext" has a body with exactly one statement, which is not a return statement returning the boolean literal false should be flagged as lacking a guard against multiple concurrent iterations.
package scensct.var.pos;

import java.util.Iterator;

public class PosCase4_Var4 implements Iterable<Object>, Iterator<Object> {
    // iterator returns this via a ternary operator (trivial)
    public Iterator<Object> iterator() {
        return (System.currentTimeMillis() > 0) ? this : this;
    }

    // hasNext with a single statement that is not a simple return false
    public boolean hasNext() {
        for (int i = 0; i < 1; i++) {
            return i == 0;
        }
        return false; // This line is unreachable, but the method body still has one reachable return
    }

    public Object next() {
        return Integer.valueOf(42);
    }
}

Cause analysis

The query appears to rely on a very direct return this / return false style match. Once either method is wrapped in a helper, a ternary, or a small control-flow construct, it stops recognizing the same iterator misuse pattern.

That is narrower than it should be. These implementations are still self-iterating objects without a real reentrancy guard.

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 摘要。