https://github.com/python/cpython/commit/4e242dfb422e99a6987d51b6132892b8f80cfb7e
commit: 4e242dfb422e99a6987d51b6132892b8f80cfb7e
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: Eclips4 <[email protected]>
date: 2026-11-10T23:37:46Z
summary:

[3.14] gh-151763: Fix crash in PyList_New() on allocation failure under low 
memory (GH-154019) (#161786)

gh-151763: Fix crash in PyList_New() on allocation failure under low memory 
(GH-154019)
(cherry picked from commit acab53212d89b298ba0dd299e8e55c62bf71db80)

Co-authored-by: Abhishek Tiwari <[email protected]>
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 6de6b864df4555..98337071124b11 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -267,10 +267,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) {
@@ -287,7 +288,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]

Reply via email to