Skip to content

Commit d3ec939

Browse files
committed
Guard for empty FileItems in CommonsFileUploadSupport
This commit ensures that a FileItem is not empty before its value is read. Closes gh-31564
1 parent de0cb53 commit d3ec939

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

Diff for: spring-web/src/main/java/org/springframework/web/multipart/commons/CommonsFileUploadSupport.java

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -255,16 +255,21 @@ protected MultipartParsingResult parseFileItems(List<FileItem> fileItems, String
255255
for (FileItem fileItem : fileItems) {
256256
if (fileItem.isFormField()) {
257257
String value;
258-
String partEncoding = determineEncoding(fileItem.getContentType(), encoding);
259-
try {
260-
value = fileItem.getString(partEncoding);
261-
}
262-
catch (UnsupportedEncodingException ex) {
263-
if (logger.isWarnEnabled()) {
264-
logger.warn("Could not decode multipart item '" + fileItem.getFieldName() +
265-
"' with encoding '" + partEncoding + "': using platform default");
258+
if (fileItem.getSize() > 0) {
259+
String partEncoding = determineEncoding(fileItem.getContentType(), encoding);
260+
try {
261+
value = fileItem.getString(partEncoding);
262+
}
263+
catch (UnsupportedEncodingException ex) {
264+
if (logger.isWarnEnabled()) {
265+
logger.warn("Could not decode multipart item '" + fileItem.getFieldName() +
266+
"' with encoding '" + partEncoding + "': using platform default");
267+
}
268+
value = fileItem.getString();
266269
}
267-
value = fileItem.getString();
270+
}
271+
else {
272+
value = "";
268273
}
269274
String[] curParam = multipartParameters.get(fileItem.getFieldName());
270275
if (curParam == null) {

0 commit comments

Comments
 (0)