Repository navigation
Bound AST nesting so deep source raises instead of crashing #8827
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
youknowone
merged 4 commits into
RustPython:main
from
sigmaith:fix/deep-nesting-recursion-guard
Sep 30, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
948f4d9
Bound AST nesting so deep source raises instead of crashing
sigmaith fa3fb02
Return Result from too_deeply_nested_error
sigmaith 4f39e6c
Return Result from pre_parse_source_error too
sigmaith 79e8989
Drop exceeds_max_nesting for the check beside it
sigmaith File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import ast | ||
|
|
||
| from testutils import assert_raises | ||
|
|
||
| # Deeply nested source used to exhaust the native stack, killing the process | ||
| # with a signal no `except` here could catch. What is asserted below is mostly | ||
| # that this file finishes at all: reaching the end means each case raised a | ||
| # Python exception instead of taking the interpreter down. | ||
| # | ||
| # Two limits keep that from happening, and each has to apply before a tree that | ||
| # deep is built. The parser grows its own stack when it runs low on one, and a | ||
| # tree it returns costs stack both to walk and to drop, so checking afterwards | ||
| # is already too late. | ||
|
|
||
|
|
||
| def nested_binop(depth): | ||
| return "x" + "+1" * depth | ||
|
|
||
|
|
||
| def nested_brackets(depth): | ||
| return "[" * depth + "1" + "]" * depth | ||
|
|
||
|
|
||
| # Brackets are counted in the source text before it is parsed, the way CPython | ||
| # counts them in its tokenizer, so no tree is built for these at all. | ||
| assert_raises(SyntaxError, compile, nested_brackets(200000), "<test>", "exec") | ||
| assert_raises(SyntaxError, ast.parse, nested_brackets(200000)) | ||
| assert_raises(SyntaxError, compile, nested_brackets(5000), "<test>", "exec") | ||
| assert_raises(SyntaxError, ast.parse, nested_brackets(5000)) | ||
|
|
||
| # Nesting that reaches the parser is bounded by the compiler's recursion limit, | ||
| # applied to the tree it returns. CPython bounds the same walks by how much | ||
| # stack is left rather than by a count, so it accepts depths rejected here; | ||
| # either outcome is fine, crashing is not. | ||
| try: | ||
| compile(nested_binop(20000), "<test>", "exec") | ||
| except RecursionError: | ||
| pass | ||
|
|
||
| # `compile(..., PyCF_ONLY_AST)`, which `ast.parse` uses, returns before the | ||
| # symbol table runs, so it used to reach the limit of no pass at all. | ||
| try: | ||
| ast.parse(nested_binop(5000)) | ||
| except RecursionError: | ||
| pass | ||
|
|
||
| # Nesting within both limits still compiles and runs. | ||
| assert compile(nested_binop(100), "<test>", "exec") | ||
| assert compile(nested_brackets(100), "<test>", "exec") | ||
| assert ast.parse(nested_binop(100)) | ||
| assert eval(compile(nested_binop(100), "<test>", "eval"), {"x": 0}) == 100 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 8436
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 44543
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 30399
🌐 Web query:
rustpython-ruff_python_ast 0.16.5 Visitor walk_stmt source💡 Result:
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 23923
Bound statement traversal in
EscapeWarningVisitor.pre_parse_source_errorchecks only delimiter nesting. Deeply nestedifsuites can pass this guard. The parser can produce trees deeper than later passes can walk, andVisitor::walk_stmtrecursively visits nested statement bodies.EscapeWarningVisitorlimits only expression traversal, so the warning pass can exhaust the native stack beforetoo_deeply_nested_errorruns. Add a statement-depth guard and a regression test.🐛 Suggested fix
impl<'a> Visitor<'a> for EscapeWarningVisitor<'a> { + fn visit_stmt(&mut self, stmt: &'a ast::Stmt) { + if self.error.is_some() { + return; + } + if self.depth < self.depth_limit { + self.depth += 1; + ast::visitor::walk_stmt(self, stmt); + self.depth -= 1; + } + } + fn visit_expr(&mut self, expr: &'a ast::Expr) {🤖 Prompt for AI Agents