FILEUPLOAD-250 FileUploadBase - potential resource leak - InputStream not closed on exception
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/fileupload/trunk@1568866 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 794d7c4..2ecdb53 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -45,6 +45,7 @@
<body>
<release version="1.4" date="TBA" description=
"TBA">
+ <action issue="FILEUPLOAD-250" dev="sebb" type="fix">FileUploadBase - potential resource leak - InputStream not closed on exception</action>
<action issue="FILEUPLOAD-244" dev="sebb" type="fix">DiskFileItem.readObject fails to close FileInputStream</action>
<action issue="FILEUPLOAD-246" dev="sebb" type="update">FileUpload should use IOUtils.closeQuietly where relevant</action>
<action issue="FILEUPLOAD-245" dev="sebb" type="fix">DiskFileItem.get() may not fully read the data</action>
diff --git a/src/main/java/org/apache/commons/fileupload/FileUploadBase.java b/src/main/java/org/apache/commons/fileupload/FileUploadBase.java
index d0a4caf..34aa7bf 100644
--- a/src/main/java/org/apache/commons/fileupload/FileUploadBase.java
+++ b/src/main/java/org/apache/commons/fileupload/FileUploadBase.java
@@ -38,6 +38,7 @@
import org.apache.commons.fileupload.util.FileItemHeadersImpl;
import org.apache.commons.fileupload.util.LimitedInputStream;
import org.apache.commons.fileupload.util.Streams;
+import org.apache.commons.io.IOUtils;
/**
* <p>High level API for processing file uploads.</p>
@@ -949,7 +950,6 @@
MULTIPART_FORM_DATA, MULTIPART_MIXED, contentType));
}
- InputStream input = ctx.getInputStream();
@SuppressWarnings("deprecation") // still has to be backward compatible
final int contentLengthInt = ctx.getContentLength();
@@ -960,6 +960,7 @@
: contentLengthInt;
// CHECKSTYLE:ON
+ InputStream input; // N.B. this is eventually closed in MultipartStream processing
if (sizeMax >= 0) {
if (requestSize != -1 && requestSize > sizeMax) {
throw new SizeLimitExceededException(
@@ -967,7 +968,8 @@
Long.valueOf(requestSize), Long.valueOf(sizeMax)),
requestSize, sizeMax);
}
- input = new LimitedInputStream(input, sizeMax) {
+ // N.B. this is eventually closed in MultipartStream processing
+ input = new LimitedInputStream(ctx.getInputStream(), sizeMax) {
@Override
protected void raiseError(long pSizeMax, long pCount)
throws IOException {
@@ -978,6 +980,8 @@
throw new FileUploadIOException(ex);
}
};
+ } else {
+ input = ctx.getInputStream();
}
String charEncoding = headerEncoding;
@@ -987,6 +991,7 @@
boundary = getBoundary(contentType);
if (boundary == null) {
+ IOUtils.closeQuietly(input); // avoid possible resource leak
throw new FileUploadException("the request was rejected because no multipart boundary was found");
}
@@ -994,6 +999,7 @@
try {
multi = new MultipartStream(input, boundary, notifier);
} catch (IllegalArgumentException iae) {
+ IOUtils.closeQuietly(input); // avoid possible resource leak
throw new InvalidContentTypeException(
format("The boundary specified in the %s header is too long", CONTENT_TYPE), iae);
}