[3.9] bpo-11105: Do not crash when compiling recursive ASTs (GH-20594) (GH-26522)

When compiling an AST object with a direct / indirect reference
cycles, on the conversion phase because of exceeding amount of
calls, a segfault was raised. This patch adds recursion guards to
places for preventing user inputs to not to crash AST but instead
raise a RecursionError..
(cherry picked from commit f3491242e4)

Co-authored-by: Batuhan Taskaya <batuhan@python.org>
This commit is contained in:
Batuhan Taskaya 2021-06-04 00:22:34 +03:00 committed by GitHub
parent 5a8ddcc452
commit de58b319af
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 732 additions and 4 deletions

View file

@ -1027,6 +1027,20 @@ def test_level_as_none(self):
exec(code, ns)
self.assertIn('sleep', ns)
def test_recursion_direct(self):
e = ast.UnaryOp(op=ast.Not(), lineno=0, col_offset=0)
e.operand = e
with self.assertRaises(RecursionError):
compile(ast.Expression(e), "<test>", "eval")
def test_recursion_indirect(self):
e = ast.UnaryOp(op=ast.Not(), lineno=0, col_offset=0)
f = ast.UnaryOp(op=ast.Not(), lineno=0, col_offset=0)
e.operand = f
f.operand = e
with self.assertRaises(RecursionError):
compile(ast.Expression(e), "<test>", "eval")
class ASTValidatorTests(unittest.TestCase):