]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-140471: Fix buffer overflow in AST node initialization with malformed ...
authorStan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
Thu, 23 Oct 2025 23:38:02 +0000 (00:38 +0100)
committerGitHub <noreply@github.com>
Thu, 23 Oct 2025 23:38:02 +0000 (00:38 +0100)
(cherry picked from commit 95953b692db6cbd88139de12d81fb123293ec2d5)

Lib/test/test_ast/test_ast.py
Misc/NEWS.d/next/Core and Builtins/2025-10-23-16-05-50.gh-issue-140471.Ax_aXn.rst [new file with mode: 0644]
Parser/asdl_c.py
Python/Python-ast.c

index 442c8f7f14ee2142325d697faace364c359d31e3..a8d111a4afe79731606f7799296ac951c8fe1ebb 100644 (file)
@@ -3119,6 +3119,15 @@ class ASTConstructorTests(unittest.TestCase):
         self.assertEqual(obj.a, 1)
         self.assertEqual(obj.b, 2)
 
+    def test_malformed_fields_with_bytes(self):
+        class BadFields(ast.AST):
+            _fields = (b'\xff'*64,)
+            _field_types = {'a': int}
+
+        # This should not crash
+        with self.assertWarnsRegex(DeprecationWarning, r"Field b'\\xff\\xff.*' .*"):
+            obj = BadFields()
+
     def test_complete_field_types(self):
         class _AllFieldTypes(ast.AST):
             _fields = ("a", "b")
diff --git a/Misc/NEWS.d/next/Core and Builtins/2025-10-23-16-05-50.gh-issue-140471.Ax_aXn.rst b/Misc/NEWS.d/next/Core and Builtins/2025-10-23-16-05-50.gh-issue-140471.Ax_aXn.rst
new file mode 100644 (file)
index 0000000..afa9326
--- /dev/null
@@ -0,0 +1,2 @@
+Fix potential buffer overflow in :class:`ast.AST` node initialization when
+encountering malformed :attr:`~ast.AST._fields` containing non-:class:`str`.
index 936317b7ae6e0c7106902e35224de5f59cd79ff0..99312b36cd33c37001735e4b60216f1f283a783c 100755 (executable)
@@ -1006,7 +1006,7 @@ ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
                 else {
                     if (PyErr_WarnFormat(
                         PyExc_DeprecationWarning, 1,
-                        "Field '%U' is missing from %.400s._field_types. "
+                        "Field %R is missing from %.400s._field_types. "
                         "This will become an error in Python 3.15.",
                         name, Py_TYPE(self)->tp_name
                     ) < 0) {
@@ -1041,7 +1041,7 @@ ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
                 // simple field (e.g., identifier)
                 if (PyErr_WarnFormat(
                     PyExc_DeprecationWarning, 1,
-                    "%.400s.__init__ missing 1 required positional argument: '%U'. "
+                    "%.400s.__init__ missing 1 required positional argument: %R. "
                     "This will become an error in Python 3.15.",
                     Py_TYPE(self)->tp_name, name
                 ) < 0) {
index 08ac2507d984d2a7343c8d99e51fdbd3dade939e..a71262c7f84abe824eb8253979e7b37f151eb64d 100644 (file)
@@ -5203,7 +5203,7 @@ ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
                 else {
                     if (PyErr_WarnFormat(
                         PyExc_DeprecationWarning, 1,
-                        "Field '%U' is missing from %.400s._field_types. "
+                        "Field %R is missing from %.400s._field_types. "
                         "This will become an error in Python 3.15.",
                         name, Py_TYPE(self)->tp_name
                     ) < 0) {
@@ -5238,7 +5238,7 @@ ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
                 // simple field (e.g., identifier)
                 if (PyErr_WarnFormat(
                     PyExc_DeprecationWarning, 1,
-                    "%.400s.__init__ missing 1 required positional argument: '%U'. "
+                    "%.400s.__init__ missing 1 required positional argument: %R. "
                     "This will become an error in Python 3.15.",
                     Py_TYPE(self)->tp_name, name
                 ) < 0) {