Skip to content

Commit 7e7ff8b

Browse files
authored
gh-158660: Check for NULL items in PyListBuilder_Extend() (#162962)
* Test PyListBuilder_Append(NULL) and PyListBuilder_AppendSteal(NULL). * In debug mode, PyListBuilder_Extend() raises SystemError if an item is NULL. * Test PyListBuilder_Extend() on a tuple which contains a NULL item. * _PyDict_Next(): add assertions to make sure that key and value are not NULL. * Declare _list_extend_impl() as static inline. * Add the function name in SystemError error messages.
1 parent d8048e9 commit 7e7ff8b

4 files changed

Lines changed: 100 additions & 8 deletions

File tree

‎Lib/test/test_capi/test_list.py‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,15 @@ def check_append(self, method):
408408
append(item)
409409
self.list_equal(builder.finish(), items)
410410

411+
# Methods only check for NULL in debug mode
412+
if support.Py_DEBUG:
413+
builder = self.create_builder(0)
414+
append = getattr(builder, method)
415+
with self.assertRaises(SystemError):
416+
append(NULL)
417+
append("after")
418+
self.list_equal(builder.finish(), ["after"])
419+
411420
def test_append(self):
412421
# Test PyListBuilder_Append()
413422
self.check_append('append')
@@ -435,7 +444,9 @@ def check_extend(self, method):
435444
extend(items)
436445
self.list_equal(builder.finish(), items)
437446

438-
if method != 'extend' and support.Py_DEBUG:
447+
# Methods only check for NULL items in debug mode
448+
if support.Py_DEBUG:
449+
# test tuple with a NULL item
439450
items = (object(), object(), NULL)
440451
builder = self.create_builder(0)
441452
extend = getattr(builder, method)

‎Modules/_testlimitedcapi/list.c‎

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,9 @@ builder_append_steal(PyObject *self_raw, PyObject *item)
261261
if (builder_check(self) < 0) {
262262
return NULL;
263263
}
264+
NULLABLE(item);
264265

265-
if (PyListBuilder_AppendSteal(self->builder, Py_NewRef(item)) < 0) {
266+
if (PyListBuilder_AppendSteal(self->builder, Py_XNewRef(item)) < 0) {
266267
return NULL;
267268
}
268269
Py_RETURN_NONE;
@@ -279,7 +280,45 @@ builder_extend(PyObject *self_raw, PyObject *iterable)
279280
}
280281
NULLABLE(iterable);
281282

282-
if (PyListBuilder_Extend(self->builder, iterable) < 0) {
283+
if (iterable == NULL) {
284+
// Crash at runtime!
285+
if (PyListBuilder_Extend(self->builder, NULL) < 0) {
286+
return NULL;
287+
}
288+
Py_RETURN_NONE;
289+
}
290+
291+
if (PyTuple_Check(iterable)) {
292+
// Create a new tuple to replace None with NULL
293+
Py_ssize_t len = PyTuple_Size(iterable);
294+
PyObject *tuple = PyTuple_New(len);
295+
if (tuple == NULL) {
296+
PyErr_NoMemory();
297+
return NULL;
298+
}
299+
for (Py_ssize_t i = 0; i < len; i++) {
300+
PyObject *item = PyTuple_GetItem(iterable, i); // borrowed ref
301+
assert(item != NULL);
302+
if (item != Py_None) {
303+
item = Py_NewRef(item);
304+
}
305+
else {
306+
item = NULL;
307+
}
308+
if (PyTuple_SetItem(tuple, i, item) < 0) {
309+
Py_DECREF(tuple);
310+
return NULL;
311+
}
312+
}
313+
iterable = tuple;
314+
}
315+
else {
316+
Py_INCREF(iterable);
317+
}
318+
319+
int res = PyListBuilder_Extend(self->builder, iterable);
320+
Py_DECREF(iterable);
321+
if (res < 0) {
283322
return NULL;
284323
}
285324
Py_RETURN_NONE;

‎Objects/dictobject.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3328,6 +3328,10 @@ _PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
33283328
value = entry_ptr->me_value;
33293329
}
33303330
}
3331+
3332+
// On success, key and value cannot be NULL
3333+
assert(key != NULL);
3334+
assert(value != NULL);
33313335
*ppos = i+1;
33323336
if (pkey)
33333337
*pkey = key;

‎Objects/listobject.c‎

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "pycore_modsupport.h" // _PyArg_NoKwnames()
1313
#include "pycore_object.h" // _PyObject_GC_TRACK(), _PyDebugAllocatorStats()
1414
#include "pycore_pyatomic_ft_wrappers.h"
15-
#include "pycore_setobject.h" // _PySet_NextEntry()
15+
#include "pycore_setobject.h" // _PySet_NextEntryRef()
1616
#include "pycore_stackref.h" // _Py_TryIncrefCompareStackRef()
1717
#include "pycore_tuple.h" // _PyTuple_FromArraySteal()
1818
#include "pycore_typeobject.h" // _Py_TYPE_VERSION_LIST
@@ -1346,6 +1346,14 @@ list_extend_fast(PyListObject *self, PyObject *iterable)
13461346
PyObject **dest = self->ob_item + m;
13471347
for (Py_ssize_t i = 0; i < n; i++) {
13481348
PyObject *o = src[i];
1349+
#ifdef Py_DEBUG
1350+
if (o == NULL) {
1351+
Py_SET_SIZE(self, m + i); // shrink the list
1352+
PyErr_Format(PyExc_SystemError,
1353+
"%s: item #%zd is NULL", __func__, i);
1354+
return -1;
1355+
}
1356+
#endif
13491357
FT_ATOMIC_STORE_PTR_RELEASE(dest[i], Py_NewRef(o));
13501358
}
13511359
return 0;
@@ -1459,6 +1467,8 @@ list_extend_set(PyListObject *self, PySetObject *other)
14591467
PyObject *key;
14601468
PyObject **dest = self->ob_item + m;
14611469
while (_PySet_NextEntryRef((PyObject *)other, &setpos, &key, &hash)) {
1470+
// On _PySet_NextEntryRef() success, key cannot be NULL
1471+
assert(key != NULL);
14621472
FT_ATOMIC_STORE_PTR_RELEASE(*dest, key);
14631473
dest++;
14641474
}
@@ -1486,6 +1496,8 @@ list_extend_dict(PyListObject *self, PyDictObject *dict, int which_item)
14861496
PyObject *keyvalue[2];
14871497
while (_PyDict_Next((PyObject *)dict, &pos, &keyvalue[0], &keyvalue[1], NULL)) {
14881498
PyObject *obj = keyvalue[which_item];
1499+
// On _PyDict_Next() success, key and value cannot be NULL
1500+
assert(obj != NULL);
14891501
Py_INCREF(obj);
14901502
FT_ATOMIC_STORE_PTR_RELEASE(*dest, obj);
14911503
dest++;
@@ -1514,6 +1526,9 @@ list_extend_dictitems(PyListObject *self, PyDictObject *dict)
15141526
Py_ssize_t i = 0;
15151527
PyObject *key, *value;
15161528
while (_PyDict_Next((PyObject *)dict, &pos, &key, &value, NULL)) {
1529+
// On _PyDict_Next() success, key and value cannot be NULL
1530+
assert(key != NULL);
1531+
assert(value != NULL);
15171532
PyObject *item = _PyTuple_FromPair(key, value);
15181533
if (item == NULL) {
15191534
Py_SET_SIZE(self, m + i);
@@ -1530,7 +1545,10 @@ list_extend_dictitems(PyListObject *self, PyDictObject *dict)
15301545

15311546
// Implementation of list.extend() and PyListBuilder_Extend().
15321547
// Force inlining to optimize "if (lock_self)" at build time.
1533-
static Py_ALWAYS_INLINE int
1548+
//
1549+
// In debug mode, the function raises SystemError if iterable contains
1550+
// a NULL item.
1551+
static inline Py_ALWAYS_INLINE int
15341552
_list_extend_impl(PyListObject *self, PyObject *iterable, const int lock_self)
15351553
{
15361554
// Special case:
@@ -1559,6 +1577,7 @@ _list_extend_impl(PyListObject *self, PyObject *iterable, const int lock_self)
15591577
}
15601578
}
15611579
else if (PyTuple_CheckExact(iterable)) {
1580+
// Similar to PyList_CheckExact() above, but don't lock iterable
15621581
if (lock_self) {
15631582
Py_BEGIN_CRITICAL_SECTION(self);
15641583
res = list_extend_lock_held(self, iterable);
@@ -4567,7 +4586,8 @@ PyListBuilder_Finish(PyListBuilder *builder)
45674586
PyObject **list_items = list->ob_item;
45684587
for (Py_ssize_t index = 0; index < size; index++) {
45694588
if (list_items[index] == NULL) {
4570-
PyErr_Format(PyExc_SystemError, "list item #%zd is NULL", index);
4589+
PyErr_Format(PyExc_SystemError,
4590+
"%s: list item #%zd is NULL", __func__, index);
45714591
Py_DECREF(list);
45724592
return NULL;
45734593
}
@@ -4608,9 +4628,17 @@ int
46084628
PyListBuilder_AppendSteal(PyListBuilder *builder, PyObject *item)
46094629
{
46104630
assert(listbuilder_check_consistency(builder));
4611-
assert(item != NULL);
46124631
PyListObject *list = _PyList_CAST(builder);
46134632

4633+
#ifdef Py_DEBUG
4634+
if (item == NULL) {
4635+
PyErr_Format(PyExc_SystemError, "%s: item is NULL", __func__);
4636+
return -1;
4637+
}
4638+
#else
4639+
assert(item != NULL);
4640+
#endif
4641+
46144642
Py_ssize_t index = Py_SIZE(list);
46154643
if (index < list->allocated) {
46164644
Py_SET_SIZE(list, index + 1);
@@ -4630,6 +4658,15 @@ PyListBuilder_AppendSteal(PyListBuilder *builder, PyObject *item)
46304658
int
46314659
PyListBuilder_Append(PyListBuilder *builder, PyObject *item)
46324660
{
4661+
#ifdef Py_DEBUG
4662+
if (item == NULL) {
4663+
PyErr_Format(PyExc_SystemError, "%s: item is NULL", __func__);
4664+
return -1;
4665+
}
4666+
#else
4667+
assert(item != NULL);
4668+
#endif
4669+
46334670
return PyListBuilder_AppendSteal(builder, Py_NewRef(item));
46344671
}
46354672

@@ -4714,7 +4751,8 @@ listbuilder_extend_array(PyListBuilder *builder,
47144751
#ifdef Py_DEBUG
47154752
for (Py_ssize_t index = 0; index < nitem; index++) {
47164753
if (items[index] == NULL) {
4717-
PyErr_Format(PyExc_SystemError, "array item #%zd is NULL", index);
4754+
PyErr_Format(PyExc_SystemError,
4755+
"%s: array item #%zd is NULL", __func__, index);
47184756
goto error;
47194757
}
47204758
}

0 commit comments

Comments
 (0)