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 0eec26224f Close file-backed resources when workbook/extractor 
construction fails (#1248)
0eec26224f is described below

commit 0eec26224f2c5e7cf15e73fa0907ce22f8f54fd5
Author: PJ Fanning <[email protected]>
AuthorDate: Mon Sep 7 13:07:14 2026 +0100

    Close file-backed resources when workbook/extractor construction fails 
(#1248)
    
    Three factory methods opened a file-backed resource, then only cleaned it
    up for a subset of the exceptions the following call can actually throw,
    leaking the underlying file handle for the rest.
    
    HSSFWorkbookFactory.create(File, String, boolean) caught only
    RuntimeException, but HSSFWorkbook(POIFSFileSystem, boolean, char[]) is
    declared throws IOException, so a corrupt .xls that fails with an
    IOException leaked the RandomAccessFile behind the POIFSFileSystem.
    
    XSSFWorkbookFactory.createWorkbook(OPCPackage) had the same gap: it
    caught only RuntimeException while new XSSFWorkbook(pkg) throws
    IOException. Reached from create(File, String, boolean), the package is
    ZipFile-backed, so the zip handle leaked.
    
    POIXMLExtractorFactory.create(File, String) handled InvalidFormatException
    and IOException but not RuntimeException. The InputStream overload
    directly below it already had that arm, and it is the File variant that
    actually holds a descriptor.
    
    Each is widened to a multi-catch that reverts/closes and rethrows the
    original exception unchanged. ExtractorFactory.createExtractor(File,
    String) already used this catch (IOException | RuntimeException) shape.
    
    Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
---
 .../java/org/apache/poi/ooxml/extractor/POIXMLExtractorFactory.java     | 2 +-
 .../main/java/org/apache/poi/xssf/usermodel/XSSFWorkbookFactory.java    | 2 +-
 .../main/java/org/apache/poi/hssf/usermodel/HSSFWorkbookFactory.java    | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git 
a/poi-ooxml/src/main/java/org/apache/poi/ooxml/extractor/POIXMLExtractorFactory.java
 
b/poi-ooxml/src/main/java/org/apache/poi/ooxml/extractor/POIXMLExtractorFactory.java
index 7aef57bf55..54e6461ecd 100644
--- 
a/poi-ooxml/src/main/java/org/apache/poi/ooxml/extractor/POIXMLExtractorFactory.java
+++ 
b/poi-ooxml/src/main/java/org/apache/poi/ooxml/extractor/POIXMLExtractorFactory.java
@@ -157,7 +157,7 @@ public final class POIXMLExtractorFactory implements 
ExtractorProvider {
             return ex;
         } catch (InvalidFormatException ife) {
             throw new IOException(ife);
-        } catch (IOException e) {
+        } catch (IOException | RuntimeException e) {
             if (pkg != null) {
                 pkg.revert();
             }
diff --git 
a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbookFactory.java
 
b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbookFactory.java
index 8d49b1eaf9..bad38be305 100644
--- 
a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbookFactory.java
+++ 
b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbookFactory.java
@@ -116,7 +116,7 @@ public class XSSFWorkbookFactory implements 
WorkbookProvider {
     public static XSSFWorkbook createWorkbook(OPCPackage pkg) throws 
IOException {
         try {
             return new XSSFWorkbook(pkg);
-        } catch (RuntimeException ioe) {
+        } catch (IOException | RuntimeException ioe) {
             // ensure that file handles are closed (use revert() to not 
re-write the file)
             pkg.revert();
             //pkg.close();
diff --git 
a/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFWorkbookFactory.java 
b/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFWorkbookFactory.java
index 5dd5585130..4f2dd813aa 100644
--- a/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFWorkbookFactory.java
+++ b/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFWorkbookFactory.java
@@ -91,7 +91,7 @@ public class HSSFWorkbookFactory implements WorkbookProvider {
         try {
             final char[] passwordChars = password == null ? null : 
password.toCharArray();
             return new HSSFWorkbook(fs, true, passwordChars);
-        } catch (RuntimeException e) {
+        } catch (IOException | RuntimeException e) {
             // we need to close the filesystem if we encounter an exception to 
not leak file handles
             fs.close();
             throw e;


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

Reply via email to