Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Lib/test/test_ast/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1614,6 +1614,17 @@ def test_literal_eval_syntax_errors(self):
(\
\ ''')

def test_literal_eval_large_input_crash(self):
# gh-125010: Fix use-after-free in ast repr()
# Note: Without setting sys.set_int_max_str_digits(0),
# this code throws a 'ValueError: Exceeds the limit (4300 digits)'.
# With sys.set_int_max_str_digits(0),
# this code throws a 'ValueError: malformed node or string on line 1':
source = "{0x0" + "e" * 250_000 + "%" + "e" * 250_000 + "1j}"
with self.assertRaisesRegex(ValueError,
r"Exceeds the limit \(4300 digits\)"):
ast.literal_eval(source)

def test_bad_integer(self):
# issue13436: Bad error message with invalid numeric values
body = [ast.ImportFrom(module='time',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a use-after-free bug in the :func:`repr` implementation of
:class:`ast.AST` nodes.
4 changes: 2 additions & 2 deletions Parser/asdl_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -1604,14 +1604,14 @@ def visitModule(self, mod):
value_repr = PyObject_Repr(value);
}

Py_DECREF(value);

if (!value_repr) {
Py_DECREF(name);
Py_DECREF(value);
goto error;
}

Py_DECREF(value);

if (i > 0) {
if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0) {
Py_DECREF(name);
Expand Down
4 changes: 2 additions & 2 deletions Python/Python-ast.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading