Skip to content

Commit d7f9c64

Browse files
gh-156689: Fix out-of-bounds read in PyAst_CheckMode() for mode='func_type' (#156697)
1 parent 03503dc commit d7f9c64

5 files changed

Lines changed: 26 additions & 9 deletions

File tree

Doc/library/ast.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2254,7 +2254,7 @@ and classes for traversing abstract syntax trees:
22542254

22552255
In addition, if ``mode`` is ``'func_type'``, the input syntax is
22562256
modified to correspond to :pep:`484` "signature type comments",
2257-
e.g. ``(str, int) -> List[str]``.
2257+
for example ``(str, int) -> List[str]``.
22582258

22592259
Setting ``feature_version`` to a tuple ``(major, minor)`` will result in
22602260
a "best-effort" attempt to parse using that Python version's grammar.

Lib/test/test_ast/test_ast.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,15 @@ def test_parse_invalid_ast(self):
162162
self.assertRaises(TypeError, ast.parse, ast.Constant(42),
163163
optimize=optval)
164164

165+
def test_parse_ast_func_type(self):
166+
# see gh-156689
167+
tree = ast.parse('(int, str) -> bool', mode='func_type')
168+
self.assertEqual(ast.dump(ast.parse(tree, mode='func_type')),
169+
ast.dump(tree))
170+
self.assertRaises(TypeError, ast.parse, ast.Constant(42),
171+
mode='func_type')
172+
self.assertRaises(TypeError, ast.parse, tree, mode='exec')
173+
165174
def test_optimization_levels__debug__(self):
166175
cases = [(-1, '__debug__'), (0, '__debug__'), (1, False), (2, False)]
167176
for (optval, expected) in cases:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix an out-of-bounds read in :func:`compile` and :func:`ast.parse` when an AST
2+
object is passed with ``mode='func_type'``.

Parser/asdl_c.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2122,22 +2122,25 @@ class PartingShots(StaticVisitor):
21222122
return result;
21232123
}
21242124
2125-
/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
2125+
/* mode is 0 for "exec", 1 for "eval", 2 for "single" and 3 for "func_type"
2126+
input */
21262127
int PyAst_CheckMode(PyObject *ast, int mode)
21272128
{
2128-
const char * const req_name[] = {"Module", "Expression", "Interactive"};
2129+
const char * const req_name[] = {"Module", "Expression", "Interactive",
2130+
"FunctionType"};
21292131
21302132
struct ast_state *state = get_ast_state();
21312133
if (state == NULL) {
21322134
return -1;
21332135
}
21342136
2135-
PyObject *req_type[3];
2137+
PyObject *req_type[4];
21362138
req_type[0] = state->Module_type;
21372139
req_type[1] = state->Expression_type;
21382140
req_type[2] = state->Interactive_type;
2141+
req_type[3] = state->FunctionType_type;
21392142
2140-
assert(0 <= mode && mode <= 2);
2143+
assert(0 <= mode && mode <= 3);
21412144
int isinstance = PyObject_IsInstance(ast, req_type[mode]);
21422145
if (isinstance == -1) {
21432146
return -1;

Python/Python-ast.c

Lines changed: 7 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)