Author: fanningpj
Date: Sat Dec 18 18:04:52 2021
New Revision: 1896140

URL: http://svn.apache.org/viewvc?rev=1896140&view=rev
Log:
try to close resources when RuntimeExceptions happen

Modified:
    
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/ooxml/POIXMLDocumentPart.java
    
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/ZipPackage.java
    
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/temp/AesZipFileZipEntrySource.java

Modified: 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/ooxml/POIXMLDocumentPart.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/main/java/org/apache/poi/ooxml/POIXMLDocumentPart.java?rev=1896140&r1=1896139&r2=1896140&view=diff
==============================================================================
--- 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/ooxml/POIXMLDocumentPart.java 
(original)
+++ 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/ooxml/POIXMLDocumentPart.java 
Sat Dec 18 18:04:52 2021
@@ -739,24 +739,31 @@ public class POIXMLDocumentPart {
      * Since POI 4.1.2 - pkg is closed if this method throws an exception
      */
     private static PackagePart getPartFromOPCPackage(OPCPackage pkg, String 
coreDocumentRel) {
-        PackageRelationship coreRel = 
pkg.getRelationshipsByType(coreDocumentRel).getRelationship(0);
+        try {
+            PackageRelationship coreRel = 
pkg.getRelationshipsByType(coreDocumentRel).getRelationship(0);
 
-        if (coreRel != null) {
-            PackagePart pp = pkg.getPart(coreRel);
-            if (pp == null) {
+            if (coreRel != null) {
+                PackagePart pp = pkg.getPart(coreRel);
+                if (pp == null) {
+                    IOUtils.closeQuietly(pkg);
+                    throw new POIXMLException("OOXML file structure 
broken/invalid - core document '" + coreRel.getTargetURI() + "' not found.");
+                }
+                return pp;
+            }
+
+            coreRel = 
pkg.getRelationshipsByType(PackageRelationshipTypes.STRICT_CORE_DOCUMENT).getRelationship(0);
+            if (coreRel != null) {
                 IOUtils.closeQuietly(pkg);
-                throw new POIXMLException("OOXML file structure broken/invalid 
- core document '" + coreRel.getTargetURI() + "' not found.");
+                throw new POIXMLException("Strict OOXML isn't currently 
supported, please see bug #57699");
             }
-            return pp;
-        }
 
-        coreRel = 
pkg.getRelationshipsByType(PackageRelationshipTypes.STRICT_CORE_DOCUMENT).getRelationship(0);
-        if (coreRel != null) {
             IOUtils.closeQuietly(pkg);
-            throw new POIXMLException("Strict OOXML isn't currently supported, 
please see bug #57699");
+            throw new POIXMLException("OOXML file structure broken/invalid - 
no core document found!");
+        } catch (POIXMLException e) {
+            throw e;
+        } catch (RuntimeException e) {
+            IOUtils.closeQuietly(pkg);
+            throw new POIXMLException("OOXML file structure broken/invalid", 
e);
         }
-
-        IOUtils.closeQuietly(pkg);
-        throw new POIXMLException("OOXML file structure broken/invalid - no 
core document found!");
     }
 }

Modified: 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/ZipPackage.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/ZipPackage.java?rev=1896140&r1=1896139&r2=1896140&view=diff
==============================================================================
--- 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/ZipPackage.java 
(original)
+++ 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/ZipPackage.java 
Sat Dec 18 18:04:52 2021
@@ -130,7 +130,7 @@ public final class ZipPackage extends OP
         ZipArchiveThresholdInputStream zis = ZipHelper.openZipStream(in); // 
NOSONAR
         try {
             this.zipArchive = new ZipInputStreamZipEntrySource(zis);
-        } catch (final IOException e) {
+        } catch (final IOException | RuntimeException e) {
             IOUtils.closeQuietly(zis);
             throw e;
         }

Modified: 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/temp/AesZipFileZipEntrySource.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/temp/AesZipFileZipEntrySource.java?rev=1896140&r1=1896139&r2=1896140&view=diff
==============================================================================
--- 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/temp/AesZipFileZipEntrySource.java
 (original)
+++ 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/temp/AesZipFileZipEntrySource.java
 Sat Dec 18 18:04:52 2021
@@ -105,14 +105,17 @@ public final class AesZipFileZipEntrySou
     }
 
     public static AesZipFileZipEntrySource createZipEntrySource(InputStream 
is) throws IOException {
-        // generate session key
-        byte[] ivBytes = new byte[16], keyBytes = new byte[16];
-        RandomSingleton.getInstance().nextBytes(ivBytes);
-        RandomSingleton.getInstance().nextBytes(keyBytes);
-        final File tmpFile = TempFile.createTempFile("protectedXlsx", ".zip");
-        copyToFile(is, tmpFile, keyBytes, ivBytes);
-        IOUtils.closeQuietly(is);
-        return fileToSource(tmpFile, keyBytes, ivBytes);
+        try {
+            // generate session key
+            byte[] ivBytes = new byte[16], keyBytes = new byte[16];
+            RandomSingleton.getInstance().nextBytes(ivBytes);
+            RandomSingleton.getInstance().nextBytes(keyBytes);
+            final File tmpFile = TempFile.createTempFile("protectedXlsx", 
".zip");
+            copyToFile(is, tmpFile, keyBytes, ivBytes);
+            return fileToSource(tmpFile, keyBytes, ivBytes);
+        } finally {
+            IOUtils.closeQuietly(is);
+        }
     }
 
     private static void copyToFile(InputStream is, File tmpFile, byte[] 
keyBytes, byte[] ivBytes) throws IOException {



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

Reply via email to