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

BUG: Prevent recursive loop in some PDF files #2505

Merged
merged 6 commits into from
Mar 16, 2024
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
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
Loading