Java Null 取消引用垃圾回收(来自 Fortify scan)



我的Java有点生疏...特别是在垃圾收集方面,所以我可以使用一点帮助来找到解决方案,以解决如何不正确地以"null"结尾:

public void copyFileBuffered(String inPUT, String outPUT) throws
FileNotFoundException, IOException {
InputStream is = null;
OutputStream os = null;
try{
if(inPUT != null)
is = new FileInputStream(inPUT);
if(outPUT != null)
os = new FileOutputStream(outPUT);
int count = 0;
byte b[] = new byte[BLKSIZ];
while ((count = is.read(b)) != -1) {  /** FORTIFY ERROR LINE */
os.write(b, 0, count);
}
}
catch(Exception e){}
finally{
if(is != null)
is.close();
if(os != null)
os.close();
}
}

从我所看到的,它是由"os = new FileOutputStream(outPUT(;"处理的 FORTIFY:方法copyFileBuffered((正在取消引用空指针。

你的 if 语句是逐字和明确的,陈述以下内容:

  • 如果inPUTnull,则isnull
  • 如果outPUTnull,则osnull

静态分析会告诉你的代码你可能正在取消引用null,这将是一个重大的错误。

你没有告诉我们这些是从哪里来的,所以如果你遇到一个实际的NullPointerException,请确保inPUToutPUT实际上都没有作为null传递到这个代码块。

最新更新