This is an automated email from the ASF dual-hosted git repository.

remm pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/main by this push:
     new 1c8a00224c Make sure temporary files are deleted
1c8a00224c is described below

commit 1c8a00224cf4282d623d1694a01e0e4a7ab09d84
Author: remm <r...@apache.org>
AuthorDate: Wed Feb 26 19:34:47 2025 +0100

    Make sure temporary files are deleted
    
    The API forces catching exceptions to delete the temporary file.
    Also log a warning if delete fails.
---
 java/org/apache/catalina/servlets/DefaultServlet.java     | 11 ++++++++++-
 java/org/apache/catalina/servlets/LocalStrings.properties |  1 +
 webapps/docs/changelog.xml                                |  4 ++++
 3 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/java/org/apache/catalina/servlets/DefaultServlet.java 
b/java/org/apache/catalina/servlets/DefaultServlet.java
index 0a6a4f0a1e..0e4a33e50c 100644
--- a/java/org/apache/catalina/servlets/DefaultServlet.java
+++ b/java/org/apache/catalina/servlets/DefaultServlet.java
@@ -637,7 +637,9 @@ public class DefaultServlet extends HttpServlet {
                 }
             }
             if (tempContentFile != null) {
-                tempContentFile.delete();
+                if (!tempContentFile.delete()) {
+                    log(sm.getString("defaultServlet.deleteTempFileFailed", 
tempContentFile.getAbsolutePath()));
+                }
             }
         }
     }
@@ -692,6 +694,13 @@ public class DefaultServlet extends HttpServlet {
                     randAccessContentFile.write(transferBuffer, 0, 
numBytesRead);
                 }
             }
+
+        } catch (IOException | RuntimeException | Error e) {
+            // This has to be done this way to be able to close the file 
without changing the method signature
+            if (!contentFile.delete()) {
+                log(sm.getString("defaultServlet.deleteTempFileFailed", 
contentFile.getAbsolutePath()));
+            }
+            throw e;
         }
 
         return contentFile;
diff --git a/java/org/apache/catalina/servlets/LocalStrings.properties 
b/java/org/apache/catalina/servlets/LocalStrings.properties
index 6f172cbbc8..c09a862802 100644
--- a/java/org/apache/catalina/servlets/LocalStrings.properties
+++ b/java/org/apache/catalina/servlets/LocalStrings.properties
@@ -40,6 +40,7 @@ cgiServlet.runStdErrFail=I/O problems with stderr
 defaultServlet.blockExternalEntity=Blocked access to external entity with 
publicId [{0}] and systemId [{0}]
 defaultServlet.blockExternalEntity2=Blocked access to external entity with 
name [{0}], publicId [{1}], baseURI [{2}] and systemId [{3}]
 defaultServlet.blockExternalSubset=Blocked access to external subset with name 
[{0}] and baseURI [{1}]
+defaultServlet.deleteTempFileFailed=Failed to delete temporary file [{0}] used 
for partial PUT
 defaultServlet.directory.parent=Up To [{0}]
 defaultServlet.directory.title=Directory Listing For [{0}]
 defaultServlet.globalXSLTTooBig=The global XSLT file [{0}] is too big to buffer
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 03895193ce..ce102da757 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -166,6 +166,10 @@
         <code>ExtendedAccessLogValve</code>. Based on pull request <pr>814</pr>
         by Dmole. (markt)
       </add>
+      <fix>
+        Avoid scenarios where temporary files used for partial PUT would not
+        be deleted. (remm)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Coyote">


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to