https://github.com/python/cpython/commit/8285bc7ea2a99b571a08eb1d7883eff0c7ca7e1c
commit: 8285bc7ea2a99b571a08eb1d7883eff0c7ca7e1c
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: JelleZijlstra <[email protected]>
date: 2025-10-23T10:20:21-07:00
summary:

[3.14] gh-140471: Fix buffer overflow in AST node initialization with malformed 
`_fields` (GH-140506) (#140509)

gh-140471: Fix buffer overflow in AST node initialization with malformed 
`_fields` (GH-140506)
(cherry picked from commit 95953b692db6cbd88139de12d81fb123293ec2d5)

Co-authored-by: Stan Ulbrych <[email protected]>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2025-10-23-16-05-50.gh-issue-140471.Ax_aXn.rst
M Lib/test/test_ast/test_ast.py
M Parser/asdl_c.py
M Python/Python-ast.c

diff --git a/Lib/test/test_ast/test_ast.py b/Lib/test/test_ast/test_ast.py
index 5dbf36c6ce4f3b..963a3e705e2b81 100644
--- a/Lib/test/test_ast/test_ast.py
+++ b/Lib/test/test_ast/test_ast.py
@@ -3309,6 +3309,15 @@ class MoreFieldsThanTypes(ast.AST):
         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
index 00000000000000..afa9326fff3aee
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2025-10-23-16-05-50.gh-issue-140471.Ax_aXn.rst
@@ -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`.
diff --git a/Parser/asdl_c.py b/Parser/asdl_c.py
index dba20226c3283a..3e252cbc4883d1 100755
--- a/Parser/asdl_c.py
+++ b/Parser/asdl_c.py
@@ -1009,7 +1009,7 @@ def visitModule(self, mod):
                 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) {
@@ -1044,7 +1044,7 @@ def visitModule(self, mod):
                 // 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) {
diff --git a/Python/Python-ast.c b/Python/Python-ast.c
index 660bc598a4862c..aac24ed7d3c0c5 100644
--- a/Python/Python-ast.c
+++ b/Python/Python-ast.c
@@ -5293,7 +5293,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) {
@@ -5328,7 +5328,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) {

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to