diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2024-03-18 12:40:34 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-18 12:40:34 (GMT) |
commit | 1c0c6c91afcb765f30bbb4e3d80a50e31d7adfcc (patch) | |
tree | dd4fae1357987c5ea9d7be6e117e302015823d0d | |
parent | 25243b1461e524560639ebe54bab9b689b6cc31e (diff) | |
download | cpython-1c0c6c91afcb765f30bbb4e3d80a50e31d7adfcc.zip cpython-1c0c6c91afcb765f30bbb4e3d80a50e31d7adfcc.tar.gz cpython-1c0c6c91afcb765f30bbb4e3d80a50e31d7adfcc.tar.bz2 |
[3.12] gh-115874: Don't use module state in teedataobject tp_dealloc (GH-116204) (#116955)
(cherry picked from commit e2fcaf19d302b05d3466807bad0a61f39db2a51b)
Co-authored-by: Erlend E. Aasland <erlend@python.org>
Co-authored-by: Brandt Bucher <brandtbucher@microsoft.com>
-rw-r--r-- | Lib/test/test_itertools.py | 10 | ||||
-rw-r--r-- | Modules/itertoolsmodule.c | 8 |
2 files changed, 12 insertions, 6 deletions
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py index 705e880..3d20e70 100644 --- a/Lib/test/test_itertools.py +++ b/Lib/test/test_itertools.py @@ -1,7 +1,7 @@ import doctest import unittest from test import support -from test.support import threading_helper +from test.support import threading_helper, script_helper from itertools import * import weakref from decimal import Decimal @@ -1695,6 +1695,14 @@ class TestBasicOps(unittest.TestCase): self.pickletest(proto, a, compare=ans) self.pickletest(proto, b, compare=ans) + def test_tee_dealloc_segfault(self): + # gh-115874: segfaults when accessing module state in tp_dealloc. + script = ( + "import typing, copyreg, itertools; " + "copyreg.buggy_tee = itertools.tee(())" + ) + script_helper.assert_python_ok("-c", script) + # Issue 13454: Crash when deleting backward iterator from tee() def test_tee_del_backward(self): forward, backward = tee(repeat(None, 20000000)) diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index a8eee72..d42f9dd 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -810,10 +810,9 @@ teedataobject_traverse(teedataobject *tdo, visitproc visit, void * arg) } static void -teedataobject_safe_decref(PyObject *obj, PyTypeObject *tdo_type) +teedataobject_safe_decref(PyObject *obj) { - while (obj && Py_IS_TYPE(obj, tdo_type) && - Py_REFCNT(obj) == 1) { + while (obj && Py_REFCNT(obj) == 1) { PyObject *nextlink = ((teedataobject *)obj)->nextlink; ((teedataobject *)obj)->nextlink = NULL; Py_SETREF(obj, nextlink); @@ -832,8 +831,7 @@ teedataobject_clear(teedataobject *tdo) Py_CLEAR(tdo->values[i]); tmp = tdo->nextlink; tdo->nextlink = NULL; - itertools_state *state = get_module_state_by_cls(Py_TYPE(tdo)); - teedataobject_safe_decref(tmp, state->teedataobject_type); + teedataobject_safe_decref(tmp); return 0; } |