PyMuPDF icon indicating copy to clipboard operation
PyMuPDF copied to clipboard

find_tables() segfaults in get_layout() on Chrome-printed tagged page (/StructParents 0) — PyMuPDF 2.0/2.1

Open alexsukhrin opened this issue 3 weeks ago • 2 comments

Description of the bug

page.find_tables() (default use_layout=True) crashes the interpreter with a segmentation fault on PyMuPDF 2.0 / 2.1. The crash happens inside page.get_layout() → page_get_textpage, i.e. the layout module now used by default by find_tables. The same file works on 1.28.2.

Follow-up to #5188 as requested there; same root cause as analysed by @JorjMcKie (/StructParents 0 on the page — setting it to null avoids the crash), MuPDF Bugzilla 709841.

Attached structparents_segfault.pdf is a Chrome-printed page (Skia/PDF, tagged: /StructParents 0, /Tabs /S, /StructTreeRoot) reduced to a minimum: all text was redacted except a single glyph U+EDDB from an embedded SegoeFluentIcons subset. With that one glyph left the crash still reproduces; with it redacted too, it does not.

In production this affects a very common class of documents for us: PDFs printed from a web portal in Chrome (90 of 92 documents that crashed our workers).

How to reproduce the bug

import pymupdf
print(pymupdf.VersionBind)
page = pymupdf.open("structparents_segfault.pdf")[0]
print(len(page.find_tables().tables))

Result:

  • 2.1 / 2.0: Fatal Python error: Segmentation fault (exit code 139), 3 of 3 runs
  • 1.28.2: prints 1

Workarounds: page.find_tables(use_layout=False) on 2.x, or pin PyMuPDF<2.

PyMuPDF version

2.1

Operating system

MacOS arm64 (the original unreduced documents also crash on Linux x86_64, Docker)

Python version

3.12

alexsukhrin avatar Nov 06 '26 08:11 alexsukhrin

Thanks for the preparation! This is an upstream bug, and I'll report it to MuPDF's issue system. Link to MuPDF issue: https://bugs.ghostscript.com/show_bug.cgi?id=709968

JorjMcKie avatar Nov 07 '26 10:11 JorjMcKie

[!NOTE] 🤖 Claude Opus 5.5 responding on behalf of @JoshuaKeegan3

Another reproduction of this, on Linux x86_64 with a non-Chrome (Adobe Acrobat) tagged PDF. /StructParents is the trigger here too, so it looks like the same MuPDF bug (709841).

File: Transpower's public FINAL 2025 Transmission Planning Report (Adobe Acrobat 25.1, tagged, about 21 MB, 414 pages), from https://www.transpower.co.nz/resources sha256 3ad6a9b63ab9692a35cb276f9316000f3dea1261675d2b0038f5896489f5eb29

Versions: PyMuPDF 2.1 / MuPDF 1.28.6 with pymupdf_layout 2.1, CPython 3.12.12, Arch Linux x86_64.

import pymupdf, pymupdf.layout
doc = pymupdf.open("transmission-planning-report-2025.pdf")
doc[52].get_layout()   # SIGSEGV, every run

Results, each in a separate process:

Case Result
doc[52].get_layout() (page has /StructParents 210) segfault (139)
Same, after doc.xref_set_key(page.xref, "StructParents", "null") ok
Page 52 copied into a new doc via insert_pdf (drops the struct tree) ok
doc[52].find_tables(use_layout=False) ok
get_text() on all 414 pages ok
PyMuPDF 1.28.2: doc[52].find_tables() ok, 5 tables

Native backtrace from the core (MuPDF has no debug symbols, so frame 0 is unnamed):

#0  libmupdf.so.28.6 + 0x25a009        mov %rdx,0x40(%rax)   ; rax == 0
#1  fz_table_hunt_within_bounds ()
#2  fz_close_device ()
#3  page_get_textpage(mupdf::FzPage&, _object*, int, _object*) ()  [_extra.so]
#4  _wrap_page_get_textpage ()

si_code is SEGV_MAPERR at address 0x40: the code loads a pointer field at +0x38 of some struct, gets NULL, then writes +0x40 through it, inside table hunting during fz_close_device. The offset was identical across 3 crashes. My guess is that the structure-tree-aware stext path leaves a node's parent/struct pointer NULL, and table hunting doesn't check for it. That part is inference from the disassembly; I haven't verified it against the source.

JoshuaKeegan3 avatar Nov 22 '26 03:11 JoshuaKeegan3