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

False Negative: CloseWriter.ql misses leaked wrapped streams when the close is skipped by an early return or hidden in a factory.

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

还没有人认领这个 Issue。

评估

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

调研方向

首先阅读 Likely Bugs/Resource Leaks/CloseWriter.ql,并检查 PosCase2_Var3.java 和 PosCase2_Var5.java 中受影响的用例。跟踪查询如何处理提前返回和由 helper 创建的流,然后运行相关的 CloseWriter 测试。完成的标准是两个用例都被报告为泄漏,且不破坏现有覆盖率。

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

描述

question

Version
codeql 2.24.3

Checker

  • Checker id: Likely Bugs/Resource Leaks/CloseWriter.ql
  • Checker description: This checker detects instances where a Writer or OutputStream is created but not guaranteed to be closed on method exit, potentially causing resource leaks.

Description of the false negative

Both examples still leak the underlying file stream. One exits the method before the wrapper is closed. The other moves stream construction into a helper and then never closes the returned wrapper.

Those are ordinary resource-leak scenarios. Neither refactoring changes the ownership or lifetime of the stream.

Affected test cases

PosCase2_Var3.java

The return bypasses the only close() call. bos and the wrapped FileOutputStream both remain unclosed on the live path.

// FileOutputStream passed to BufferedOutputStream where inner resource not guaranteed to be closed should be flagged.
package scensct.var.pos;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class PosCase2_Var3 {
    public void writeBuffered() throws IOException {
        // Variant 3: Introduce early return that skips close
        FileOutputStream fos = new FileOutputStream("data.bin");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        bos.write(1);
        if (System.currentTimeMillis() > 0) {
            return; // exit without closing
        }
        // Unreachable code, close never called
        bos.close();
    }
}
PosCase2_Var5.java

The factory method only hides allocation. It does not transfer cleanup responsibility anywhere else, and the caller still never closes the returned stream.

// FileOutputStream passed to BufferedOutputStream where inner resource not guaranteed to be closed should be flagged.
package scensct.var.pos;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class PosCase2_Var5 {
    public void writeBuffered() throws IOException {
        // Variant 5: Wrap in a method that returns the BufferedOutputStream without closing
        BufferedOutputStream bos = openBufferedStream("data.bin");
        bos.write(1);
        // Not closed
    }
    
    private BufferedOutputStream openBufferedStream(String file) throws IOException {
        FileOutputStream fos = new FileOutputStream(file);
        return new BufferedOutputStream(fos);
    }
}

Cause analysis

Likely Bugs/Resource Leaks/CloseWriter.ql looks too dependent on one direct construction-and-close pattern. As soon as the leak is expressed through an early return or a helper that returns the wrapper, the result disappears.

That is too brittle for a resource-leak query. In real code, stream creation is often factored into small helpers, and missing close calls frequently happen on short-circuit exits.

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

更多 Security Issue

把新 issue 发到你的邮箱

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