diff options
author | PJ Fanning <fanningpj@apache.org> | 2021-12-18 18:04:52 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2021-12-18 18:04:52 +0000 |
commit | b88cb5620adc7dd7a1d152c7472486383a17d9bb (patch) | |
tree | b0a960d905fd97be5ba9cc0c3bd71474b43473a3 | |
parent | f4bb4560edd288e6916d222b76caa8c9aa97baf7 (diff) | |
download | poi-b88cb5620adc7dd7a1d152c7472486383a17d9bb.tar.gz poi-b88cb5620adc7dd7a1d152c7472486383a17d9bb.zip |
try to close resources when RuntimeExceptions happen
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1896140 13f79535-47bb-0310-9956-ffa450edef68
3 files changed, 32 insertions, 22 deletions
diff --git a/poi-ooxml/src/main/java/org/apache/poi/ooxml/POIXMLDocumentPart.java b/poi-ooxml/src/main/java/org/apache/poi/ooxml/POIXMLDocumentPart.java index d676373cca..3d289ebab7 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/ooxml/POIXMLDocumentPart.java +++ b/poi-ooxml/src/main/java/org/apache/poi/ooxml/POIXMLDocumentPart.java @@ -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) { + IOUtils.closeQuietly(pkg); + throw new POIXMLException("OOXML file structure broken/invalid - core document '" + coreRel.getTargetURI() + "' not found."); + } + return pp; + } - if (coreRel != null) { - PackagePart pp = pkg.getPart(coreRel); - if (pp == null) { + 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!"); } } diff --git a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/ZipPackage.java b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/ZipPackage.java index 8feaf81252..af1a4c07b1 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/ZipPackage.java +++ b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/ZipPackage.java @@ -130,7 +130,7 @@ public final class ZipPackage extends OPCPackage { 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; } diff --git a/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/temp/AesZipFileZipEntrySource.java b/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/temp/AesZipFileZipEntrySource.java index f38be63ce4..7582d323a0 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/temp/AesZipFileZipEntrySource.java +++ b/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/temp/AesZipFileZipEntrySource.java @@ -105,14 +105,17 @@ public final class AesZipFileZipEntrySource implements ZipEntrySource { } 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 { |