Skip to content

Commit 8285bc7

Browse files
[3.14] pythongh-140471: Fix buffer overflow in AST node initialization with malformed _fields (pythonGH-140506) (python#140509)
pythongh-140471: Fix buffer overflow in AST node initialization with malformed `_fields` (pythonGH-140506) (cherry picked from commit 95953b6) Co-authored-by: Stan Ulbrych <[email protected]>
1 parent 29c42cc commit 8285bc7

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
@@ -3309,6 +3309,15 @@ class MoreFieldsThanTypes(ast.AST):
33093309
self.assertEqual(obj.a, 1)
33103310
self.assertEqual(obj.b, 2)
33113311

3312+
def test_malformed_fields_with_bytes(self):
3313+
class BadFields(ast.AST):
3314+
_fields = (b'\xff'*64,)
3315+
_field_types = {'a': int}
3316+
3317+
# This should not crash
3318+
with self.assertWarnsRegex(DeprecationWarning, r"Field b'\\xff\\xff.*' .*"):
3319+
obj = BadFields()
3320+
33123321
def test_complete_field_types(self):
33133322
class _AllFieldTypes(ast.AST):
33143323
_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
@@ -1009,7 +1009,7 @@ def visitModule(self, mod):
10091009
else {
10101010
if (PyErr_WarnFormat(
10111011
PyExc_DeprecationWarning, 1,
1012-
"Field '%U' is missing from %.400s._field_types. "
1012+
"Field %R is missing from %.400s._field_types. "
10131013
"This will become an error in Python 3.15.",
10141014
name, Py_TYPE(self)->tp_name
10151015
) < 0) {
@@ -1044,7 +1044,7 @@ def visitModule(self, mod):
10441044
// simple field (e.g., identifier)
10451045
if (PyErr_WarnFormat(
10461046
PyExc_DeprecationWarning, 1,
1047-
"%.400s.__init__ missing 1 required positional argument: '%U'. "
1047+
"%.400s.__init__ missing 1 required positional argument: %R. "
10481048
"This will become an error in Python 3.15.",
10491049
Py_TYPE(self)->tp_name, name
10501050
) < 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)