From fa80b209a2a3f166b34cff12a8d8c36a99fa16d2 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Mon, 19 Oct 2026 12:34:30 -0700 Subject: [PATCH] gh-159739: Check character formatting allocation failure Propagate the error from PyUnicode_FromOrdinal() before using the result. Backport to 3.15 and 3.14. On 3.14, the equivalent code is in Python/formatter_unicode.c. --- .../2026-10-19-19-33-35.gh-issue-159739.173d49.rst | 2 ++ Objects/unicode_formatter.c | 3 +++ 2 files changed, 5 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.173d49.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.173d49.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.173d49.rst new file mode 100644 index 000000000000000..a552d06ff7bb898 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.173d49.rst @@ -0,0 +1,2 @@ +Avoid a crash if formatting an integer with the ``c`` presentation type runs +out of memory. diff --git a/Objects/unicode_formatter.c b/Objects/unicode_formatter.c index 8cbc774584fa685..7f0e0584f3813f3 100644 --- a/Objects/unicode_formatter.c +++ b/Objects/unicode_formatter.c @@ -1258,6 +1258,9 @@ format_long_internal(PyObject *value, const InternalFormatSpec *format, goto done; } tmp = PyUnicode_FromOrdinal(x); + if (tmp == NULL) { + goto done; + } inumeric_chars = 0; n_digits = 1; maxchar = Py_MAX(maxchar, (Py_UCS4)x);