Skip to content

Commit cf7d790

Browse files
StanFromIrelandmiss-islington
authored andcommitted
gh-156689: Fix out-of-bounds read in PyAst_CheckMode() for mode='func_type' (GH-156697)
(cherry picked from commit d7f9c64) Co-authored-by: Stan Ulbrych <stan@python.org>
1 parent 502191f commit cf7d790

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
@@ -2106,22 +2106,25 @@ class PartingShots(StaticVisitor):
21062106
return result;
21072107
}
21082108
2109-
/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
2109+
/* mode is 0 for "exec", 1 for "eval", 2 for "single" and 3 for "func_type"
2110+
input */
21102111
int PyAst_CheckMode(PyObject *ast, int mode)
21112112
{
2112-
const char * const req_name[] = {"Module", "Expression", "Interactive"};
2113+
const char * const req_name[] = {"Module", "Expression", "Interactive",
2114+
"FunctionType"};
21132115
21142116
struct ast_state *state = get_ast_state();
21152117
if (state == NULL) {
21162118
return -1;
21172119
}
21182120
2119-
PyObject *req_type[3];
2121+
PyObject *req_type[4];
21202122
req_type[0] = state->Module_type;
21212123
req_type[1] = state->Expression_type;
21222124
req_type[2] = state->Interactive_type;
2125+
req_type[3] = state->FunctionType_type;
21232126
2124-
assert(0 <= mode && mode <= 2);
2127+
assert(0 <= mode && mode <= 3);
21252128
int isinstance = PyObject_IsInstance(ast, req_type[mode]);
21262129
if (isinstance == -1) {
21272130
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)