我最近用Fortify扫描了我们的代码库,我不明白为什么它会标记出某些问题。我面临的一个问题是发布资源。
下面是上下文的一个片段。
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);
}
}然而,它仍然抱怨资源没有被释放。我没有意识到的实际问题是什么?
发布于 2021-05-11 02:02:24
我认为代码一切正常,Fortify中的问题,也许你应该为它提出一个问题。Idea - https://stackoverflow.com/a/34655863/5790043也有同样的问题。
https://stackoverflow.com/questions/67475344
复制相似问题