commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject DO NOT REPLY [Bug 21221] New: - DefaultFileItem.write() throws NullPointerException if a stream cannot be created
Date Tue, 01 Jul 2003 12:39:25 GMT
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=21221>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=21221

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

           Summary: DefaultFileItem.write() throws NullPointerException if a
                    stream cannot be created
           Product: Commons
           Version: 1.0 Final
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: Normal
          Priority: Other
         Component: File Upload
        AssignedTo: commons-dev@jakarta.apache.org
        ReportedBy: peter.chase@globalgraphics.com


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 $

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message