bpo-42609: Check recursion depth in the AST validator and optimizer (… · devdanzin/cpython@face87c · GitHub
Skip to content

Commit face87c

Browse files
bpo-42609: Check recursion depth in the AST validator and optimizer (pythonGH-23744)
1 parent b5adc8a commit face87c

5 files changed

Lines changed: 309 additions & 149 deletions

File tree

Include/internal/pycore_compile.h

Lines changed: 3 additions & 0 deletions

Lib/test/test_compile.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -543,21 +543,26 @@ def test_compiler_recursion_limit(self):
543543
# XXX (ncoghlan): duplicating the scaling factor here is a little
544544
# ugly. Perhaps it should be exposed somewhere...
545545
fail_depth = sys.getrecursionlimit() * 3
546+
crash_depth = sys.getrecursionlimit() * 300
546547
success_depth = int(fail_depth * 0.75)
547548

548-
def check_limit(prefix, repeated):
549+
def check_limit(prefix, repeated, mode="single"):
549550
expect_ok = prefix + repeated * success_depth
550-
self.compile_single(expect_ok)
551-
broken = prefix + repeated * fail_depth
552-
details = "Compiling ({!r} + {!r} * {})".format(
553-
prefix, repeated, fail_depth)
554-
with self.assertRaises(RecursionError, msg=details):
555-
self.compile_single(broken)
551+
compile(expect_ok, '<test>', mode)
552+
for depth in (fail_depth, crash_depth):
553+
broken = prefix + repeated * depth
554+
details = "Compiling ({!r} + {!r} * {})".format(
555+
prefix, repeated, depth)
556+
with self.assertRaises(RecursionError, msg=details):
557+
compile(broken, '<test>', mode)
556558

557559
check_limit("a", "()")
558560
check_limit("a", ".b")
559561
check_limit("a", "[0]")
560562
check_limit("a", "*a")
563+
# XXX Crashes in the parser.
564+
# check_limit("a", " if a else a")
565+
# check_limit("if a: pass", "\nelif a: pass", mode="exec")
561566

562567
def test_null_terminated(self):
563568
# The source code is null-terminated internally, but bytes-like
Lines changed: 3 additions & 0 deletions

0 commit comments

Comments
 (0)