Skip to content

Commit 297af71

Browse files
committed
Fix _PyListBuilder_INIT_ITEMS
* Fix PyListBuilder_Extend() in debug mode: initialize newly allocated items to NULL. * PyListBuilder_AppendSteal() no longer checks if the item was already set. * PyListBuilder_ExtendArray() now raises SystemError if an array item is NULL in debug mode. * PyListBuilder_Finish() now fails with SystemError if an item is NULL. The error message gives the item index.
1 parent 05db290 commit 297af71

2 files changed

Lines changed: 50 additions & 14 deletions

File tree

‎Lib/test/test_capi/test_list.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,21 @@ def check_allocated(items, alloc, expected):
536536
self.list_equal(result, [1])
537537
self.assertEqual(list_get_allocated(result), alloc)
538538

539+
def test_combined(self):
540+
# Call each method twice: it must not fail/crash.
541+
builder = self.create_builder(0)
542+
builder.append('a')
543+
builder.append_steal('b')
544+
builder.extend(('c',))
545+
builder.extend_array(('d',))
546+
builder.extend_array_steal(('e',))
547+
builder.append('A')
548+
builder.append_steal('B')
549+
builder.extend(('C',))
550+
builder.extend_array(('D',))
551+
builder.extend_array_steal(('E',))
552+
self.list_equal(builder.finish(), list('abcdeABCDE'))
553+
539554
def test_discard(self):
540555
# Test PyListBuilder_Discard(): must not leak memory
541556
builder = self.create_builder(0)

‎Objects/listobject.c‎

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4506,11 +4506,15 @@ PyListBuilder_Finish(PyListBuilder *builder)
45064506
}
45074507

45084508
#if _PyListBuilder_INIT_ITEMS
4509-
// Make sure that all items have been initialized
4509+
// Make sure that all list items have been set
45104510
Py_ssize_t size = Py_SIZE(list);
45114511
PyObject **list_items = list->ob_item;
4512-
for (Py_ssize_t i = 0; i < size; i++) {
4513-
assert(list_items[i] != NULL);
4512+
for (Py_ssize_t index = 0; index < size; index++) {
4513+
if (list_items[index] == NULL) {
4514+
PyErr_Format(PyExc_SystemError, "list item #%zd is NULL", index);
4515+
Py_DECREF(list);
4516+
return NULL;
4517+
}
45144518
}
45154519
#endif
45164520

@@ -4527,12 +4531,15 @@ listbuilder_resize(PyListBuilder *builder, Py_ssize_t new_size)
45274531
#if _PyListBuilder_INIT_ITEMS
45284532
Py_ssize_t old_size = Py_SIZE(list);
45294533
#endif
4534+
assert(Py_SIZE(list) < new_size);
45304535

45314536
// On Free Threading, it can call _PyObject_GC_SET_SHARED() on the list
45324537
int res = list_resize(list, new_size);
4538+
45334539
#if _PyListBuilder_INIT_ITEMS
45344540
Py_ssize_t allocated = list->allocated;
45354541
if (res == 0 && allocated > old_size) {
4542+
// Set newly allocated items to NULL
45364543
memset(&list->ob_item[old_size], 0,
45374544
(allocated - old_size) * sizeof(PyObject*));
45384545
}
@@ -4552,10 +4559,6 @@ PyListBuilder_AppendSteal(PyListBuilder *builder, PyObject *item)
45524559
Py_ssize_t allocated = list->allocated;
45534560
if (index < allocated) {
45544561
Py_SET_SIZE(list, index + 1);
4555-
#if _PyListBuilder_INIT_ITEMS
4556-
// A list item must be set exactly once
4557-
assert(list->ob_item[index] == NULL);
4558-
#endif
45594562
list->ob_item[index] = item;
45604563
return 0;
45614564
}
@@ -4565,10 +4568,6 @@ PyListBuilder_AppendSteal(PyListBuilder *builder, PyObject *item)
45654568
return -1;
45664569
}
45674570

4568-
#if _PyListBuilder_INIT_ITEMS
4569-
// A list item must be set exactly once
4570-
assert(list->ob_item[index] == NULL);
4571-
#endif
45724571
list->ob_item[index] = item;
45734572
return 0;
45744573
}
@@ -4612,6 +4611,9 @@ PyListBuilder_Extend(PyListBuilder *builder, PyObject *iterable)
46124611
PyListObject *self = _PyList_CAST(builder);
46134612
assert(_PyObject_CAST(self) != iterable);
46144613

4614+
#if _PyListBuilder_INIT_ITEMS
4615+
Py_ssize_t old_allocated = self->allocated;
4616+
#endif
46154617
Py_ssize_t old_size = Py_SIZE(self);
46164618
int res;
46174619
if (PyList_CheckExact(iterable)) {
@@ -4654,6 +4656,16 @@ PyListBuilder_Extend(PyListBuilder *builder, PyObject *iterable)
46544656
res = list_extend_iter_lock_held(self, iterable);
46554657
}
46564658

4659+
#if _PyListBuilder_INIT_ITEMS
4660+
Py_ssize_t new_size = Py_SIZE(self);
4661+
// Check if the list has been resized
4662+
if (old_allocated < self->allocated && new_size < self->allocated) {
4663+
// Initialize newly allocated items to NULL
4664+
memset(&self->ob_item[new_size], 0,
4665+
(self->allocated - new_size) * sizeof(PyObject*));
4666+
}
4667+
#endif
4668+
46574669
if (res < 0) {
46584670
// On error, reset the builder to its previous state:
46594671
// discard added items
@@ -4685,9 +4697,18 @@ listbuilder_extend_array(PyListBuilder *builder,
46854697
return 0;
46864698
}
46874699

4700+
#ifdef Py_DEBUG
4701+
for (Py_ssize_t index = 0; index < nitem; index++) {
4702+
if (items[index] == NULL) {
4703+
PyErr_Format(PyExc_SystemError, "array item #%zd is NULL", index);
4704+
return -1;
4705+
}
4706+
}
4707+
#endif
4708+
46884709
PyListObject *list = _PyList_CAST(builder);
4689-
Py_ssize_t size = Py_SIZE(list);
4690-
Py_ssize_t new_size = size + nitem;
4710+
Py_ssize_t old_size = Py_SIZE(list);
4711+
Py_ssize_t new_size = old_size + nitem;
46914712
if (new_size > list->allocated) {
46924713
if (listbuilder_resize(builder, new_size) < 0) {
46934714
if (steal) {
@@ -4702,7 +4723,7 @@ listbuilder_extend_array(PyListBuilder *builder,
47024723
Py_SET_SIZE(list, new_size);
47034724
}
47044725

4705-
PyObject **list_items = &list->ob_item[size];
4726+
PyObject **list_items = &list->ob_item[old_size];
47064727
if (steal) {
47074728
memcpy(list_items, items, nitem * sizeof(PyObject*));
47084729
}

0 commit comments

Comments
 (0)