Parser stack overflow: deeply-nested source causes SIGSEGV instead of SyntaxError

Aperta
#7,655 1 commento 0 reazioni 1 assegnatario Vedi su GitHub

@sigmaith ci sta già lavorando.

Dal 17/8/2026.

Valutazione

Questa issue non è ancora stata valutata.

Descrizione

Summary

Parsing deeply-nested Python source (brackets, parens, etc.) crashes the interpreter with SIGSEGV at ~4000 levels of nesting. CPython caps this at 200 via SyntaxError: too many nested parentheses from the parser's internal depth limit.

The crash is in the parser itself (rustpython-ruff_python_parser 0.15.8): compile(src, '<s>', 'exec') (parser + codegen, does NOT go through _ast::ast_to_object) and ast.parse() crash at the identical depth threshold, showing the shared component is the parser, not post-parse conversion.

This is the parser-layer root cause of the class of bugs tracked in #2796. #4857 also still reproduces on current main (closed as fixed in 2023).

Expected

Matches CPython 3.13.4 behavior:

$ python3 -c "import ast; ast.parse('[' * 5000 + '1' + ']' * 5000)"
Traceback (most recent call last):
  ...
SyntaxError: too many nested parentheses (<unknown>, line 1)

(CPython's parser rejects at depth 201 via a hard-coded limit, well before the native stack is exhausted.)

Actual

$ rustpython -c "import ast; ast.parse('[' * 5000 + '1' + ']' * 5000)"
[no output, exit 139 — SIGSEGV]

$ rustpython -c "src='['*5000+'1'+']'*5000; compile(src,'<s>','exec')"
[no output, exit 139 — SIGSEGV]

# #4857's original reproducer also still crashes:
$ rustpython -c "import ast; ast.parse('+chr(33)' * 1000000)"
[no output, exit 139]
Threshold
Depth compile() ast.parse()
3000 ok ok
4000 SIGSEGV SIGSEGV
15000 SIGSEGV SIGSEGV

Identical thresholds confirm the crash is in the parser, not in post-parse conversion.

Relation to prior fixes

  • #7630 guarded _ast::ast_from_object (AST object → Rust IR conversion).
  • #7632 guarded json.loads() via vm.with_recursion.

Both fixes live downstream of the parser. The parser's own recursive descent has no depth guard, so any Python source deeply nested in brackets, parens, unary ops, or binary ops reaches the native stack limit first.

Environment

  • RustPython main @ 5a45d41d (parser: rustpython-ruff_python_parser 0.15.8)
  • Compared against: CPython 3.13.4
  • macOS 15.1 / Darwin 24.1.0
  • Rust release build
Lingua principale
Rust
Stelle
22.4k
Fork
1.5k
Merge medio
15h 18m
PR unite (30g)
172

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di RustPython/RustPython

Tutte le issue di RustPython/RustPython

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.