Skip to content

Commit

Permalink
BUG: infinite recursion caused by IndirectObject clone
Browse files Browse the repository at this point in the history
if a object contains a indirect_reference, which points to the object it self,
cloning it will cause infinite recursion.
for example: a page contains a link to self.
  • Loading branch information
exiledkingcc committed Sep 6, 2023
1 parent 05f2a65 commit 54ac2b7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pypdf/generic/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,11 @@ def clone(
if id(self.pdf) not in pdf_dest._id_translated:
pdf_dest._id_translated[id(self.pdf)] = {}

if not force_duplicate and self.idnum in pdf_dest._id_translated[id(self.pdf)]:
if self.idnum in pdf_dest._id_translated[id(self.pdf)]:
dup = pdf_dest.get_object(pdf_dest._id_translated[id(self.pdf)][self.idnum])
if force_duplicate:
idref: IndirectObject = dup.indirect_reference
return IndirectObject(idref.idnum, idref.generation, idref.pdf)
else:
obj = self.get_object()
# case observed : a pointed object can not be found
Expand Down
12 changes: 12 additions & 0 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1783,3 +1783,15 @@ def test_viewerpreferences():
assert reader.viewer_preferences is None
writer = PdfWriter(clone_from=reader)
assert writer.viewer_preferences is None


@pytest.mark.enable_socket()
def test_object_contains_indirect_reference_to_self():
url = "https://github.com/py-pdf/pypdf/files/12389243/testbook.pdf"
name = "iss2102.pdf"
reader = PdfReader(BytesIO(get_data_from_url(url, name=name)))
writer = PdfWriter()
width, height = 595, 841
outpage = writer.add_blank_page(width, height)
outpage.merge_page(reader.pages[6])
writer.append(reader)

0 comments on commit 54ac2b7

Please sign in to comment.