Skip to content
Open
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
6 changes: 4 additions & 2 deletions pypdf/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3035,7 +3035,8 @@ def _insert_filtered_annotations(
if str(d) in self.get_named_dest_root():
outlist.append(ano.clone(self).indirect_reference)
else:
d = cast("ArrayObject", d)
if not isinstance(d, ArrayObject) or not d:
continue
p = self._get_cloned_page(d[0], pages, reader)
if p is not None:
anc = ano.clone(self, ignore_fields=("/Dest",))
Expand All @@ -3050,7 +3051,8 @@ def _insert_filtered_annotations(
if str(d) in self.get_named_dest_root():
outlist.append(ano.clone(self).indirect_reference)
else:
d = cast("ArrayObject", d)
if not isinstance(d, ArrayObject) or not d:
continue
p = self._get_cloned_page(d[0], pages, reader)
if p is not None:
anc = ano.clone(self, ignore_fields=("/D",))
Expand Down
4 changes: 3 additions & 1 deletion pypdf/generic/_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ def __init__(self, reference: ArrayObject) -> None:
"""reference: an ArrayObject whose first element is the Page indirect object"""
self._reference = reference

def find_referenced_page(self) -> IndirectObject:
def find_referenced_page(self) -> Union[IndirectObject, None]:
if not self._reference:
return None
return cast(IndirectObject, self._reference[0])

def patch_reference(self, target_pdf: "PdfWriter", new_page: IndirectObject) -> None:
Expand Down
7 changes: 7 additions & 0 deletions tests/generic/test_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,10 @@ def test_extract_links_ignores_uri_annotation_offsets(caplog: pytest.LogCaptureF
assert isinstance(links[0][0], DirectReferenceLink)
assert isinstance(links[0][1], DirectReferenceLink)
assert caplog.messages == []


def test_direct_reference_link_empty_reference() -> None:
# An empty destination array has no target page to resolve.
assert DirectReferenceLink(ArrayObject([])).find_referenced_page() is None
# A populated array still resolves to its first element.
assert DirectReferenceLink(ArrayObject([NumberObject(7)])).find_referenced_page() == 7
46 changes: 46 additions & 0 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,52 @@ def test_link_annotation(pdf_file_path):
writer.write(output_stream)


@pytest.mark.parametrize(
"annotation",
[
# Empty /Dest array.
DictionaryObject({

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide meaningful IDs.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added ids: empty-dest-array, empty-goto-action-dest, non-array-dest.

NameObject("/Subtype"): NameObject("/Link"),
NameObject("/Dest"): ArrayObject([]),
}),
# Empty /GoTo action destination.
DictionaryObject({
NameObject("/Subtype"): NameObject("/Link"),
NameObject("/A"): DictionaryObject({
NameObject("/S"): NameObject("/GoTo"),
NameObject("/D"): ArrayObject([]),
}),
}),
# /Dest that is not an array at all.
DictionaryObject({
NameObject("/Subtype"): NameObject("/Link"),
NameObject("/Dest"): NumberObject(5),
}),
],
ids=["empty-dest-array", "empty-goto-action-dest", "non-array-dest"],
)
def test_malformed_link_destination_does_not_crash_transfer(annotation):
"""A link with an empty or non-array destination must not abort page transfer."""
writer = PdfWriter()
writer.add_blank_page(width=72, height=72)
writer.pages[0][NameObject("/Annots")] = ArrayObject([writer._add_object(annotation)])
stream = BytesIO()
writer.write(stream)
stream.seek(0)
reader = PdfReader(stream)

# append() routes the annotation through _insert_filtered_annotations.
appended = PdfWriter()
appended.append(reader)
appended.write(BytesIO())

# add_page() + write() routes it through extract_links()/_resolve_links().
added = PdfWriter()
for page in reader.pages:
added.add_page(page)
added.write(BytesIO())


def test_io_streams():
"""This is the example from the docs ("Streaming data")."""
filepath = RESOURCE_ROOT / "pdflatex-outline.pdf"
Expand Down
Loading