Skip to content

Commit ec21940

Browse files
committed
hold strong reference before PyNumber_AsSsize_t()/PyObject_GetIter()
runs arbitrary Python
1 parent fb0276c commit ec21940

1 file changed

Lines changed: 40 additions & 13 deletions

File tree

‎Modules/itertoolsmodule.c‎

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,11 +1630,18 @@ islice_new_impl(PyTypeObject *type, PyObject *iterable, PyObject *start,
16301630
PyObject *stop, PyObject *step)
16311631
/*[clinic end generated code: output=9291712502695467 input=33c4713c4aa2d211]*/
16321632
{
1633-
PyObject *it;
1633+
PyObject *it = NULL;
16341634
Py_ssize_t start_val=0, stop_val=-1, step_val=1;
16351635
Py_ssize_t numargs = (stop == NULL) ? 2 : ((step == NULL) ? 3 : 4);
16361636
isliceobject *lz;
16371637

1638+
/* vectorcall passes borrowed references; hold strong references before
1639+
PyNumber_AsSsize_t() or PyObject_GetIter() can execute arbitrary Python code. */
1640+
Py_XINCREF(iterable);
1641+
Py_XINCREF(start);
1642+
Py_XINCREF(stop);
1643+
Py_XINCREF(step);
1644+
16381645
if (numargs == 2) {
16391646
if (start != Py_None) {
16401647
stop_val = PyNumber_AsSsize_t(start, PyExc_OverflowError);
@@ -1644,7 +1651,7 @@ islice_new_impl(PyTypeObject *type, PyObject *iterable, PyObject *start,
16441651
PyErr_SetString(PyExc_ValueError,
16451652
"Stop argument for islice() must be None or "
16461653
"an integer: 0 <= x <= sys.maxsize.");
1647-
return NULL;
1654+
goto error;
16481655
}
16491656
}
16501657
} else {
@@ -1660,15 +1667,15 @@ islice_new_impl(PyTypeObject *type, PyObject *iterable, PyObject *start,
16601667
PyErr_SetString(PyExc_ValueError,
16611668
"Stop argument for islice() must be None or "
16621669
"an integer: 0 <= x <= sys.maxsize.");
1663-
return NULL;
1670+
goto error;
16641671
}
16651672
}
16661673
}
16671674
if (start_val<0 || stop_val<-1) {
16681675
PyErr_SetString(PyExc_ValueError,
16691676
"Indices for islice() must be None or "
16701677
"an integer: 0 <= x <= sys.maxsize.");
1671-
return NULL;
1678+
goto error;
16721679
}
16731680

16741681
if (step != NULL) {
@@ -1680,27 +1687,38 @@ islice_new_impl(PyTypeObject *type, PyObject *iterable, PyObject *start,
16801687
if (step_val<1) {
16811688
PyErr_SetString(PyExc_ValueError,
16821689
"Step for islice() must be a positive integer or None.");
1683-
return NULL;
1690+
goto error;
16841691
}
16851692

16861693
/* Get iterator. */
16871694
it = PyObject_GetIter(iterable);
16881695
if (it == NULL)
1689-
return NULL;
1696+
goto error;
16901697

16911698
/* create isliceobject structure */
16921699
lz = (isliceobject *)type->tp_alloc(type, 0);
1693-
if (lz == NULL) {
1694-
Py_DECREF(it);
1695-
return NULL;
1696-
}
1700+
if (lz == NULL)
1701+
goto error;
16971702
lz->it = it;
1703+
it = NULL;
16981704
lz->next = start_val;
16991705
lz->stop = stop_val;
17001706
lz->step = step_val;
17011707
lz->cnt = 0L;
17021708

1709+
Py_DECREF(iterable);
1710+
Py_XDECREF(start);
1711+
Py_XDECREF(stop);
1712+
Py_XDECREF(step);
17031713
return (PyObject *)lz;
1714+
1715+
error:
1716+
Py_XDECREF(it);
1717+
Py_XDECREF(iterable);
1718+
Py_XDECREF(start);
1719+
Py_XDECREF(stop);
1720+
Py_XDECREF(step);
1721+
return NULL;
17041722
}
17051723

17061724
static void
@@ -3768,11 +3786,15 @@ repeat_new_impl(PyTypeObject *type, PyObject *element, PyObject *times)
37683786
repeatobject *ro;
37693787
Py_ssize_t cnt = -1;
37703788

3789+
/* vectorcall passes borrowed references; hold them before PyNumber_AsSsize_t() can
3790+
execute arbitrary Python code. */
3791+
Py_INCREF(element);
3792+
37713793
if (times != NULL) {
37723794
/* Does user supply times argument? */
37733795
cnt = PyNumber_AsSsize_t(times, PyExc_OverflowError);
37743796
if (cnt == -1 && PyErr_Occurred()) {
3775-
return NULL;
3797+
goto error;
37763798
}
37773799
if (cnt < 0) {
37783800
cnt = 0;
@@ -3781,10 +3803,15 @@ repeat_new_impl(PyTypeObject *type, PyObject *element, PyObject *times)
37813803

37823804
ro = (repeatobject *)type->tp_alloc(type, 0);
37833805
if (ro == NULL)
3784-
return NULL;
3785-
ro->element = Py_NewRef(element);
3806+
goto error;
3807+
ro->element = element; /* transfer the reference taken above */
37863808
ro->cnt = cnt;
37873809
return (PyObject *)ro;
3810+
3811+
error:
3812+
Py_DECREF(element);
3813+
Py_XDECREF(times);
3814+
return NULL;
37883815
}
37893816

37903817
static void

0 commit comments

Comments
 (0)