Uploaded image for project: 'Commons FileUpload'
  1. Commons FileUpload
  2. FILEUPLOAD-21

DefaultFileItem.write() throws NullPointerException if a stream cannot be created

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.0 Final
    • None
    • None
    • Operating System: All
      Platform: All

    • 21221

    Description

      In class org.apache.commons.fileupload.DefaultFileItem, the write() method has
      poor error handling, resulting in a NullPointerException.

      The streams "in" and "out" are initialised to null. Then the code attempts to
      assign them to new streams. A "try...finally" attempts to ensure that these
      streams are always closed. However, if one or both streams have not been
      created (e.g. IOException), the "finally" code tries to invoke close() on a
      null reference, causing NullPointerException.

      The simple fix is to check that each stream is not null, before closing it.

      BufferedInputStream in = null;
      BufferedOutputStream out = null;
      try
      {
      in = new BufferedInputStream(new FileInputStream(outputFile));
      out = new BufferedOutputStream(new FileOutputStream(file));
      byte[] bytes = new byte[2048];
      int s = 0;
      while ((s = in.read(bytes)) != -1)

      { out.write(bytes, 0, s); }

      }
      finally
      {
      try

      { in.close(); }

      catch (IOException e)

      { // ignore }
      try
      { out.close(); }
      catch (IOException e)
      { // ignore }

      }

      Version information:

      • $Header: /home/cvs/jakarta-
        commons/fileupload/src/java/org/apache/commons/fileupload/DefaultFileItem.java,
        v 1.21 2003/06/24 05:45:15 martinc Exp $
      • $Revision: 1.21 $
      • $Date: 2003/06/24 05:45:15 $

      Attachments

        Activity

          People

            Unassigned Unassigned
            peter.chase@globalgraphics.com Peter Chase
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: