From d3ec9395e17e4a2e357be6dc7c90cc4f00cc1bf9 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Wed, 8 Nov 2023 15:31:52 +0100 Subject: [PATCH] Guard for empty FileItems in CommonsFileUploadSupport This commit ensures that a FileItem is not empty before its value is read. Closes gh-31564 --- .../commons/CommonsFileUploadSupport.java | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/multipart/commons/CommonsFileUploadSupport.java b/spring-web/src/main/java/org/springframework/web/multipart/commons/CommonsFileUploadSupport.java index 99fcee5f8c..3b0f0ec85b 100644 --- a/spring-web/src/main/java/org/springframework/web/multipart/commons/CommonsFileUploadSupport.java +++ b/spring-web/src/main/java/org/springframework/web/multipart/commons/CommonsFileUploadSupport.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -255,16 +255,21 @@ public abstract class CommonsFileUploadSupport { for (FileItem fileItem : fileItems) { if (fileItem.isFormField()) { String value; - String partEncoding = determineEncoding(fileItem.getContentType(), encoding); - try { - value = fileItem.getString(partEncoding); - } - catch (UnsupportedEncodingException ex) { - if (logger.isWarnEnabled()) { - logger.warn("Could not decode multipart item '" + fileItem.getFieldName() + - "' with encoding '" + partEncoding + "': using platform default"); + if (fileItem.getSize() > 0) { + String partEncoding = determineEncoding(fileItem.getContentType(), encoding); + try { + value = fileItem.getString(partEncoding); } - value = fileItem.getString(); + catch (UnsupportedEncodingException ex) { + if (logger.isWarnEnabled()) { + logger.warn("Could not decode multipart item '" + fileItem.getFieldName() + + "' with encoding '" + partEncoding + "': using platform default"); + } + value = fileItem.getString(); + } + } + else { + value = ""; } String[] curParam = multipartParameters.get(fileItem.getFieldName()); if (curParam == null) {