Fortify - 在 try-with-resources 中有一个三元的未发布资源流问题

问题描述

我最近使用 Fortify 扫描了我们的代码库,我很困惑为什么它会标记某些问题。我面临的一个问题是发布资源。

这是上下文的片段。

sa_handler

Fortify 抱怨具有此 try-with-resources 块的方法无法释放 FileInputStream() 分配的系统资源。 try-with-resources 不是帮我自动关闭 FileInputStream 吗?

假设 Fortify 不识别 try-with-resources 范式,我就避免使用它并以常规方式使用它。

 String someLocation = getPathToTheLocation(); //gives location
 try (InputStream in = someLocation == null ? Thread.currentThread().getContextClassLoader()
                .getResourceAsstream("someFile.xml") : new FileInputStream(new File(someLocation))) {

/// Do Something

}

但它仍然抱怨资源未被释放。我无法识别的实际问题是什么?

解决方法

我认为代码一切正常,Fortify 中的问题,也许你应该为它提出一个问题。 Idea 也有同样的问题 - https://stackoverflow.com/a/34655863/5790043