【问题标题】:Fortify - Unreleased Resource Streams issue for having a ternary in try-with-resourcesFortify - 在 try-with-resources 中有一个三元组的未发布资源流问题
【发布时间】:2021-05-10 17:49:21
【问题描述】:

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

这里是上下文的 sn-p。

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

/// Do Something

}

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

假设 Fortify 不识别 try-with-resources 范例,我没有使用它,而是按常规方式进行。

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

//Do Something.

} finally {
    assert in != null;
    try {
      in.close();
    } catch (IOException ioe) {
      throw new IllegalStateException("Could not close input stream.", ioe);
    }
}

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

【问题讨论】:

  • Fortify 非常全面,但远非完美。我猜三元运算符会混淆它。也许像 private InputStream open(String location) 这样的私有方法不太可能混淆 Fortify。

标签: java fortify


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    • 2017-11-18
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-13
    相关资源
    最近更新 更多