Skip to content

delete_page() raises a SWIG TypeError when another page has an invalid UTF-16 GoTo destination #5270

Open

Description

@jczeal

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

  1. added
    postpone
    on Nov 12, 2026
  2. JorjMcKie commented on Nov 12, 2026

    @JorjMcKie
    Collaborator

    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.

  3. julian-smith-artifex-com commented on Dec 2, 2026

    @julian-smith-artifex-com
    Collaborator

    With destination='FEFFD800' we end up trying to pass the string \udced\udca0\udc80 to MuPDF, but this string cannot be represented as utf8, so the Swig-generated code returns an error, which in turn results in the rather misleading TypeError ... message. (Swig-generated wrappers always convert python strings into utf8-encoded C strings.)

    I don't yet know where the \udced\udca0\udc80 string comes from.

  4. julian-smith-artifex-com commented on Dec 2, 2026

    @julian-smith-artifex-com
    Collaborator

    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 use errors='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.]

  5. added 2 commits that reference this issue on Dec 2, 2026
    89aeffa
    25b5d21
  6. jczeal commented on Dec 3, 2026

    @jczeal
    Author

    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.

  7. added a commit that references this issue on Dec 3, 2026
    8622b27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Fixed in next releasebugpostpone

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions