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

False Positive: CloseReader.ql reports resources that are either transferred to another owner or closed by a wrapper.

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

还没有人认领这个 Issue。

评估

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

调研方向

阅读 Likely Bugs/Resource Leaks/CloseReader.ql,并将其行为与 NegCase3.java 和 NegCase7.java 进行比较,然后运行受影响的查询测试。当这两行已报告的代码不再被标记,同时真正未关闭的 Reader、InputStream 和 ZipFile 情况仍能被检测到时,即表示完成。

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

描述

question

Version
codeql 2.24.3

Checker

  • Checker id: Likely Bugs/Resource Leaks/CloseReader.ql
  • Checker description: This checker detects instances of Reader, InputStream, or ZipFile objects that are created but not guaranteed to be closed on method exit, potentially causing resource leaks.

Description of the false positive

Neither sample is a straightforward leak.

In NegCase3.java, the FileInputStream is immediately wrapped by a type whose close() implementation delegates to the underlying stream. In NegCase7.java, the stream is passed into another object and stored there, which is an ownership-transfer pattern rather than a local leak.

Affected test cases

NegCase3.java

The wrapper is the owner. Reporting the inner FileInputStream as leaked means the query is not trusting a wrapper that clearly forwards close().

NegCase7.java

The stream escapes through field = new Outer(is). That may or may not be a great API design, but it is not the same as dropping a local resource on the floor.

Reproduction code

NegCase3.java
// A FileInputStream wrapped in a custom closeable with empty close should not be flagged as a leak.
package scensct.core.neg;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;

public class NegCase3 {
    // Custom wrapper with empty close method.
    static class NoCloseWrapper extends InputStream {
        private final InputStream inner;
        NoCloseWrapper(InputStream in) { this.inner = in; }
        @Override public int read() throws IOException { return inner.read(); }
        @Override public void close() throws IOException { inner.close(); } // Now delegates closing.
    }

    public void test() throws IOException {
        // Scenario 3: Transitive closeable parent satisfies noNeedToClose.
        new NoCloseWrapper(new FileInputStream("test.txt")); // Inner resource managed by wrapper. // [REPORTED LINE]
    }
}
NegCase7.java
// A FileInputStream passed to a locally initialized outer constructor and escaping should not be flagged as a leak.
package scensct.core.neg;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;

public class NegCase7 {
    static class Outer {
        private final InputStream inner;
        Outer(InputStream in) { this.inner = in; } // No exceptions declared.
    }

    private Outer field;

    public void test() throws IOException {
        // Scenario 7: Resource not assigned, passed to constructor, escapes via field.
        InputStream is = new FileInputStream("test.txt"); // [REPORTED LINE]
        field = new Outer(is); // Resource escapes, no leak.
    }
}

Cause analysis

These results suggest two over-approximations in Likely Bugs/Resource Leaks/CloseReader.ql.

First, the query is not reliably recognizing wrapper classes that take responsibility for the underlying resource. Second, it is treating ownership transfer as if it were equivalent to local abandonment. Both behaviors inflate the result set with cases that developers will not read as direct leaks in the current method.

主要语言
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 摘要。