From 324b93295fd81133d2dd597c3e3a0333f6c9d95b Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Mon, 5 Jul 2021 15:34:01 -0700 Subject: [PATCH] bpo-44563: Fix error handling in tee.fromiterable() (GH-27020) (GH-27042) In debug build failed tee.fromiterable() corrupted the linked list of all GC objects. (cherry picked from commit f64de53ff01e734d48d1d42195443d7d1646f220) Co-authored-by: Serhiy Storchaka --- Modules/itertoolsmodule.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index 95ef8d79a16533..bc5382af7e98e1 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -729,7 +729,7 @@ static PyObject * tee_fromiterable(PyObject *iterable) { teeobject *to; - PyObject *it = NULL; + PyObject *it; it = PyObject_GetIter(iterable); if (it == NULL) @@ -739,21 +739,22 @@ tee_fromiterable(PyObject *iterable) goto done; } - to = PyObject_GC_New(teeobject, &tee_type); - if (to == NULL) - goto done; - to->dataobj = (teedataobject *)teedataobject_newinternal(it); - if (!to->dataobj) { - PyObject_GC_Del(to); + PyObject *dataobj = teedataobject_newinternal(it); + if (!dataobj) { to = NULL; goto done; } - + to = PyObject_GC_New(teeobject, &tee_type); + if (to == NULL) { + Py_DECREF(dataobj); + goto done; + } + to->dataobj = (teedataobject *)dataobj; to->index = 0; to->weakreflist = NULL; PyObject_GC_Track(to); done: - Py_XDECREF(it); + Py_DECREF(it); return (PyObject *)to; }