Issue Details (XML | Word | Printable)

Key: FILEUPLOAD-21
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Unassigned
Reporter: Peter Chase
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Commons FileUpload

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

Created: 01/Jul/03 07:39 PM   Updated: 09/Mar/07 08:31 PM
Return to search
Component/s: None
Affects Version/s: 1.0 Final
Fix Version/s: None

Time Tracking:
Not Specified

Environment:
Operating System: All
Platform: All

Bugzilla Id: 21221


 Description  « Hide
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 $


 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Martin Cooper added a comment - 02/Jul/03 11:45 AM
Fixed in 20030702 nightly build.

Martin Cooper added a comment - 15/Aug/03 10:42 AM
      • COM-759 has been marked as a duplicate of this bug. ***