Skip to content

Cap parser nesting depth to prevent stack overflow#4

Merged
pgundlach merged 1 commit into
mainfrom
claude/parser-depth-limit
Jun 19, 2026
Merged

Cap parser nesting depth to prevent stack overflow#4
pgundlach merged 1 commit into
mainfrom
claude/parser-depth-limit

Conversation

@fank

@fank fank commented Jun 18, 2026

Copy link
Copy Markdown
Collaborator

Problem

parseArray / parseDict recurse with no depth limit. A hostile PDF with deeply nested [[[…]]] or <<…>> drives the recursive-descent parser into a stack overflow — a fatal, unrecoverable crash (it isn't catchable by recover). Reachable from Open and from any object resolution.

Fix

Cap nesting at maxParseDepth (1000) in both parseArray and parseDict, returning an error past it. Real PDFs nest only a handful of levels; 1000 is far above legitimate use and far below a stack overflow.

Tests

  • TestDeeplyNestedRejected (array + dict subtests) — input nested well past the cap returns an error instead of crashing.
  • TestModeratelyNestedArrayResolves — moderate nesting (100) still resolves to an Array, so the cap doesn't break legitimate input.

Test depths sit above the cap but below an actual stack overflow, so a regression surfaces as a clean failure. Full suite + go vet pass; gofmt-clean.

parseArray/parseDict recursed without limit, so deeply nested [[[...]]]
or <<...>> in a hostile PDF could overflow the stack -- a fatal,
unrecoverable crash. Cap nesting at maxParseDepth (1000) and return an
error past it; real PDFs nest only a few levels.

Tests: deeply nested array and dict are rejected; moderate nesting still
resolves.
@fank fank requested a review from pgundlach June 18, 2026 17:08
@fank fank marked this pull request as ready for review June 18, 2026 17:08
@pgundlach pgundlach merged commit 3e7a086 into main Jun 19, 2026
1 check passed
@pgundlach pgundlach deleted the claude/parser-depth-limit branch June 19, 2026 05:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants