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

ROB: Prevent loop in Cloning #1770

Merged
merged 3 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions pypdf/_protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ def get_object(self, indirect_reference: Any) -> Optional[PdfObjectProtocol]:
def write(self, stream: Union[Path, StrByteType]) -> Tuple[bool, IO]:
...

def _add_object(self, obj: Any) -> Any:
...

@property
def pages(self) -> List[Any]:
...
Expand Down
5 changes: 4 additions & 1 deletion pypdf/generic/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,10 @@ def clone(
obj = NullObject()
assert isinstance(self, (IndirectObject,))
obj.indirect_reference = self
dup = obj.clone(pdf_dest, force_duplicate, ignore_fields)
dup = pdf_dest._add_object(
obj.clone(pdf_dest, force_duplicate, ignore_fields)
)
# asserts added to prevent errors in mypy
assert dup is not None
assert dup.indirect_reference is not None
return dup.indirect_reference
Expand Down
5 changes: 4 additions & 1 deletion pypdf/generic/_data_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,10 @@ def _clone(
if decoded_self is None:
self.decoded_self = None
else:
self.decoded_self = decoded_self.clone(pdf_dest, True, ignore_fields) # type: ignore[assignment]
self.decoded_self = cast(
"DecodedStreamObject",
decoded_self.clone(pdf_dest, force_duplicate, ignore_fields),
)
except Exception:
pass
super()._clone(src, pdf_dest, force_duplicate, ignore_fields)
Expand Down
11 changes: 11 additions & 0 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1292,3 +1292,14 @@ def test_iss1723():
in_pdf = PdfReader(BytesIO(get_pdf_from_url(url, name=name)))
out_pdf = PdfWriter()
out_pdf.append(in_pdf, (3, 5))


@pytest.mark.enable_socket()
def test_iss1767():
# test with a pdf which is buggy because the object 389,0 exists 3 times:
# twice to define catalog and one as an XObject inducing a loop when
# cloning
url = "https://github.com/py-pdf/pypdf/files/11138472/test.pdf"
name = "iss1723.pdf"
in_pdf = PdfReader(BytesIO(get_pdf_from_url(url, name=name)))
PdfWriter(clone_from=in_pdf)