diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2023-12-04 11:47:55 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-04 11:47:55 (GMT) |
commit | 6ca9d3e0173c38e2eac50367b187d4c1d43f9892 (patch) | |
tree | 44dbb6b789c303c9c04f22cc68b99db86e4955be /Modules | |
parent | c74e9fb18917ceb287c3ed5be5d0c2a16a646a99 (diff) | |
download | cpython-6ca9d3e0173c38e2eac50367b187d4c1d43f9892.zip cpython-6ca9d3e0173c38e2eac50367b187d4c1d43f9892.tar.gz cpython-6ca9d3e0173c38e2eac50367b187d4c1d43f9892.tar.bz2 |
gh-109786: Fix leaks and crash when re-enter itertools.pairwise.__next__() (GH-109788)
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/itertoolsmodule.c | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index 4ed34aa..ab99fa4 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -330,21 +330,30 @@ pairwise_next(pairwiseobject *po) return NULL; } if (old == NULL) { - po->old = old = (*Py_TYPE(it)->tp_iternext)(it); + old = (*Py_TYPE(it)->tp_iternext)(it); + Py_XSETREF(po->old, old); if (old == NULL) { Py_CLEAR(po->it); return NULL; } + it = po->it; + if (it == NULL) { + Py_CLEAR(po->old); + return NULL; + } } + Py_INCREF(old); new = (*Py_TYPE(it)->tp_iternext)(it); if (new == NULL) { Py_CLEAR(po->it); Py_CLEAR(po->old); + Py_DECREF(old); return NULL; } /* Future optimization: Reuse the result tuple as we do in enumerate() */ result = PyTuple_Pack(2, old, new); - Py_SETREF(po->old, new); + Py_XSETREF(po->old, new); + Py_DECREF(old); return result; } |