PyMuPDF
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
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:
- Crash on point-like tuple or list: In PyMuPDF, coordinates are standardly accepted as "point-like" sequences (
tuple,list, orPoint). If a user passesdest_dict={'kind': 1, 'page': 0, 'to': (100, 200)}, line 7515 attemptsto.y = page_height - to.y. Becausetupleandlistdo not have a.yattribute, Python raisesAttributeError: 'tuple' object has no attribute 'y'. (Notice that whentois passed as an explicit keyword argument on lines 7552–7554,to = Point(to)is correctly used). - In-place destructive coordinate corruption: When
dest_dict["to"]is aPoint(whichget_toc(False)produces),to.y = page_height - to.ydirectly mutates the caller's Point instance and dictionary. Ifset_toc_item()is called again with the same dictionary, or if the caller reusesdest_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