Skip to content

Commit 1cc2c95

Browse files
[3.13] pythongh-140471: Fix buffer overflow in AST node initialization with malformed _fields (pythonGH-140506) (python#140510)
(cherry picked from commit 95953b6)
1 parent adf0c11 commit 1cc2c95

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

Lib/test/test_ast/test_ast.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3119,6 +3119,15 @@ class MoreFieldsThanTypes(ast.AST):
31193119
self.assertEqual(obj.a, 1)
31203120
self.assertEqual(obj.b, 2)
31213121

3122+
def test_malformed_fields_with_bytes(self):
3123+
class BadFields(ast.AST):
3124+
_fields = (b'\xff'*64,)
3125+
_field_types = {'a': int}
3126+
3127+
# This should not crash
3128+
with self.assertWarnsRegex(DeprecationWarning, r"Field b'\\xff\\xff.*' .*"):
3129+
obj = BadFields()
3130+
31223131
def test_complete_field_types(self):
31233132
class _AllFieldTypes(ast.AST):
31243133
_fields = ("a", "b")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix potential buffer overflow in :class:`ast.AST` node initialization when
2+
encountering malformed :attr:`~ast.AST._fields` containing non-:class:`str`.

Parser/asdl_c.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,7 @@ def visitModule(self, mod):
10061006
else {
10071007
if (PyErr_WarnFormat(
10081008
PyExc_DeprecationWarning, 1,
1009-
"Field '%U' is missing from %.400s._field_types. "
1009+
"Field %R is missing from %.400s._field_types. "
10101010
"This will become an error in Python 3.15.",
10111011
name, Py_TYPE(self)->tp_name
10121012
) < 0) {
@@ -1041,7 +1041,7 @@ def visitModule(self, mod):
10411041
// simple field (e.g., identifier)
10421042
if (PyErr_WarnFormat(
10431043
PyExc_DeprecationWarning, 1,
1044-
"%.400s.__init__ missing 1 required positional argument: '%U'. "
1044+
"%.400s.__init__ missing 1 required positional argument: %R. "
10451045
"This will become an error in Python 3.15.",
10461046
Py_TYPE(self)->tp_name, name
10471047
) < 0) {

Python/Python-ast.c

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)