]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-109351: Fix crash when compiling AST with invalid NamedExpr (GH-109352...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 13 Sep 2023 16:32:08 +0000 (09:32 -0700)
committerGitHub <noreply@github.com>
Wed, 13 Sep 2023 16:32:08 +0000 (16:32 +0000)
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 b286ab7cfe7e6bf1e684386957dd0828efb1fa30..408064919b0b49f020a1cd9bd2bc42d5a5997766 100644 (file)
@@ -442,6 +442,33 @@ if 1:
         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 95179cb70281b2a8abfb1f1d59773d8af9d436eb..8bc3c9623731bebbd0e82195a136ab73ad6dc2a0 100644 (file)
@@ -379,6 +379,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. */