Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Safely exit infinite loops on AProfile.outlinesOK / checkItemOutline #704

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ public final class AProfile extends PdfProfile
* PRIVATE CLASS FIELDS.
******************************************************************/

/*
* Map of visited nodes when walking through an outline.- used to stop
* infinite loops. These may be caused by mishandled escape characters
* when loading objects.
*/
protected Set<Integer> _visitedOutlineNodes;

/* TaggedProfile to which this profile is linked. */
private TaggedProfile _taggedProfile;
private boolean _levelA;
Expand Down Expand Up @@ -822,6 +829,7 @@ private boolean checkUncalIntent ()
we save the time to do this test. */
private boolean outlinesOK ()
{
_visitedOutlineNodes = new HashSet<Integer>();
if (!_module.getActionsExist ()) {
return true;
}
Expand All @@ -833,11 +841,16 @@ private boolean outlinesOK ()
PdfDictionary item = (PdfDictionary) _module.resolveIndirectObject
(outlineDict.get ("First"));
while (item != null) {
_visitedOutlineNodes.add(item.getObjNumber());
if (!checkOutlineItem (item)) {
return false;
}
item = (PdfDictionary) _module.resolveIndirectObject
PdfDictionary next = (PdfDictionary) _module.resolveIndirectObject
(((PdfDictionary) item).get ("Next"));
if (_visitedOutlineNodes.contains(next.getObjNumber())) {
throw new PdfInvalidException(MessageConstants.PDF_HUL_129);
}
item = next;
}
}
catch (Exception e) {
Expand All @@ -861,12 +874,17 @@ private boolean checkOutlineItem (PdfDictionary item)
_module.resolveIndirectObject (item.get ("First"));
PdfDictionary next;
while (child != null) {
_visitedOutlineNodes.add(child.getObjNumber());
if (!checkOutlineItem (child)) {
return false;
}
next = (PdfDictionary)
_module.resolveIndirectObject (child.get ("Next"));
if (next.getObjNumber() != child.getObjNumber()) {
Integer nextObjNum = next.getObjNumber();
if (nextObjNum != child.getObjNumber()) {
if (_visitedOutlineNodes.contains(nextObjNum)) {
throw new PdfInvalidException(MessageConstants.PDF_HUL_129);
}
child = next;
} else {
child = null;
Expand Down