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 327e1c8da2 Close the POIFS filesystem when MAPIMessage parsing fails 
(#1249)
327e1c8da2 is described below

commit 327e1c8da253a78bfdf07160251a52b9026dd2d9
Author: PJ Fanning <[email protected]>
AuthorDate: Mon Sep 7 13:08:11 2026 +0100

    Close the POIFS filesystem when MAPIMessage parsing fails (#1249)
    
    MAPIMessage(File) and MAPIMessage(String) open a file-backed
    POIFSFileSystem and hand it to MAPIMessage(POIFSFileSystem), which
    delegates to MAPIMessage(DirectoryNode). That constructor runs
    POIFSChunkParser.parse and the chunk sorters with no cleanup, so a
    malformed .msg file leaked the RandomAccessFile behind the filesystem.
    MAPIMessage(File) is the constructor the javadoc recommends for lower
    memory use, so this is the path most likely to be hit.
    
    MAPIMessage(POIFSFileSystem) already takes ownership of the filesystem -
    close() closes it via POIDocument.close() - so it is also the right place
    to close it when construction does not complete. The chunk parsing moves
    into a private parseChunks method, and MAPIMessage(POIFSFileSystem) now
    calls super/parseChunks directly rather than delegating, so it can wrap
    the parse in a try/catch.
    
    MAPIMessage(DirectoryNode) deliberately keeps no cleanup: it is used for
    messages embedded in a filesystem owned by someone else, so closing that
    filesystem there would be wrong.
    
    Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
---
 .../main/java/org/apache/poi/hsmf/MAPIMessage.java | 24 +++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hsmf/MAPIMessage.java 
b/poi-scratchpad/src/main/java/org/apache/poi/hsmf/MAPIMessage.java
index 248573dbaf..9b3ea18208 100644
--- a/poi-scratchpad/src/main/java/org/apache/poi/hsmf/MAPIMessage.java
+++ b/poi-scratchpad/src/main/java/org/apache/poi/hsmf/MAPIMessage.java
@@ -52,6 +52,7 @@ import org.apache.poi.hsmf.parsers.POIFSChunkParser;
 import org.apache.poi.poifs.filesystem.DirectoryNode;
 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 import org.apache.poi.util.CodePageUtil;
+import org.apache.poi.util.IOUtils;
 import org.apache.poi.util.LocaleUtil;
 import org.apache.poi.util.StringUtil;
 
@@ -153,7 +154,17 @@ public class MAPIMessage extends POIReadOnlyDocument {
      *                          input format
      */
     public MAPIMessage(POIFSFileSystem fs) throws IOException {
-        this(fs.getRoot());
+        super(fs.getRoot());
+
+        // this instance takes ownership of fs - close() closes it via the 
directory - so
+        // close it here too if parsing fails, otherwise the file handle 
behind a
+        // MAPIMessage(File) or MAPIMessage(String) would leak on a malformed 
message
+        try {
+            parseChunks(fs.getRoot());
+        } catch (IOException | RuntimeException e) {
+            IOUtils.closeQuietly(fs);
+            throw e;
+        }
     }
 
     /**
@@ -167,7 +178,18 @@ public class MAPIMessage extends POIReadOnlyDocument {
      */
     public MAPIMessage(DirectoryNode poifsDir) throws IOException {
         super(poifsDir);
+        parseChunks(poifsDir);
+    }
 
+    /**
+     * Reads the chunks out of the given directory and populates this message.
+     *
+     * <p>Split out of {@link #MAPIMessage(DirectoryNode)} so that
+     * {@link #MAPIMessage(POIFSFileSystem)} can close the filesystem it owns 
if parsing
+     * fails. Note that the {@link DirectoryNode} constructor deliberately 
does not do
+     * that - it is used for messages embedded in a filesystem owned by 
someone else.</p>
+     */
+    private void parseChunks(DirectoryNode poifsDir) throws IOException {
         // Grab all the chunks
         ChunkGroup[] chunkGroups = POIFSChunkParser.parse(poifsDir);
 


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

Reply via email to