summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorSergey Fedoseev <fedoseev.sergey@gmail.com>2018-10-20 05:20:39 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2018-10-20 05:20:39 (GMT)
commita5259fb05d03f4871837c14fed704541a20896c0 (patch)
treee8d8b3310f9f3df3c316f683c8fad3c547fa2ce2 /Objects
parent8c9fd9c91ba748df68a11e3bf216fa158314c9b5 (diff)
downloadcpython-a5259fb05d03f4871837c14fed704541a20896c0.zip
cpython-a5259fb05d03f4871837c14fed704541a20896c0.tar.gz
cpython-a5259fb05d03f4871837c14fed704541a20896c0.tar.bz2
bpo-34574: Prevent OrderedDict iterators from exhaustion during pickling. (GH-9051)
Diffstat (limited to 'Objects')
-rw-r--r--Objects/odictobject.c37
1 files changed, 9 insertions, 28 deletions
diff --git a/Objects/odictobject.c b/Objects/odictobject.c
index 47e77b6..67c3674 100644
--- a/Objects/odictobject.c
+++ b/Objects/odictobject.c
@@ -1805,38 +1805,19 @@ PyDoc_STRVAR(reduce_doc, "Return state information for pickling");
static PyObject *
odictiter_reduce(odictiterobject *di)
{
- PyObject *list, *iter;
-
- list = PyList_New(0);
- if (!list)
- return NULL;
+ /* copy the iterator state */
+ odictiterobject tmp = *di;
+ Py_XINCREF(tmp.di_odict);
+ Py_XINCREF(tmp.di_current);
/* iterate the temporary into a list */
- for(;;) {
- PyObject *element = odictiter_iternext(di);
- if (element) {
- if (PyList_Append(list, element)) {
- Py_DECREF(element);
- Py_DECREF(list);
- return NULL;
- }
- Py_DECREF(element);
- }
- else {
- /* done iterating? */
- break;
- }
- }
- if (PyErr_Occurred()) {
- Py_DECREF(list);
- return NULL;
- }
- iter = _PyObject_GetBuiltin("iter");
- if (iter == NULL) {
- Py_DECREF(list);
+ PyObject *list = PySequence_List((PyObject*)&tmp);
+ Py_XDECREF(tmp.di_odict);
+ Py_XDECREF(tmp.di_current);
+ if (list == NULL) {
return NULL;
}
- return Py_BuildValue("N(N)", iter, list);
+ return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
}
static PyMethodDef odictiter_methods[] = {