From 00474472944944b346d8409cfded84bb299f601a Mon Sep 17 00:00:00 2001 From: Pablo Galindo Salgado Date: Sun, 24 Jul 2022 15:58:52 +0100 Subject: gh-95185: Check recursion depth in the AST constructor (#95186) Co-authored-by: Serhiy Storchaka --- Lib/test/test_ast.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'Lib/test/test_ast.py') diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py index 480089aa8a..9734218c21 100644 --- a/Lib/test/test_ast.py +++ b/Lib/test/test_ast.py @@ -793,6 +793,27 @@ class AST_Tests(unittest.TestCase): return self enum._test_simple_enum(_Precedence, ast._Precedence) + @support.cpython_only + def test_ast_recursion_limit(self): + fail_depth = sys.getrecursionlimit() * 3 + crash_depth = sys.getrecursionlimit() * 300 + success_depth = int(fail_depth * 0.75) + + def check_limit(prefix, repeated): + expect_ok = prefix + repeated * success_depth + ast.parse(expect_ok) + for depth in (fail_depth, crash_depth): + broken = prefix + repeated * depth + details = "Compiling ({!r} + {!r} * {})".format( + prefix, repeated, depth) + with self.assertRaises(RecursionError, msg=details): + ast.parse(broken) + + check_limit("a", "()") + check_limit("a", ".b") + check_limit("a", "[0]") + check_limit("a", "*a") + class ASTHelpers_Test(unittest.TestCase): maxDiff = None -- cgit v1.2.1