diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-03-01-10-15-06.gh-issue-145376.lG5u1a.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-03-01-10-15-06.gh-issue-145376.lG5u1a.rst new file mode 100644 index 00000000000000..a5a6908757e458 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-03-01-10-15-06.gh-issue-145376.lG5u1a.rst @@ -0,0 +1 @@ +Fix reference leaks in various unusual error scenarios. diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index ff0e2fd2b3569d..50e93c348afed5 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -3538,8 +3538,10 @@ count_nextlong(countobject *lz) if (long_cnt == NULL) { /* Switch to slow_mode */ long_cnt = PyLong_FromSsize_t(PY_SSIZE_T_MAX); - if (long_cnt == NULL) + if (long_cnt == NULL) { return NULL; + } + lz->long_cnt = long_cnt; } assert(lz->cnt == PY_SSIZE_T_MAX && long_cnt != NULL); diff --git a/Objects/enumobject.c b/Objects/enumobject.c index 814ce4f919514b..8feb074f66a0cd 100644 --- a/Objects/enumobject.c +++ b/Objects/enumobject.c @@ -184,6 +184,7 @@ increment_longindex_lock_held(enumobject *en) if (next_index == NULL) { return NULL; } + en->en_longindex = next_index; } assert(next_index != NULL); PyObject *stepped_up = PyNumber_Add(next_index, en->one); diff --git a/Objects/listobject.c b/Objects/listobject.c index 4a98c8e54ab03f..527b970af24bb2 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -4270,7 +4270,9 @@ listiter_reduce_general(void *_it, int forward) } /* empty iterator, create an empty list */ list = PyList_New(0); - if (list == NULL) + if (list == NULL) { + Py_DECREF(iter); return NULL; + } return Py_BuildValue("N(N)", iter, list); }