Skip to content

v0.13.0 — nested source is an error, not a crash

Choose a tag to compare

@tannevaled tannevaled released this 07 Oct 16:19
fc6ea8c

Bounds expression recursion, so deeply nested source is a parse error instead of taking the process down.

What it fixes

Deeply nested source overflowed the Go stack, which is a fatal error: rather than a panic: — recover() never sees it, so neither this package's own recover in Parse nor an embedder's could contain it, and the process died. x = + f(×50000, about 150 KB of source, was enough.

It matters beyond this repository because go-embedded-ruby/ruby calls Parse as the first statement of its public Run, so a host that merely compiles untrusted Ruby was exposed without ever executing it. Tracked as #48.

Behaviour change

Parse now returns parse error at line N: nesting of 32769 is too deep past the limit. Previously it either succeeded (up to ~45 000 levels) or killed the process. No exported API changed — the package still exports Parse alone — which is why this is a minor and not a major.

The limit sits between two measured bounds:

nesting counter units
deepest shape MRI 4.0.5 accepts (ruby -c → "Syntax OK") 9 500 19 000
the limit — 32 768
deepest observed safe here 45 000 90 000

1.7× above the first, 2.7× below the second. So nothing MRI accepts is newly rejected, and nothing reaches the Go stack's real ceiling.

Two things the measurement settled

Where to count. No single function sees every production: instrumenting four candidates at nesting 1000 showed parsePrimary reaching 1 (not 1001) for f { … }, and parseExprOrAssign reaching 2 for a ternary chain. The four productions that actually overflow route through parseExprOrAssign, ternary through parseTernary, so both carry the count. An assignment chain (y = ×N) recurses through parseExprOrAssign alone — which is why deleting its check as apparently-dead code would have left y = ×300000, 1.2 MB of source, with no guard at all.

The counter measures frames, not brackets, and the two differ by exactly 2× for bracket productions. A first revision set the limit to 16384 believing it counted brackets; that capped nesting at 8192 and rejected a shape MRI accepts. A limit that is too low is as much a defect as no limit, just a quieter one, and the suite pins both ends for that reason.

Why a frame count and not a byte budget

Go offers no portable stack-pointer read, and a saved stack address is useless as a baseline: the runtime copies a goroutine's stack when it grows, leaving the value stale by the relocation delta. MRI can budget bytes; we cannot. The honest consequence, recorded in the source, is that a production costing far more stack per frame than any probed would reach the real ceiling first.

For the dependent

go-embedded-ruby/ruby pins this module and needs a go.mod bump to pick the fix up; go-embedded-ruby/ruby#768 tracks the VM-side sibling of this defect.

🤖 Generated with Claude Code