Skip to content

MaxSymbolPixels default (4 Mpx) refuses real scanned PDFs; symbols up to 8 Mpx occur in the wild #1

Open

Description

@tannevaled

Thank you for this decoder — it is the only pure-Go JBIG2 I found that is correct, and I want to report that measurement before the two problems, because the problems are small next to it.

The measurement

I extracted 403 JBIG2 streams from /Mask and /SMask entries of real scanned PDFs (public Internet Archive scans: biodiversity, medical heritage, and digitised books) and decoded each with four pure-Go JBIG2 decoders. Each result was compared bit for bit against poppler's own decoder — using pdfimages to extract rather than pdftoppm to render, so that a rasteriser's resampling could not be mistaken for a decoding difference.

decoder decoded bit-exact with poppler
gobig2 396 / 403 396 (100% of what it decoded)
B 399 / 403 37 (9%)
C 157 / 403 157 (generic region only)
D 0 / 403 —

The ranking by what decodes is the opposite of the ranking by what is right. gobig2 is the only one that never decoded a stream wrongly.

Problem 1: the MaxSymbolPixels default refuses real documents

All 7 refusals are the same ceiling, in the most common configuration (SDHUFF=false SDREFAGG=false SDTEMPLATE=0):

parseSymbolDict: decode (SDHUFF=false SDREFAGG=false SDTEMPLATE=0, 1015 new / 1015 exported):
symbol bitmap exceeds MaxSymbolPixels: resource budget exceeded

The doc comment says "Real glyphs are tens of pixels/side; default 4 megapixels = two orders beyond legit". In this corpus that is not so — some encoders emit a page-sized region as a single symbol:

ceiling streams decoded (of the 7)
4 Mpx (default) 0
5 Mpx 1
6 Mpx 2
7 Mpx 4
8 Mpx 7

At 8 Mpx it is 403 of 403, every one bit-exact with poppler. poppler decodes all 7 at its own defaults.

The seven come from four public Internet Archive documents, whose mask pages are 2381x4115 to 3825x4950 — so the offending symbol is roughly half a page:

CAT31349454.pdf              page 4   2402x3711
bulletin07081984east.pdf     page 1   2828x4104
bulletin33virg.pdf           page 1   3825x4950
bulletindelasoci4243soci.pdf page 1   2381x4115   (4 streams)

I am happy to attach the extracted streams if that is useful.

Refusing is clearly better than decoding wrongly, and I would rather the default stayed conservative than became permissive. But 4 Mpx is below what real scanners emit, which makes it a correctness ceiling rather than a hardening one.

Problem 2: Limits.Apply() is process-global, so a library cannot use it

Apply() writes package-level variables in the internal packages:

func (l Limits) Apply() {
	page.MaxImagePixels = l.MaxImagePixels
	symbol.MaxSymbolPixels = l.MaxSymbolPixels
	...
}

This makes the limits unusable from a library. A PDF renderer that raises MaxSymbolPixels to read scanned documents also raises it for every other consumer of gobig2 in the same binary — including one that deliberately lowered it for hardening — and concurrent calls race.

So I ship at the defaults and lose those 7 of 403 rather than change state I do not own. Per-decode limits, for example an option on NewDecoderEmbedded or a field on Decoder, would let a consumer make this choice for itself.

A smaller note: no tagged version

The module resolves to a pseudo-version (v0.0.0-20260513123937-51e39052fde6). A tag would let dependents pin and upgrade normally, and would let automated dependency updates see the module at all.

Two behaviours I checked and think are right

  • Globals that are not parseable do not stop a stream that does not read them. Of the 403 streams, none named a JBIG2Globals stream, so this is the common path and refusing there would lose pictures that decode. I have a regression test asserting it.
  • Refusing rather than guessing is the right call throughout. The decoder that "handled" the most streams in my comparison was wrong on 91% of them, and nothing in its output said so.

Activity

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions