Skip to content

Commit 2dc08b0

Browse files
committed
PDFBOX-6097: avoid StackOverflow as proposed by Tilman
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1929678 13f79535-47bb-0310-9956-ffa450edef68
1 parent ff2ac47 commit 2dc08b0

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

pdfbox/src/main/java/org/apache/pdfbox/cos/COSDictionary.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public class COSDictionary extends COSBase implements COSUpdateInfo
5151

5252
private static final String PATH_SEPARATOR = "/";
5353

54+
private static final List<COSName> PARENT_KEYS = Arrays.asList(COSName.PARENT, COSName.P);
55+
5456
/**
5557
* The name-value pairs of this dictionary. The pairs are kept in the order they were added to the dictionary.
5658
*/
@@ -1457,7 +1459,7 @@ public void getIndirectObjectKeys(Collection<COSObjectKey> indirectObjects)
14571459
COSBase cosBase = entry.getValue();
14581460
COSObjectKey cosBaseKey = cosBase != null ? cosBase.getKey() : null;
14591461
// avoid endless recursions
1460-
if (COSName.PARENT.equals(entry.getKey())
1462+
if (PARENT_KEYS.contains(entry.getKey())
14611463
|| (cosBaseKey != null && indirectObjects.contains(cosBaseKey)))
14621464
{
14631465
continue;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.pdfbox.pdmodel;
18+
19+
import java.io.ByteArrayOutputStream;
20+
import java.io.IOException;
21+
22+
import org.apache.pdfbox.pdmodel.common.PDRectangle;
23+
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;
24+
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
25+
import org.apache.pdfbox.pdmodel.interactive.form.PDTextField;
26+
27+
import org.junit.jupiter.api.Test;
28+
29+
/**
30+
* Test case introduced with PDFBOX-6097
31+
*
32+
*/
33+
class TestPDPage
34+
{
35+
@Test
36+
void testAddingPageAfterCreatingAnnotation() throws IOException
37+
{
38+
try (PDDocument document = new PDDocument())
39+
{
40+
PDPage page = new PDPage(PDRectangle.A4);
41+
// Create AcroForm
42+
PDAcroForm acroForm = new PDAcroForm(document);
43+
document.getDocumentCatalog().setAcroForm(acroForm);
44+
45+
// Create a single text field
46+
PDTextField textField = new PDTextField(acroForm);
47+
textField.setPartialName("testField");
48+
PDAnnotationWidget widget = textField.getWidgets().get(0);
49+
widget.setRectangle(new PDRectangle(100, 700, 200, 20));
50+
widget.setPage(page);
51+
page.getAnnotations().add(widget);
52+
acroForm.getFields().add(textField);
53+
54+
// Adding page AFTER creating form fields causes a StackOverflowError
55+
document.addPage(page);
56+
57+
document.save(new ByteArrayOutputStream());
58+
document.close();
59+
}
60+
}
61+
62+
}

0 commit comments

Comments
 (0)