Title: Deeply nested source overflows the Go stack fatally (140 KB is enough): no nesting limit, and recover() cannot contain it · Issue #48 · go-ruby-parser/parser · GitHub
Open Graph Title: Deeply nested source overflows the Go stack fatally (140 KB is enough): no nesting limit, and recover() cannot contain it · Issue #48 · go-ruby-parser/parser
X Title: Deeply nested source overflows the Go stack fatally (140 KB is enough): no nesting limit, and recover() cannot contain it · Issue #48 · go-ruby-parser/parser
Description: Summary Deeply nested source makes the parser overflow the Go stack, which is a fatal runtime error rather than a panic — so recover() cannot contain it and the host process dies. 140 KB of input is enough. MRI raises a catchable SystemS...
Open Graph Description: Summary Deeply nested source makes the parser overflow the Go stack, which is a fatal runtime error rather than a panic — so recover() cannot contain it and the host process dies. 140 KB of input i...
X Description: Summary Deeply nested source makes the parser overflow the Go stack, which is a fatal runtime error rather than a panic — so recover() cannot contain it and the host process dies. 140 KB of input i...
Opengraph URL: https://github.com/go-ruby-parser/parser/issues/48
X: @github
Domain: github.com
IPv4 address: 140.82.112.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Deeply nested source overflows the Go stack fatally (140 KB is enough): no nesting limit, and recover() cannot contain it","articleBody":"## Summary\n\nDeeply nested source makes the parser overflow the **Go** stack, which is a fatal runtime error rather than a panic — so `recover()` cannot contain it and the host process dies. 140 KB of input is enough. MRI raises a catchable `SystemStackError` on a *smaller* input.\n\nThis matters more than an ordinary parser limit because `go-embedded-ruby/ruby`'s public embedding API calls `parser.Parse` as its **first statement**: a host that merely *compiles* untrusted Ruby, without ever running it, is exposed.\n\nFound while auditing the embedding surface of rbgo; the VM-side sibling of this defect is go-embedded-ruby/ruby#768.\n\n## Measurement\n\nInput: `x = ` followed by `[` × N and `]` × N.\n\n| N | source size | parser (via rbgo's `ruby.Run`) | MRI 4.0.5 |\n|---|---|---|---|\n| 20 000 | 40 005 B | parses, exit 0 | `stack level too deep (SystemStackError)` |\n| 50 000 | 100 005 B | parses, exit 0 | `stack level too deep` |\n| 70 000 | 140 005 B | **`fatal error: stack overflow`, exit 2** | `stack level too deep` |\n\nAt N = 70 000 the goroutine dump contains **192 frames naming `go-ruby-parser`**, cycling through `parseExprOrAssign → parseTernary → parseRange → parseBinary → parseOneCallArg → parsePrimary → parseArrayLiteral → parseArrayElems` — roughly 8 Go frames per level of Ruby nesting, which is why it dies at a depth the VM-side paths survive.\n\nNote the shape of the first two rows: between 20 000 and 50 000 the parser is **more permissive than MRI**, accepting input MRI refuses. The defect is not that there is a limit; it is that there is no limit, so the ceiling is the Go stack and hitting it is unrecoverable.\n\n## Why `recover()` does not help\n\nA Go stack overflow is `fatal error:`, not `panic:`. Verified in plain Go with no parser involved — infinite recursion in a goroutine behind `defer func(){ recover() }()` prints neither the recover message nor anything after it, because **the deferred function never runs**; exit status 2. `runtime/debug.SetMaxStack` only moves the threshold: with `SetMaxStack(1 \u003c\u003c 20)` the message becomes `goroutine stack exceeds 1048576-byte limit` and it is still fatal. So a host cannot wrap `Parse` in a recover and survive, and there is no host-side mitigation to defer to.\n\n## Suggested shape\n\nA nesting-depth counter in the recursive descent, raising a parse **error** (not a panic) past a configured maximum, the way the JSON path in the sibling project already does — `JSON.parse` on deep input gives `JSON::NestingError: nesting of 101 is too deep`, cleanly, which is the behaviour to copy.\n\nTwo things worth deciding deliberately rather than by accident:\n\n- **Where to count.** The cycle above enters through several functions, so a counter in one of them undercounts. Incrementing on entry to the shared recursive step, or on each opening bracket/paren/brace, covers nesting that arrives through a different production — hashes, parentheses, method call chains and blocks should all be probed, not just array literals, since a limit that only sees `[` leaves the same hole behind a `{`.\n- **What the limit should be.** It should be chosen from a measurement of Go stack bytes per nesting level on the **worst** production, not copied from MRI's number, which is a consequence of MRI's own byte budget. Reporting bytes-per-level alongside the chosen depth makes the limit re-derivable when the parser's frames change.\n\nA regression test with a deeply nested literal that asserts a clean parse error is the part that keeps this fixed; the current ceiling is invisible until it is fatal.\n\n🤖 Generated with [Claude Code](https://claude.com/claude-code)\n","author":{"url":"https://github.com/tannevaled","@type":"Person","name":"tannevaled"},"datePublished":"2026-10-04T19:12:40Z","url":"https://github.com/go-ruby-parser/parser/issues/48"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:b551857d-b5e4-4858-81c2-dada571cf9fd |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8B28:3F9E09:3C37FCD:5049980:6B03FF91 |
| html-safe-nonce | dd2494b34290276bf8e8d3ff75504827e9b8b324356e8ed4f30bb8acbca37936 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QjI4OjNGOUUwOTozQzM3RkNEOjUwNDk5ODA6NkIwM0ZGOTEiLCJ2aXNpdG9yX2lkIjoiMjExMzYxMjY2NTgyMDgwNzA1NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 2543ef6df5bd543d00e9b1a2c30b8f54adfdf4527a459f730fe2b1eb5efa7899 |
| hovercard-subject-tag | issue:5702415479 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/go-ruby-parser/parser/48/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6272a37dd2e1e543ccfb20dfa2833243c57308a190fc906ef9cba6ba49d483f4/go-ruby-parser/parser/issues/48 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6272a37dd2e1e543ccfb20dfa2833243c57308a190fc906ef9cba6ba49d483f4/go-ruby-parser/parser/issues/48 |
| og:image:alt | Summary Deeply nested source makes the parser overflow the Go stack, which is a fatal runtime error rather than a panic — so recover() cannot contain it and the host process dies. 140 KB of input i... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | tannevaled |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/go-ruby-parser/parser git https://github.com/go-ruby-parser/parser.git |
| octolytics-dimension-user_id | 295635196 |
| octolytics-dimension-user_login | go-ruby-parser |
| octolytics-dimension-repository_id | 1276240137 |
| octolytics-dimension-repository_nwo | go-ruby-parser/parser |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1276240137 |
| octolytics-dimension-repository_network_root_nwo | go-ruby-parser/parser |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width