Skip to content

bpo-42609: Check recursion depth in the AST validator and optimizer #23744

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Include/internal/pycore_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ extern PyObject* _Py_Mangle(PyObject *p, PyObject *name);
typedef struct {
int optimize;
int ff_features;

int recursion_depth; /* current recursion depth */
int recursion_limit; /* recursion limit */
} _PyASTOptimizeState;

extern int _PyAST_Optimize(
Expand Down
19 changes: 12 additions & 7 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,21 +543,26 @@ def test_compiler_recursion_limit(self):
# XXX (ncoghlan): duplicating the scaling factor here is a little
# ugly. Perhaps it should be exposed somewhere...
fail_depth = sys.getrecursionlimit() * 3
crash_depth = sys.getrecursionlimit() * 300
success_depth = int(fail_depth * 0.75)

def check_limit(prefix, repeated):
def check_limit(prefix, repeated, mode="single"):
expect_ok = prefix + repeated * success_depth
self.compile_single(expect_ok)
broken = prefix + repeated * fail_depth
details = "Compiling ({!r} + {!r} * {})".format(
prefix, repeated, fail_depth)
with self.assertRaises(RecursionError, msg=details):
self.compile_single(broken)
compile(expect_ok, '<test>', mode)
for depth in (fail_depth, crash_depth):
broken = prefix + repeated * depth
details = "Compiling ({!r} + {!r} * {})".format(
prefix, repeated, depth)
Comment on lines +554 to +555
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An f-string could be used.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but this code already was here.

with self.assertRaises(RecursionError, msg=details):
compile(broken, '<test>', mode)

check_limit("a", "()")
check_limit("a", ".b")
check_limit("a", "[0]")
check_limit("a", "*a")
# XXX Crashes in the parser.
# check_limit("a", " if a else a")
# check_limit("if a: pass", "\nelif a: pass", mode="exec")

def test_null_terminated(self):
# The source code is null-terminated internally, but bytes-like
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Prevented crashes in the AST validator and optimizer when compiling some
absurdly long expressions like ``"+0"*1000000``. :exc:`RecursionError` is
now raised instead.
Loading