【发布时间】:2019-09-07 20:23:19
【问题描述】:
说明: 在下面的代码中我在下面的代码中做错了什么,在我的代码审计中,我得到了不正确的资源关闭或释放。 我尝试从下面的代码中取出关闭和刷新
File someFile = new File(fileName);
fos = new FileOutputStream(someFile);
fos.write(data);
fos.flush();
fos.close();
主要代码:
FileOutputStream fos=null;
try {
Hashtable hash = responseBlob.getAllAttachments();
Enumeration e = hash.elements();
while (e.hasMoreElements()) {
SBADataAttach tmpAttach = (SBADataAttach) e.nextElement();
String tag = tmpAttach.getTag();
byte[] data = tmpAttach.getData();
// encode compressed file
if (hasTag(tag))
mimeResponse.addPart(tag, Base64.encodeBytes(data)
.getBytes());
else
mimeResponse.addPart(tag, data);
try {
if (this.configData.getBlobPath() != null)
{
// save compressed file
String fileName = File.separatorChar + "tmp"
+ File.separatorChar + tag;
Log.theLogger.debug("XisServlet.process() ... "
+ "Save compressed file = " + fileName);
File someFile = new File(fileName);
fos = new FileOutputStream(
someFile);
fos.write(data);
fos.flush();
fos.close();
}
} catch (Exception zipe) {
Log.theLogger.error(zipe.getMessage(), zipe);
}
}
} catch (SBADataException sde) {
// cannot detach files from blob
Log.theLogger.error(sde.getMessage(), sde);
}
finally {
try {
if( fos!=null ) {
fos.close();
}
} catch(IOException e) {
Log.theLogger.error(e.getMessage(), e);
}
}
我没有收到任何错误。但在 Appsec 发现中,我得到了不正确的资源关闭或释放
【问题讨论】:
-
如果
new FileOutputStream(someFile);和fos.close()之间的某些代码引发异常,您的代码可能不会关闭fos。 -
我真的需要 fos.close();正如我在做 fos.close 一样,最后也终于 { try { if( fos!=null ) { fos.close(); } } catch(IOException e) { Log.theLogger.error(e.getMessage(), e); } }
-
使用 try-with-resource 语句。
-
我已将代码更改为尝试{ //打开资源 File someFile = new File(fileName); fos = new FileOutputStream(someFile); fos.write(数据); } catch(Exception e1){ //处理异常 }finally{ //关闭资源 fos.flush(); fos.close(); } }