Skip to content

Commit c83691f

Browse files
committed
PDFBOX-6153: catch cycles
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1931540 13f79535-47bb-0310-9956-ffa450edef68
1 parent 34e3b2b commit c83691f

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,9 +603,15 @@ public void appendDocument(PDDocument destination, PDDocument source) throws IOE
603603
else
604604
{
605605
// search last sibling for dest, because /Last entry is sometimes wrong
606+
Set<COSDictionary> visited = new HashSet<>();
606607
PDOutlineItem destLastOutlineItem = destOutline.getFirstChild();
607608
while (true)
608609
{
610+
if (!visited.add(destLastOutlineItem.getCOSObject()))
611+
{
612+
LOG.warn("Outline ignored: " + destLastOutlineItem.getCOSObject());
613+
break; // Cycle detected
614+
}
609615
PDOutlineItem outlineItem = destLastOutlineItem.getNextSibling();
610616
if (outlineItem == null)
611617
{

pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/documentnavigation/outline/PDOutlineItemIterator.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
*/
1717
package org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline;
1818

19+
import java.util.HashSet;
1920
import java.util.Iterator;
2021
import java.util.NoSuchElementException;
22+
import java.util.Set;
23+
import org.apache.pdfbox.cos.COSDictionary;
2124

2225
/**
2326
* Iterator over the linked list of {@link PDOutlineItem} siblings.
@@ -29,6 +32,7 @@ class PDOutlineItemIterator implements Iterator<PDOutlineItem>
2932
{
3033
private PDOutlineItem currentItem;
3134
private final PDOutlineItem startingItem;
35+
private final Set<COSDictionary> visited = new HashSet<>();
3236

3337
PDOutlineItemIterator(PDOutlineItem startingItem)
3438
{
@@ -47,7 +51,7 @@ public boolean hasNext()
4751
return true;
4852
}
4953
PDOutlineItem sibling = currentItem.getNextSibling();
50-
return sibling != null && !startingItem.equals(sibling);
54+
return sibling != null && !visited.contains(sibling.getCOSObject());
5155
}
5256

5357
@Override
@@ -65,6 +69,7 @@ public PDOutlineItem next()
6569
{
6670
currentItem = currentItem.getNextSibling();
6771
}
72+
visited.add(currentItem.getCOSObject());
6873
return currentItem;
6974
}
7075

0 commit comments

Comments
 (0)