Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/test/test_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def test_varargs13_kw(self):
self.assertRaisesRegex(TypeError, msg, classmethod, func=id)

def test_varargs14_kw(self):
msg = r"^product\(\) takes at most 1 keyword argument \(2 given\)$"
msg = r"^product\(\) got an unexpected keyword argument 'foo'$"
self.assertRaisesRegex(TypeError, msg,
itertools.product, 0, repeat=1, foo=2)

Expand Down
188 changes: 187 additions & 1 deletion Modules/clinic/itertoolsmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

105 changes: 49 additions & 56 deletions Modules/itertoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2089,38 +2089,29 @@ struct productobject {

#define productobject_CAST(op) ((productobject *)(op))

/*[clinic input]
@vectorcall
@classmethod
itertools.product.__new__ as product_new
*iterables: array
repeat: Py_ssize_t(allow_negative=False) = 1
[clinic start generated code]*/

static PyObject *
product_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
product_new_impl(PyTypeObject *type, PyObject * const *iterables,
Py_ssize_t iterables_length, Py_ssize_t repeat)
/*[clinic end generated code: output=b0881d40e6be4c00 input=189ab566d53a8ea9]*/
{
productobject *lz;
Py_ssize_t nargs, npools, repeat=1;
Py_ssize_t nargs, npools;
PyObject *pools = NULL;
Py_ssize_t *indices = NULL;
Py_ssize_t i;

if (kwds != NULL) {
char *kwlist[] = {"repeat", 0};
PyObject *tmpargs = PyTuple_New(0);
if (tmpargs == NULL)
return NULL;
if (!PyArg_ParseTupleAndKeywords(tmpargs, kwds, "|n:product",
kwlist, &repeat)) {
Py_DECREF(tmpargs);
return NULL;
}
Py_DECREF(tmpargs);
if (repeat < 0) {
PyErr_SetString(PyExc_ValueError,
"repeat argument cannot be negative");
return NULL;
}
}

assert(PyTuple_CheckExact(args));
if (repeat == 0) {
nargs = 0;
} else {
nargs = PyTuple_GET_SIZE(args);
nargs = iterables_length;
if ((size_t)nargs > PY_SSIZE_T_MAX/sizeof(Py_ssize_t)/repeat) {
PyErr_SetString(PyExc_OverflowError, "repeat argument too large");
return NULL;
Expand All @@ -2139,7 +2130,7 @@ product_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
goto error;

for (i=0; i < nargs ; ++i) {
PyObject *item = PyTuple_GET_ITEM(args, i);
PyObject *item = iterables[i];
PyObject *pool = PySequence_Tuple(item);
if (pool == NULL)
goto error;
Expand Down Expand Up @@ -2328,6 +2319,7 @@ static PyType_Slot product_slots[] = {
{Py_tp_iternext, product_next},
{Py_tp_methods, product_methods},
{Py_tp_new, product_new},
{Py_tp_vectorcall, product_vectorcall},
{Py_tp_free, PyObject_GC_Del},
{0, NULL},
};
Expand Down Expand Up @@ -3881,53 +3873,47 @@ struct ziplongestobject {

#define ziplongestobject_CAST(op) ((ziplongestobject *)(op))

/*[clinic input]
@vectorcall
@classmethod
itertools.zip_longest.__new__ as zip_longest_new
*iterables: array
fillvalue: object = None
[clinic start generated code]*/

static PyObject *
zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
zip_longest_new_impl(PyTypeObject *type, PyObject * const *iterables,
Py_ssize_t iterables_length, PyObject *fillvalue)
/*[clinic end generated code: output=9f45c950feb8e18d input=2c4cc9910ee8dab7]*/
{
ziplongestobject *lz;
Py_ssize_t i;
PyObject *ittuple; /* tuple of iterators */
PyObject *result;
PyObject *fillvalue = Py_None;
Py_ssize_t tuplesize;
PyObject *ittuple = NULL; /* tuple of iterators */
PyObject *result = NULL;
Py_ssize_t tuplesize = iterables_length;

if (kwds != NULL && PyDict_CheckExact(kwds) && PyDict_GET_SIZE(kwds) > 0) {
fillvalue = NULL;
if (PyDict_GET_SIZE(kwds) == 1) {
fillvalue = PyDict_GetItemWithError(kwds, &_Py_ID(fillvalue));
}
if (fillvalue == NULL) {
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError,
"zip_longest() got an unexpected keyword argument");
}
return NULL;
}
}

/* args must be a tuple */
assert(PyTuple_Check(args));
tuplesize = PyTuple_GET_SIZE(args);
/* vectorcall passes borrowed references; hold a strong reference before
PyObject_GetIter() can run arbitrary code. */
Py_INCREF(fillvalue);

/* obtain iterators */
ittuple = PyTuple_New(tuplesize);
if (ittuple == NULL)
return NULL;
if (ittuple == NULL) {
goto error;
}
for (i=0; i < tuplesize; i++) {
PyObject *item = PyTuple_GET_ITEM(args, i);
PyObject *item = iterables[i];
PyObject *it = PyObject_GetIter(item);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can run arbitrary code, need to make sure to turn fillvalue into a strong reference from a borrowed reference:

import functools, itertools
class Evil:
    def __iter__(self):
        p.keywords.clear()          # drops the only ref to fillvalue
        return iter([1])
p = functools.partial(itertools.zip_longest, fillvalue=object())
list(p(Evil(), [1, 2, 3]))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added strong reference of fillvalue; route early returns through error; release fillvalue, ittuple, and result at error.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rhettinger: ^ See the code above which segfaults without the additional reference counting

(functools.partial also has refcount bugs, which is what makes this visible in python. Generally it should be only visible in C API calling C API)

if (it == NULL) {
Py_DECREF(ittuple);
return NULL;
goto error;
}
PyTuple_SET_ITEM(ittuple, i, it);
}

/* create a result holder */
result = PyTuple_New(tuplesize);
if (result == NULL) {
Py_DECREF(ittuple);
return NULL;
goto error;
}
for (i=0 ; i < tuplesize ; i++) {
Py_INCREF(Py_None);
Expand All @@ -3937,16 +3923,22 @@ zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
/* create ziplongestobject structure */
lz = (ziplongestobject *)type->tp_alloc(type, 0);
if (lz == NULL) {
Py_DECREF(ittuple);
Py_DECREF(result);
return NULL;
goto error;
}
lz->ittuple = ittuple;
ittuple = NULL;
lz->tuplesize = tuplesize;
lz->numactive = tuplesize;
lz->result = result;
lz->fillvalue = Py_NewRef(fillvalue);
result = NULL;
lz->fillvalue = fillvalue;
return (PyObject *)lz;

error:
Py_XDECREF(ittuple);
Py_XDECREF(result);
Py_DECREF(fillvalue);
return NULL;
}

static void
Expand Down Expand Up @@ -4071,6 +4063,7 @@ static PyType_Slot ziplongest_slots[] = {
{Py_tp_iter, PyObject_SelfIter},
{Py_tp_iternext, zip_longest_next},
{Py_tp_new, zip_longest_new},
{Py_tp_vectorcall, zip_longest_vectorcall},
{Py_tp_free, PyObject_GC_Del},
{0, NULL},
};
Expand Down
Loading