Bernhard Fey created PDFBOX-6036:
------------------------------------

             Summary: StackOverflowError in COSWriterCompressionPool for large 
number of bookmarks
                 Key: PDFBOX-6036
                 URL: https://issues.apache.org/jira/browse/PDFBOX-6036
             Project: PDFBox
          Issue Type: Bug
    Affects Versions: 3.0.5 PDFBox
            Reporter: Bernhard Fey


Saving a document containing thousands of bookmarks causes a 
{{StackOverflowError}} in {{{}COSWriterCompressionPool{}}}.
 
The stack trace alternates between the following two methods:
 
 
{code:java}
at 
org.apache.pdfbox.pdfwriter.compress.COSWriterCompressionPool.addStructure(COSWriterCompressionPool.java:190)
at 
org.apache.pdfbox.pdfwriter.compress.COSWriterCompressionPool.addElements(COSWriterCompressionPool.java:204)
 {code}
 
 
You can replicate the issue with the following class:
 
 
{code:java}
public class StackOverflBookm {
    public static void main(String[] args) {
        for (int i = 1; i <= 1_111_111; i *= 2) {
            System.out.println(new java.text.DecimalFormat("#,###").format(i));
            try (org.apache.pdfbox.pdmodel.PDDocument document = new 
org.apache.pdfbox.pdmodel.PDDocument()) {
                
org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline
 outline =
                        new 
org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline();
 
                document.getDocumentCatalog().setDocumentOutline(outline);
                for (int j = 0; j < i; j++) {
                    outline.addLast(new 
org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem());
                }
                document.save(new java.io.ByteArrayOutputStream(),
                        
org.apache.pdfbox.pdfwriter.compress.CompressParameters.DEFAULT_COMPRESSION); 
// NO_COMPRESSION avoids the Error
            } catch (Throwable e) {
                e.printStackTrace(System.out);
                return;
            }
        }
    }
}{code}
 
 
Without compression it will create documents with up to over a million 
bookmarks (assuming sufficient heap size, 1 gigabyte is enough).
But with the default compression the StackOverflowError will be thrown (on my 
Windows VMs, with a default stack size of about 1 megabyte, before reaching ten 
thousand bookmarks).
 
Apparently the recursion depth of the two methods grows with the amount of 
bookmarks, until the Error is thrown.
 
I have chosen the priority Critical, because an {{Error}} is thrown and, unlike 
Exceptions, likely not handled in integrations.
Additionally, most integrations likely use the {{save}} methods without the 
second parameter, getting compression enabled by default.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@pdfbox.apache.org
For additional commands, e-mail: dev-h...@pdfbox.apache.org

Reply via email to