Java Null Dereference垃圾收集(来自Fortify扫描)

时间:2018-05-04 16:15:04

标签: java null dereference garbage

我的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()方法取消引用空指针。

1 个答案:

答案 0 :(得分:1)

您的if语句在说明以下内容时是逐字明确的:

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

静态分析会告诉您的代码可能解除引用null,这将是一个重大错误。

您没有告诉我们这些来自哪里,所以如果您遇到了NullPointerException,请确保inPUToutPUT实际上都没有通过null这段代码。