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
46084628PyListBuilder_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)
46304658int
46314659PyListBuilder_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