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

pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 36838f8892 Release the temp file when ChunkedCipherOutputStream 
construction fails (#1250)
36838f8892 is described below

commit 36838f8892fbdf1058560bba7d08b36b15561163
Author: PJ Fanning <[email protected]>
AuthorDate: Mon Sep 7 13:15:25 2026 +0100

    Release the temp file when ChunkedCipherOutputStream construction fails 
(#1250)
    
    ChunkedCipherOutputStream(DirectoryNode, int) creates a temp file and
    opens an OutputStream on it, then calls the abstract
    initCipherForBlock(null, 0, false), which is declared throws IOException,
    GeneralSecurityException. If that throws - a bad key, an unavailable JCE
    algorithm - the constructor never completes, so the caller has no
    instance to close and both the open stream and the temp file are leaked.
    
    Wrap the initCipherForBlock call so the stream is closed and the temp
    file deleted before the original exception is rethrown. The field
    assignments are left in place rather than reordered, so subclass
    initCipherForBlock implementations still see exactly the initialisation
    state they see today.
    
    The other constructor, ChunkedCipherOutputStream(OutputStream, int), is
    unaffected: it owns no temp file and wraps a stream the caller supplied.
    
    Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
---
 .../apache/poi/poifs/crypt/ChunkedCipherOutputStream.java    | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git 
a/poi/src/main/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java 
b/poi/src/main/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java
index a5b6bee158..f8aef3b8f6 100644
--- 
a/poi/src/main/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java
+++ 
b/poi/src/main/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java
@@ -74,7 +74,17 @@ public abstract class ChunkedCipherOutputStream extends 
FilterOutputStream {
         this.fileOut = TempFile.createTempFile("encrypted_package", "crypt");
         this.out = Files.newOutputStream(fileOut.toPath());
         this.dir = dir;
-        this.cipher = initCipherForBlock(null, 0, false);
+        try {
+            this.cipher = initCipherForBlock(null, 0, false);
+        } catch (IOException | GeneralSecurityException | RuntimeException e) {
+            // the constructor did not complete, so the caller has no instance 
to close -
+            // release the stream and the temp file here instead of leaking 
both
+            IOUtils.closeQuietly(this.out);
+            if (!fileOut.delete()) {
+                LOG.atWarn().log("Can't delete temp file: {}", fileOut);
+            }
+            throw e;
+        }
     }
 
     public ChunkedCipherOutputStream(OutputStream stream, int chunkSize) 
throws IOException, GeneralSecurityException {


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to