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: Cope with invalid objects in IndirectObject.clone #1637

Merged
merged 2 commits into from
Feb 25, 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
7 changes: 6 additions & 1 deletion pypdf/generic/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,12 @@ def clone(
dup = pdf_dest.get_object(pdf_dest._id_translated[id(self.pdf)][self.idnum])
else:
obj = self.get_object()
assert obj is not None
# case observed : a pointed object can not be found
if obj is None:
# this normally
obj = NullObject()
assert isinstance(self, (IndirectObject,))
obj.indirect_reference = self
dup = obj.clone(pdf_dest, force_duplicate, ignore_fields)
assert dup is not None
assert dup.indirect_reference is not None
Expand Down
15 changes: 15 additions & 0 deletions tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1044,3 +1044,18 @@ def test_cloning(caplog):
obj21 = obj20.clone(writer, ignore_fields=None)
assert "/Test" in obj21
assert isinstance(obj21.get("/Test2"), IndirectObject)


@pytest.mark.external
def test_append_with_indirectobject_not_pointing(caplog):
"""
reported in #1631
the object 43 0 is not invalid
"""
url = "https:/py-pdf/pypdf/files/10729142/document.pdf"
name = "tst_iss1631.pdf"
data = BytesIO(get_pdf_from_url(url, name=name))
reader = PdfReader(data, strict=False)
writer = PdfWriter()
writer.append(reader)
assert "Object 43 0 not defined." in caplog.text