首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Fortify - try-with-resources中存在三元组的未释放资源流问题

Fortify - try-with-resources中存在三元组的未释放资源流问题
EN

Stack Overflow用户
提问于 2021-05-11 01:49:22
回答 1查看 136关注 0票数 2

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

下面是上下文的一个片段。

代码语言:javascript
复制
 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范式,我没有使用它,而是以常规的方式使用它。

代码语言:javascript
复制
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);
    }
}

然而,它仍然抱怨资源没有被释放。我没有意识到的实际问题是什么?

EN

回答 1

Stack Overflow用户

发布于 2021-05-11 02:02:24

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67475344

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档