https://github.com/python/cpython/commit/acab53212d89b298ba0dd299e8e55c62bf71db80 commit: acab53212d89b298ba0dd299e8e55c62bf71db80 branch: main author: Abhishek Tiwari <[email protected]> committer: Eclips4 <[email protected]> date: 2026-11-10T22:58:40Z summary:
gh-151763: Fix crash in PyList_New() on allocation failure under low memory (#154019) Co-authored-by: Sergey Miryanov <[email protected]> files: A Misc/NEWS.d/next/Core_and_Builtins/2026-07-18-22-56-27.gh-issue-152125.sEPvJf.rst M Objects/listobject.c diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-07-18-22-56-27.gh-issue-152125.sEPvJf.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-18-22-56-27.gh-issue-152125.sEPvJf.rst new file mode 100644 index 00000000000000..ae66f69f206217 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-18-22-56-27.gh-issue-152125.sEPvJf.rst @@ -0,0 +1,3 @@ +Fix a crash in the :term:`free-threaded build` where :c:func:`PyList_New` could leave +a newly allocated list's item buffer pointer uninitialized when the backing +array allocation failed under low memory, leading to an invalid free. diff --git a/Objects/listobject.c b/Objects/listobject.c index 4384a850780b7f..c5fe704df0e23d 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -273,10 +273,11 @@ PyList_New(Py_ssize_t size) return NULL; } } - if (size <= 0) { - op->ob_item = NULL; - } - else { + op->ob_item = NULL; + op->allocated = 0; + Py_SET_SIZE(op, size); + + if (size > 0) { #ifdef Py_GIL_DISABLED _PyListArray *array = list_allocate_array(size); if (array == NULL) { @@ -293,7 +294,7 @@ PyList_New(Py_ssize_t size) return PyErr_NoMemory(); } } - Py_SET_SIZE(op, size); + op->allocated = size; _PyObject_GC_TRACK(op); return (PyObject *) op; _______________________________________________ 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]
