Files
RustPython/crates/compiler
Sumi Jeong c9c78db91c Bound AST nesting so deep source raises instead of crashing (#8827)
* Bound AST nesting so deep source raises instead of crashing

Deeply nested source exhausted the native stack and killed the process
with SIGSEGV, which no Python-level `except` can catch.

The parser grows its own stack when it runs low on one, so it returns
trees deeper than the passes after it can walk. Those passes recurse
without a limit of their own, and the symbol table, which does apply
one, runs last. `compile(..., PyCF_ONLY_AST)` returns before reaching
it at all.

Apply that same limit to the tree right after it is parsed, and count
bracket nesting before parsing rather than after, as CPython's
tokenizer does. Checking afterwards built the tree first, and one
nested that deep exhausts the stack when it is dropped.

Fixes #7655

Assisted-by: Claude Code:Opus 5

* Return Result from too_deeply_nested_error

Lets the four call sites use `?` instead of matching on `Some` and
returning the error by hand, which drops 11 lines. `Result` is already
`#[must_use]`, so the attribute on the function goes away with it.

Assisted-by: Claude Code:Opus 5

* Return Result from pre_parse_source_error too

Same shape as the previous commit, for the other check this PR adds, so
the two are consistent with each other.

Assisted-by: Claude Code:Opus 5

* Drop exceeds_max_nesting for the check beside it

The escape-warning pass needs the bracket check but had no `SourceFile` to
build a `CompileError` from, so it got a `bool` wrapper of its own. Building
the `SourceFile` there lets it call `pre_parse_source_error` instead, which
is shaped like the other checks in that file, and costs nothing measurable.

Assisted-by: Claude Code:Opus 5
2026-09-30 22:13:15 +09:00
..