]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-109351: Fix crash when compiling AST with invalid NamedExpr (GH-109352...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 2 Oct 2023 14:58:06 +0000 (07:58 -0700)
committerGitHub <noreply@github.com>
Mon, 2 Oct 2023 14:58:06 +0000 (16:58 +0200)
gh-109351: Fix crash when compiling AST with invalid NamedExpr (GH-109352)
(cherry picked from commit 79101edb03b7381b514126c68acabfcbbba2f842)

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

index e377620a0c87b8a9a18125eb887df9ddfc7050e7..b75d1c59591e91ba64de4788c2e6cd2df4997308 100644 (file)
@@ -443,6 +443,33 @@ class TestSpecifics(unittest.TestCase):
         self.assertIn("_A__mangled_mod", A.f.__code__.co_varnames)
         self.assertIn("__package__", A.f.__code__.co_varnames)
 
+    def test_compile_invalid_namedexpr(self):
+        # gh-109351
+        m = ast.Module(
+            body=[
+                ast.Expr(
+                    value=ast.ListComp(
+                        elt=ast.NamedExpr(
+                            target=ast.Constant(value=1),
+                            value=ast.Constant(value=3),
+                        ),
+                        generators=[
+                            ast.comprehension(
+                                target=ast.Name(id="x", ctx=ast.Store()),
+                                iter=ast.Name(id="y", ctx=ast.Load()),
+                                ifs=[],
+                                is_async=0,
+                            )
+                        ],
+                    )
+                )
+            ],
+            type_ignores=[],
+        )
+
+        with self.assertRaisesRegex(TypeError, "NamedExpr target must be a Name"):
+            compile(ast.fix_missing_locations(m), "<file>", "exec")
+
     def test_compile_ast(self):
         fname = __file__
         if fname.lower().endswith('pyc'):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-09-12-16-00-42.gh-issue-109351.kznGeR.rst b/Misc/NEWS.d/next/Core and Builtins/2023-09-12-16-00-42.gh-issue-109351.kznGeR.rst
new file mode 100644 (file)
index 0000000..23b81c1
--- /dev/null
@@ -0,0 +1,2 @@
+Fix crash when compiling an invalid AST involving a named (walrus)
+expression.
index a3acf78ac9584429219df830623585d440a13d65..82d7beec0ee510726556e207e54b80165b566fbc 100644 (file)
@@ -381,6 +381,11 @@ validate_expr(struct validator *state, expr_ty exp, expr_context_ty ctx)
         ret = validate_exprs(state, exp->v.Tuple.elts, ctx, 0);
         break;
     case NamedExpr_kind:
+        if (exp->v.NamedExpr.target->kind != Name_kind) {
+            PyErr_SetString(PyExc_TypeError,
+                            "NamedExpr target must be a Name");
+            return 0;
+        }
         ret = validate_expr(state, exp->v.NamedExpr.value, Load);
         break;
     /* This last case doesn't have any checking. */