Bug 62592

Summary: StackOverflow on standalone ThemeManager files in 4.0.0-SNAPSHOT
Product: POI Reporter: Tim Allison <tallison>
Component: OPCAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: blocker    
Priority: P2    
Version: 4.0.x-dev   
Target Milestone: ---   
Hardware: PC   
OS: All   
Attachments: triggering document
First draft patch

Description Tim Allison 2018-08-01 16:36:55 UTC
Created attachment 36064 [details]
triggering document

In running 4.0.0-SNAPSHOT against our regression set, I found a SO exception triggered by corrupt files.

Code:
OPCPackage.open(p.toFile());

With 4.0.0-SNAPSHOT, I get:
java.lang.StackOverflowError
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at java.base/jdk.internal.loader.URLClassPath$JarLoader.ensureOpen(URLClassPath.java:741)
	at java.base/jdk.internal.loader.URLClassPath$JarLoader.<init>(URLClassPath.java:716)
	at java.base/jdk.internal.loader.URLClassPath$JarLoader$3.run(URLClassPath.java:968)
	at java.base/jdk.internal.loader.URLClassPath$JarLoader$3.run(URLClassPath.java:966)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at java.base/jdk.internal.loader.URLClassPath$JarLoader.getResource(URLClassPath.java:965)
	at java.base/jdk.internal.loader.URLClassPath$JarLoader.getResource(URLClassPath.java:926)
	at java.base/jdk.internal.loader.URLClassPath$JarLoader.findResource(URLClassPath.java:901)
	at java.base/jdk.internal.loader.URLClassPath.findResource(URLClassPath.java:282)
	at java.base/jdk.internal.loader.BuiltinClassLoader.findResourceOnClassPath(BuiltinClassLoader.java:479)
	at java.base/jdk.internal.loader.BuiltinClassLoader.findResource(BuiltinClassLoader.java:303)
	at java.base/java.lang.ClassLoader.getResource(ClassLoader.java:1400)
	at java.base/java.lang.ClassLoader.getResourceAsStream(ClassLoader.java:1736)
	at org.apache.xerces.parsers.SecuritySupport$6.run(Unknown Source)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at org.apache.xerces.parsers.SecuritySupport.getResourceAsStream(Unknown Source)
	at org.apache.xerces.parsers.ObjectFactory.findJarServiceProvider(Unknown Source)
	at org.apache.xerces.parsers.ObjectFactory.createObject(Unknown Source)
	at org.apache.xerces.parsers.ObjectFactory.createObject(Unknown Source)
	at org.apache.xerces.parsers.DOMParser.<init>(Unknown Source)
	at org.apache.xerces.parsers.DOMParser.<init>(Unknown Source)
	at org.apache.xerces.jaxp.DocumentBuilderImpl.<init>(Unknown Source)
	at org.apache.xerces.jaxp.DocumentBuilderFactoryImpl.newDocumentBuilder(Unknown Source)
	at org.apache.poi.ooxml.util.DocumentHelper.newDocumentBuilder(DocumentHelper.java:89)
	at org.apache.poi.ooxml.util.DocumentHelper.readDocument(DocumentHelper.java:147)
...


With POI 3.17, we get:

org.apache.poi.openxml4j.exceptions.InvalidFormatException: The part /_rels/.rels does not have any content type ! Rule: Package require content types when retrieving a part from a package. [M.1.14]

	at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:341)
	at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:756)
	at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:298)
	at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:201)
	at
Comment 1 Tim Allison 2018-08-01 16:37:45 UTC
Sorry, SO _error_...of course...
Comment 2 Tim Allison 2018-08-01 16:41:09 UTC
And w Java 8:

java.lang.StackOverflowError
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.misc.URLClassPath$JarLoader.ensureOpen(URLClassPath.java:868)
	at sun.misc.URLClassPath$JarLoader.<init>(URLClassPath.java:841)
	at sun.misc.URLClassPath$JarLoader$3.run(URLClassPath.java:1094)
	at sun.misc.URLClassPath$JarLoader$3.run(URLClassPath.java:1091)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.misc.URLClassPath$JarLoader.getResource(URLClassPath.java:1090)
	at sun.misc.URLClassPath$JarLoader.getResource(URLClassPath.java:1050)
	at sun.misc.URLClassPath$JarLoader.findResource(URLClassPath.java:1020)
	at sun.misc.URLClassPath.findResource(URLClassPath.java:215)
	at java.net.URLClassLoader$2.run(URLClassLoader.java:569)
	at java.net.URLClassLoader$2.run(URLClassLoader.java:567)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findResource(URLClassLoader.java:566)
	at java.lang.ClassLoader.getResource(ClassLoader.java:1096)
	at java.net.URLClassLoader.getResourceAsStream(URLClassLoader.java:232)
	at org.apache.xerces.parsers.SecuritySupport$6.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at org.apache.xerces.parsers.SecuritySupport.getResourceAsStream(Unknown Source)
	at org.apache.xerces.parsers.ObjectFactory.findJarServiceProvider(Unknown Source)
	at org.apache.xerces.parsers.ObjectFactory.createObject(Unknown Source)
	at org.apache.xerces.parsers.ObjectFactory.createObject(Unknown Source)
	at org.apache.xerces.parsers.DOMParser.<init>(Unknown Source)
	at org.apache.xerces.parsers.DOMParser.<init>(Unknown Source)
	at org.apache.xerces.jaxp.DocumentBuilderImpl.<init>(Unknown Source)
	at org.apache.xerces.jaxp.DocumentBuilderFactoryImpl.newDocumentBuilder(Unknown Source)
	at org.apache.poi.ooxml.util.DocumentHelper.newDocumentBuilder(DocumentHelper.java:89)
	at org.apache.poi.ooxml.util.DocumentHelper.readDocument(DocumentHelper.java:147)
	at org.apache.poi.openxml4j.opc.internal.ContentTypeManager.parseContentTypesFile(ContentTypeManager.java:375)
	at org.apache.poi.openxml4j.opc.internal.ContentTypeManager.<init>(ContentTypeManager.java:100)
	at org.apache.poi.openxml4j.opc.internal.ZipContentTypeManager.<init>(ZipContentTypeManager.java:54)
	at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:255)
	at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:725)
	at org.apache.poi.openxml4j.opc.OPCPackage.getPart(OPCPackage.java:603)
	at org.apache.poi.openxml4j.opc.internal.ContentTypeManager.getContentType(ContentTypeManager.java:339)
	at org.apache.poi.openxml4j.opc.ZipPackage$EntryTriple.<init>(ZipPackage.java:317)
	at org.apache.poi.openxml4j.opc.ZipPackage.lambda$getPartsImpl$0(ZipPackage.java:284)
	at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
	at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
	at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
	at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
	at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
	at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
	at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:287)
	at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:725)
	at org.apache.poi.openxml4j.opc.OPCPackage.getPart(OPCPackage.java:603)
	at org.apache.poi.openxml4j.opc.internal.ContentTypeManager.getContentType(ContentTypeManager.java:339)
	at org.apache.poi.openxml4j.opc.ZipPackage$EntryTriple.<init>(ZipPackage.java:317)
	at org.apache.poi.openxml4j.opc.ZipPackage.lambda$getPartsImpl$0(ZipPackage.java:284)
	at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
Comment 3 Tim Allison 2018-08-01 16:47:44 UTC
Sorry, the files are not corrupt zip files. I can open them with commons-compress and WinZip...they appear to contain only ThemeManager info...any idea what these are?
Comment 4 Tim Allison 2018-08-02 14:43:00 UTC
Created attachment 36069 [details]
First draft patch

Not sure I like this patch, any recs for a better solution?
Comment 5 PJ Fanning 2018-08-08 11:57:02 UTC
The patch seems ok to me as is.
But one alternative would be just to have a boolean member to keep track of whether the ContentTypeManager has already been created. This could be extended to a map to track other items that we are worried could lead to recursive stack overflows.
Comment 6 Tim Allison 2018-08-08 16:06:58 UTC
r1837658

Thank you, PJ! We can change if needed.