]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-109341: Fix crash on compiling invalid AST including TypeAlias (GH-109349...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 14 Sep 2023 22:40:19 +0000 (15:40 -0700)
committerGitHub <noreply@github.com>
Thu, 14 Sep 2023 22:40:19 +0000 (00:40 +0200)
gh-109341: Fix crash on compiling invalid AST including TypeAlias (GH-109349)
(cherry picked from commit 987b4bc0870e1e29a88275dc3fa39bf2c3dcc763)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Lib/test/test_compile.py
Misc/NEWS.d/next/Core and Builtins/2023-09-12-15-45-49.gh-issue-109341.4V5bkm.rst [new file with mode: 0644]
Python/ast.c

index 16a59c106bbb672fd37cbe4348e41ae4c7ecd1e5..fde52d40097f406e64201a83a71fc66266bdede3 100644 (file)
@@ -478,6 +478,26 @@ class TestSpecifics(unittest.TestCase):
         ast.body = [_ast.BoolOp()]
         self.assertRaises(TypeError, compile, ast, '<ast>', 'exec')
 
+    def test_compile_invalid_typealias(self):
+        # gh-109341
+        m = ast.Module(
+            body=[
+                ast.TypeAlias(
+                    name=ast.Subscript(
+                        value=ast.Name(id="foo", ctx=ast.Load()),
+                        slice=ast.Constant(value="x"),
+                        ctx=ast.Store(),
+                    ),
+                    type_params=[],
+                    value=ast.Name(id="Callable", ctx=ast.Load()),
+                )
+            ],
+            type_ignores=[],
+        )
+
+        with self.assertRaisesRegex(TypeError, "TypeAlias with non-Name name"):
+            compile(ast.fix_missing_locations(m), "<file>", "exec")
+
     def test_dict_evaluation_order(self):
         i = 0
 
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-09-12-15-45-49.gh-issue-109341.4V5bkm.rst b/Misc/NEWS.d/next/Core and Builtins/2023-09-12-15-45-49.gh-issue-109341.4V5bkm.rst
new file mode 100644 (file)
index 0000000..9e99ef7
--- /dev/null
@@ -0,0 +1 @@
+Fix crash when compiling an invalid AST involving a :class:`ast.TypeAlias`.
index 74c97f948d15e63d5b02fade7b09d7188218adc4..a3acf78ac9584429219df830623585d440a13d65 100644 (file)
@@ -768,6 +768,11 @@ validate_stmt(struct validator *state, stmt_ty stmt)
                validate_expr(state, stmt->v.AnnAssign.annotation, Load);
         break;
     case TypeAlias_kind:
+        if (stmt->v.TypeAlias.name->kind != Name_kind) {
+            PyErr_SetString(PyExc_TypeError,
+                            "TypeAlias with non-Name name");
+            return 0;
+        }
         ret = validate_expr(state, stmt->v.TypeAlias.name, Store) &&
             validate_type_params(state, stmt->v.TypeAlias.type_params) &&
             validate_expr(state, stmt->v.TypeAlias.value, Load);