Skip to content

Commit

Permalink
BUG: Prevent recursive loop in some PDF files (#2505)
Browse files Browse the repository at this point in the history
Closes #2474. Analysis in #2477.
  • Loading branch information
pubpub-zz authored Mar 16, 2024
1 parent 8ef399a commit 5a4c35e
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions pypdf/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1923,7 +1923,14 @@ def clean_forms(
elt: DictionaryObject, stack: List[DictionaryObject]
) -> Tuple[List[str], List[str]]:
nonlocal to_delete
if elt in stack:
# elt in recursive call is a new ContentStream object, so we have to check the indirect_reference
if (elt in stack) or (
hasattr(elt, "indirect_reference")
and any(
elt.indirect_reference == getattr(x, "indirect_reference", -1)
for x in stack
)
):
# to prevent infinite looping
return [], [] # pragma: no cover
try:
Expand Down Expand Up @@ -1958,7 +1965,12 @@ def clean_forms(
if k1 not in ["/Length", "/Filter", "/DecodeParms"]
}
)
clean_forms(content, stack + [elt]) # clean sub forms
try:
content.indirect_reference = o.indirect_reference
except AttributeError: # pragma: no cover
pass
stack.append(elt)
clean_forms(content, stack) # clean subforms
if content is not None:
if isinstance(v, IndirectObject):
self._objects[v.idnum - 1] = content
Expand Down

0 comments on commit 5a4c35e

Please sign in to comment.