mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
bpo-11105: Do not crash when compiling recursive ASTs (GH-20594)
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.
This commit is contained in:
parent
f461a7fc3f
commit
f3491242e4
4 changed files with 840 additions and 4 deletions
|
@ -1098,6 +1098,20 @@ Module(
|
|||
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):
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue