summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2023-12-04 12:20:19 (GMT)
committerGitHub <noreply@github.com>2023-12-04 12:20:19 (GMT)
commite3d380ded6efd25d60a21b7eb913142e86153e33 (patch)
treeffb839eb0e0e46254497aa2ff7fdcc8fe675a736 /Modules
parent28afd8ddad8088dc3b251f18020b53b277205e5f (diff)
downloadcpython-e3d380ded6efd25d60a21b7eb913142e86153e33.zip
cpython-e3d380ded6efd25d60a21b7eb913142e86153e33.tar.gz
cpython-e3d380ded6efd25d60a21b7eb913142e86153e33.tar.bz2
[3.11] gh-109786: Fix leaks and crash when re-enter itertools.pairwise.__next__() (GH-109788) (GH-112700)
(cherry picked from commit 6ca9d3e0173c38e2eac50367b187d4c1d43f9892) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Modules')
-rw-r--r--Modules/itertoolsmodule.c13
1 files changed, 11 insertions, 2 deletions
diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c
index d5cdfc5..7a02a0d 100644
--- a/Modules/itertoolsmodule.c
+++ b/Modules/itertoolsmodule.c
@@ -119,21 +119,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;
}