X Tutup
Skip to content

Commit 886bc6e

Browse files
gh-145376: Fix various reference leaks in Objects/ and Modules/ (#145385)
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
1 parent 8060aa5 commit 886bc6e

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

Modules/itertoolsmodule.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3531,23 +3531,26 @@ count_traverse(PyObject *op, visitproc visit, void *arg)
35313531
static PyObject *
35323532
count_nextlong(countobject *lz)
35333533
{
3534-
PyObject *long_cnt;
3535-
PyObject *stepped_up;
3536-
3537-
long_cnt = lz->long_cnt;
3538-
if (long_cnt == NULL) {
3534+
if (lz->long_cnt == NULL) {
35393535
/* Switch to slow_mode */
3540-
long_cnt = PyLong_FromSsize_t(PY_SSIZE_T_MAX);
3541-
if (long_cnt == NULL)
3536+
lz->long_cnt = PyLong_FromSsize_t(PY_SSIZE_T_MAX);
3537+
if (lz->long_cnt == NULL) {
35423538
return NULL;
3539+
}
35433540
}
3544-
assert(lz->cnt == PY_SSIZE_T_MAX && long_cnt != NULL);
3541+
assert(lz->cnt == PY_SSIZE_T_MAX && lz->long_cnt != NULL);
3542+
3543+
// We hold one reference to "result" (a.k.a. the old value of
3544+
// lz->long_cnt); we'll either return it or keep it in lz->long_cnt.
3545+
PyObject *result = lz->long_cnt;
35453546

3546-
stepped_up = PyNumber_Add(long_cnt, lz->long_step);
3547-
if (stepped_up == NULL)
3547+
PyObject *stepped_up = PyNumber_Add(result, lz->long_step);
3548+
if (stepped_up == NULL) {
35483549
return NULL;
3550+
}
35493551
lz->long_cnt = stepped_up;
3550-
return long_cnt;
3552+
3553+
return result;
35513554
}
35523555

35533556
static PyObject *

Objects/enumobject.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,16 @@ enum_traverse(PyObject *op, visitproc visit, void *arg)
178178
static inline PyObject *
179179
increment_longindex_lock_held(enumobject *en)
180180
{
181-
PyObject *next_index = en->en_longindex;
182-
if (next_index == NULL) {
183-
next_index = PyLong_FromSsize_t(PY_SSIZE_T_MAX);
184-
if (next_index == NULL) {
181+
if (en->en_longindex == NULL) {
182+
en->en_longindex = PyLong_FromSsize_t(PY_SSIZE_T_MAX);
183+
if (en->en_longindex == NULL) {
185184
return NULL;
186185
}
187186
}
188-
assert(next_index != NULL);
187+
assert(en->en_longindex != NULL);
188+
// We hold one reference to "next_index" (a.k.a. the old value of
189+
// en->en_longindex); we'll either return it or keep it in en->en_longindex
190+
PyObject *next_index = en->en_longindex;
189191
PyObject *stepped_up = PyNumber_Add(next_index, en->one);
190192
if (stepped_up == NULL) {
191193
return NULL;

Objects/listobject.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4294,7 +4294,9 @@ listiter_reduce_general(void *_it, int forward)
42944294
}
42954295
/* empty iterator, create an empty list */
42964296
list = PyList_New(0);
4297-
if (list == NULL)
4297+
if (list == NULL) {
4298+
Py_DECREF(iter);
42984299
return NULL;
4300+
}
42994301
return Py_BuildValue("N(N)", iter, list);
43004302
}

0 commit comments

Comments
 (0)
X Tutup