Skip to content

Commit 4e242df

Browse files
miss-islingtonAbhi210sergey-miryanov
authored
[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 acab532) Co-authored-by: Abhishek Tiwari <[email protected]> Co-authored-by: Sergey Miryanov <[email protected]>
1 parent 5eb16ad commit 4e242df

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a crash in the :term:`free-threaded build` where :c:func:`PyList_New` could leave
2+
a newly allocated list's item buffer pointer uninitialized when the backing
3+
array allocation failed under low memory, leading to an invalid free.

‎Objects/listobject.c‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,11 @@ PyList_New(Py_ssize_t size)
267267
return NULL;
268268
}
269269
}
270-
if (size <= 0) {
271-
op->ob_item = NULL;
272-
}
273-
else {
270+
op->ob_item = NULL;
271+
op->allocated = 0;
272+
Py_SET_SIZE(op, size);
273+
274+
if (size > 0) {
274275
#ifdef Py_GIL_DISABLED
275276
_PyListArray *array = list_allocate_array(size);
276277
if (array == NULL) {
@@ -287,7 +288,7 @@ PyList_New(Py_ssize_t size)
287288
return PyErr_NoMemory();
288289
}
289290
}
290-
Py_SET_SIZE(op, size);
291+
291292
op->allocated = size;
292293
_PyObject_GC_TRACK(op);
293294
return (PyObject *) op;

0 commit comments

Comments
 (0)