【问题标题】:Fortify security issue "Unreleased resource stream" for try-with-resource强化 try-with-resource 的安全问题“未发布的资源流”
【发布时间】:2019-05-24 09:25:00
【问题描述】:

强化安全运行不合规代码

public static A read(String path) throws IOException, ClassNotFoundException {
    try (ObjectInputStream os = new ObjectInputStream(new GZIPInputStream(new FileInputStream(path)))) {
        return (A) os.readObject();
    }
}

上面写着 "Unreleased Resource: Streams" ,但它在 try-with-resource 里面,那么可能是什么问题?请帮帮我。

【问题讨论】:

  • 仅供参考:如果您返回在“try-with-resources”中声明的资源,它将在返回时关闭。这意味着如果您返回资源,它将为空。不相信我?试试看。

标签: java file-io fortify objectinputstream


【解决方案1】:

您的工具可能担心的问题是如果GZIPInputStreamObjectInputStream 在实例化期间抛出异常,那么FileInputStream 将不会关闭。您可以尝试以下方法:

public static A read(String path) throws IOException, ClassNotFoundException {
    try (FileInputStream fileInput = new FileInputStream(path);
         GZIPInputStream gzipInput = new GZIPInputStream(fileInput);
         ObjectInputStream objectInput = new ObjectInputStream(gzipInput)) {
        return (A) objectInput.readObject();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-13
    • 2011-06-10
    • 1970-01-01
    • 1970-01-01
    • 2016-08-18
    • 2015-08-19
    • 2019-04-05
    相关资源
    最近更新 更多