Repository navigation
Description
Description of the bug
Deleting the middle page of a generated three-page PDF raises a Python/SWIG argument-conversion TypeError when page 0 has a GoTo action with destination <FEFFD800>.
This is a malformed-input robustness report: the string deliberately contains a UTF-16BE BOM followed by an isolated high surrogate. With <FEFF0041> (UTF-16BE A) as the control, deletion succeeds. Neither example creates a destination name tree, so both targets are unresolved; only the string encoding changes.
How to reproduce the bug
Run with PyMuPDF==2.1. The PDF and annotation are created entirely in memory.
import json
import pymupdf as pdf
def invalid_link(destination):
with pdf.open() as doc:
for number in range(3):
doc.new_page().insert_text((40, 40), f'synthetic page {number}')
xref = doc.get_new_xref()
doc.update_object(xref, f'<</Type /Annot /Subtype /Link /Rect [0 0 20 20] /A <</S /GoTo /D <{destination}>>>>>')
doc.xref_set_key(doc.page_xref(0), 'Annots', f'[{xref} 0 R]')
try:
doc.delete_page(1)
return {'destination': destination, 'pages_after': len(doc), 'error': None}
except Exception as exc:
return {'destination': destination, 'pages_after': len(doc),
'error': type(exc).__name__, 'message': str(exc)}
print(json.dumps({
'pymupdf': pdf.VersionBind,
'mupdf': pdf.VersionFitz,
'cases': [invalid_link(d) for d in ['FEFF0041', 'FEFFD800']],
}, indent=2))Observed results
<FEFF0041>: deletion succeeds, leaving 2 pages.<FEFFD800>: deletion fails, leaving 3 pages, with:
TypeError: in method 'll_fz_resolve_link_outparams_fn', argument 2 of type 'char const *'
Expected behavior
Handle the malformed destination according to the PDF parser's invalid-link policy, for example treating it as unresolvable or raising a meaningful PDF parsing exception. A SWIG C-string conversion error escapes while scanning links on another page. The failed call in this example leaves the page count unchanged.
PyMuPDF version
2.1; MuPDF 1.28.6. Checked on 2026-11-12 against the latest official release. The executable _remove_dest_range resolver code in current main at f318af5 matches the released implementation.
Related encoding reports #4329 and #4564 concern diagnostic callbacks and metadata respectively; this example isolates a GoTo link during page deletion.
Operating system
macOS arm64
Python version
3.12.10
Activity
JorjMcKie commentedon Nov 12, 2026
Thank you providing this observation. We will assign a low priority and postpone creating a fix. It may also turn out as a bug that should better be fixed in MuPDF.
julian-smith-artifex-com commentedon Dec 2, 2026
With destination='FEFFD800' we end up trying to pass the string
\udced\udca0\udc80to MuPDF, but this string cannot be represented as utf8, so the Swig-generated code returns an error, which in turn results in the rather misleadingTypeError ...message. (Swig-generated wrappers always convert python strings into utf8-encoded C strings.)I don't yet know where the
\udced\udca0\udc80string comes from.julian-smith-artifex-com commentedon Dec 2, 2026
Ok so mupdf uses surrogate escapes as necessary when decoding a pdf_obj string, hence FEFFD800 is converted to the
\udced\udca0\udc8. But Swig does not useerrors='surrogateescape'when it converts strings into a (utf8) char* in order to call C/C++. So any such surrogate escapes cause a failure when passed back into mupdf, and i don't think there's a simple way of fixing this underlying problem.[If we treat any exception from
mupdf.fz_resolve_link()as a reason to simply skip the annot, then things work.]src/__init__.py:_remove_dest_range(): don't fail if we can't find lin…
Fix #5270.
jczeal commentedon Dec 3, 2026
Thanks for tracking this down and adding the regression test in #5325. I’ll rerun the original example once the fix is available in a release.
Fix #5270.