diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py index 04e8308ef12..77839c20381 100644 --- a/Lib/test/test_ast.py +++ b/Lib/test/test_ast.py @@ -367,6 +367,20 @@ class AST_Tests(unittest.TestCase): compile(m, "", "exec") self.assertIn("but got <_ast.expr", str(cm.exception)) + def test_invalid_identitifer(self): + m = ast.Module([ast.Expr(ast.Name(42, ast.Load()))]) + ast.fix_missing_locations(m) + with self.assertRaises(TypeError) as cm: + compile(m, "", "exec") + self.assertIn("identifier must be of type str", str(cm.exception)) + + def test_invalid_string(self): + m = ast.Module([ast.Expr(ast.Str(42))]) + ast.fix_missing_locations(m) + with self.assertRaises(TypeError) as cm: + compile(m, "", "exec") + self.assertIn("string must be of type str", str(cm.exception)) + class ASTHelpers_Test(unittest.TestCase): diff --git a/Misc/NEWS b/Misc/NEWS index 7bccc05fc63..281a9e3e425 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,9 @@ What's New in Python 3.3 Alpha 1? Core and Builtins ----------------- +- Verify the types of AST strings and identifiers provided by the user before + compiling them. + - Issue #12579: str.format_map() now raises a ValueError if used on a format string that contains positional fields. Initial patch by Julian Berman. diff --git a/Parser/asdl_c.py b/Parser/asdl_c.py index 22ef3d0af1b..c38a8533f71 100755 --- a/Parser/asdl_c.py +++ b/Parser/asdl_c.py @@ -795,8 +795,25 @@ static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena) return 0; } -#define obj2ast_identifier obj2ast_object -#define obj2ast_string obj2ast_object +static int obj2ast_stringlike(PyObject* obj, PyObject** out, PyArena* arena, + const char *name) +{ + if (!PyUnicode_CheckExact(name)) { + PyErr_Format(PyExc_TypeError, "AST %s must be of type str", name); + return 1; + } + return obj2ast_object(obj, out, arena); +} + +static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena) +{ + return obj2ast_stringlike(obj, out, arena, "identifier"); +} + +static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena) +{ + return obj2ast_stringlike(obj, out, arena, "string"); +} static int obj2ast_int(PyObject* obj, int* out, PyArena* arena) { diff --git a/Python/Python-ast.c b/Python/Python-ast.c index 96c6bf83fc7..a1866470d56 100644 --- a/Python/Python-ast.c +++ b/Python/Python-ast.c @@ -592,8 +592,25 @@ static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena) return 0; } -#define obj2ast_identifier obj2ast_object -#define obj2ast_string obj2ast_object +static int obj2ast_stringlike(PyObject* obj, PyObject** out, PyArena* arena, + const char *name) +{ + if (!PyUnicode_CheckExact(name)) { + PyErr_Format(PyExc_TypeError, "AST %s must be of type str", name); + return 1; + } + return obj2ast_object(obj, out, arena); +} + +static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena) +{ + return obj2ast_stringlike(obj, out, arena, "identifier"); +} + +static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena) +{ + return obj2ast_stringlike(obj, out, arena, "string"); +} static int obj2ast_int(PyObject* obj, int* out, PyArena* arena) {