Skip to content

Commit 36fbb73

Browse files
committed
fix one visible and several possible refleaks in rangeobject.c
In some cases, the code was just reordered to allow for less decrefing.
1 parent 91799ae commit 36fbb73

1 file changed

Lines changed: 17 additions & 20 deletions

File tree

Objects/rangeobject.c

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ int_range_iter(long start, long stop, long step)
533533
it->step = step;
534534
ulen = get_len_of_range(start, stop, step);
535535
if (ulen > (unsigned long)LONG_MAX) {
536+
Py_DECREF(it);
536537
PyErr_SetString(PyExc_OverflowError,
537538
"range too large to represent as a range_iterator");
538539
return NULL;
@@ -584,16 +585,14 @@ longrangeiter_next(longrangeiterobject *r)
584585
if (!one)
585586
return NULL;
586587

587-
product = PyNumber_Multiply(r->index, r->step);
588-
if (!product) {
589-
Py_DECREF(one);
590-
return NULL;
591-
}
592-
593588
new_index = PyNumber_Add(r->index, one);
594589
Py_DECREF(one);
595-
if (!new_index) {
596-
Py_DECREF(product);
590+
if (!new_index)
591+
return NULL;
592+
593+
product = PyNumber_Multiply(r->index, r->step);
594+
if (!product) {
595+
Py_DECREF(new_index);
597596
return NULL;
598597
}
599598

@@ -603,6 +602,9 @@ longrangeiter_next(longrangeiterobject *r)
603602
Py_DECREF(r->index);
604603
r->index = new_index;
605604
}
605+
else {
606+
Py_DECREF(new_index);
607+
}
606608

607609
return result;
608610
}
@@ -781,6 +783,9 @@ range_reverse(PyObject *seq)
781783
if (!len)
782784
goto create_failure;
783785

786+
/* Steal reference to len. */
787+
it->len = len;
788+
784789
one = PyLong_FromLong(1);
785790
if (!one)
786791
goto create_failure;
@@ -802,24 +807,16 @@ range_reverse(PyObject *seq)
802807
goto create_failure;
803808

804809
it->step = PyNumber_Negative(range->step);
805-
if (!it->step) {
806-
Py_DECREF(it->start);
810+
if (!it->step)
807811
goto create_failure;
808-
}
809-
810-
/* Steal reference to len. */
811-
it->len = len;
812812

813813
it->index = PyLong_FromLong(0);
814-
if (!it->index) {
815-
Py_DECREF(it);
816-
return NULL;
817-
}
814+
if (!it->index)
815+
goto create_failure;
818816

819817
return (PyObject *)it;
820818

821819
create_failure:
822-
Py_XDECREF(len);
823-
PyObject_Del(it);
820+
Py_DECREF(it);
824821
return NULL;
825822
}

0 commit comments

Comments
 (0)