PyMuPDF icon indicating copy to clipboard operation
PyMuPDF copied to clipboard

`Document.set_toc_item()` crashes on point-like tuple/list coordinates (`AttributeError`) and inverts `dest_dict['to']` via in-place mutation

Open codeCraft-Ritik opened this issue 1 month ago • 0 comments

Description of the bug


Description of the bug *

Document.set_toc_item() accepts a dest_dict parameter (documented as the destination dictionary created by get_toc(False)).

When dest_dict["kind"] == LINK_GOTO:

7510:             if dest_dict["kind"] == LINK_GOTO:
7511:                 pno = dest_dict["page"]
7512:                 page_xref = doc.page_xref(pno)
7513:                 page_height = doc.page_cropbox(pno).height
7514:                 to = dest_dict.get('to', Point(72, 36))
7515:                 to.y = page_height - to.y
7516:                 dest_dict["to"] = to

Two defects occur here:

  1. Crash on point-like tuple or list: In PyMuPDF, coordinates are standardly accepted as "point-like" sequences (tuple, list, or Point). If a user passes dest_dict={'kind': 1, 'page': 0, 'to': (100, 200)}, line 7515 attempts to.y = page_height - to.y. Because tuple and list do not have a .y attribute, Python raises AttributeError: 'tuple' object has no attribute 'y'. (Notice that when to is passed as an explicit keyword argument on lines 7552–7554, to = Point(to) is correctly used).
  2. In-place destructive coordinate corruption: When dest_dict["to"] is a Point (which get_toc(False) produces), to.y = page_height - to.y directly mutates the caller's Point instance and dictionary. If set_toc_item() is called again with the same dictionary, or if the caller reuses dest_dict, the y-coordinate is inverted back and forth repeatedly (642.0 -> 200.0 -> 642.0).

How to reproduce the bug

Highlight in Code

src/__init__.py:7510-7517:

7510:             if dest_dict["kind"] == LINK_GOTO:
7511:                 pno = dest_dict["page"]
7512:                 page_xref = doc.page_xref(pno)
7513:                 page_height = doc.page_cropbox(pno).height
7514:                 to = dest_dict.get('to', Point(72, 36))
7515: >>>>>           to.y = page_height - to.y    # BUG: Fails on tuple/list, mutates Point in-place!
7516:                 dest_dict["to"] = to
7517:             action = utils.getDestStr(page_xref, dest_dict)

Steps & Reproduction Code

import pymupdf

doc = pymupdf.open()
doc.new_page()
doc.set_toc([[1, "Chapter 1", 1]])

# Issue A: Crash on point-like tuple
try:
    doc.set_toc_item(0, dest_dict={"kind": pymupdf.LINK_GOTO, "page": 0, "to": (100, 200)})
except AttributeError as e:
    print("Crash confirmed:", e)
    # Output: AttributeError: 'tuple' object has no attribute 'y'

# Issue B: In-place coordinate corruption
p = pymupdf.Point(100, 200)
dest_dict = {"kind": pymupdf.LINK_GOTO, "page": 0, "to": p}
doc.set_toc_item(0, dest_dict=dest_dict)
print("After call 1, dest_dict['to'].y =", dest_dict["to"].y)  # 642.0
doc.set_toc_item(0, dest_dict=dest_dict)
print("After call 2, dest_dict['to'].y =", dest_dict["to"].y)  # 200.0 (inverted back!)

Expected Behavior

set_toc_item() should convert to using to = Point(dest_dict.get('to', (72, 36))), avoid mutating the caller's input dictionary or Point object in-place, and accept point-like tuples and lists without raising AttributeError.

Actual Behavior

Crashes with AttributeError: 'tuple' object has no attribute 'y' on tuples/lists, and inverts dest_dict["to"].y in-place when given a Point.

Suggested Fix

--- a/src/__init__.py
+++ b/src/__init__.py
@@ -7511,9 +7511,10 @@ class Document:
                 pno = dest_dict["page"]
                 page_xref = doc.page_xref(pno)
                 page_height = doc.page_cropbox(pno).height
-                to = dest_dict.get('to', Point(72, 36))
+                to = Point(dest_dict.get('to', (72, 36)))
                 to.y = page_height - to.y
-                dest_dict["to"] = to
+                dest_dict = dest_dict.copy()
+                dest_dict["to"] = to
             action = utils.getDestStr(page_xref, dest_dict)
             if not action.startswith("/A"):
                 raise ValueError("bad bookmark dest")

PyMuPDF version

2.1

Operating system

Windows

Python version

3.14

codeCraft-Ritik avatar Nov 02 '26 05:11 codeCraft-Ritik