Skip to content

Commit 1c0c6c9

Browse files
miss-islingtonerlend-aaslandbrandtbucher
authored
[3.12] gh-115874: Don't use module state in teedataobject tp_dealloc (GH-116204) (#116955)
(cherry picked from commit e2fcaf1) Co-authored-by: Erlend E. Aasland <[email protected]> Co-authored-by: Brandt Bucher <[email protected]>
1 parent 25243b1 commit 1c0c6c9

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

Lib/test/test_itertools.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import doctest
22
import unittest
33
from test import support
4-
from test.support import threading_helper
4+
from test.support import threading_helper, script_helper
55
from itertools import *
66
import weakref
77
from decimal import Decimal
@@ -1695,6 +1695,14 @@ def test_tee(self):
16951695
self.pickletest(proto, a, compare=ans)
16961696
self.pickletest(proto, b, compare=ans)
16971697

1698+
def test_tee_dealloc_segfault(self):
1699+
# gh-115874: segfaults when accessing module state in tp_dealloc.
1700+
script = (
1701+
"import typing, copyreg, itertools; "
1702+
"copyreg.buggy_tee = itertools.tee(())"
1703+
)
1704+
script_helper.assert_python_ok("-c", script)
1705+
16981706
# Issue 13454: Crash when deleting backward iterator from tee()
16991707
def test_tee_del_backward(self):
17001708
forward, backward = tee(repeat(None, 20000000))

Modules/itertoolsmodule.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -810,10 +810,9 @@ teedataobject_traverse(teedataobject *tdo, visitproc visit, void * arg)
810810
}
811811

812812
static void
813-
teedataobject_safe_decref(PyObject *obj, PyTypeObject *tdo_type)
813+
teedataobject_safe_decref(PyObject *obj)
814814
{
815-
while (obj && Py_IS_TYPE(obj, tdo_type) &&
816-
Py_REFCNT(obj) == 1) {
815+
while (obj && Py_REFCNT(obj) == 1) {
817816
PyObject *nextlink = ((teedataobject *)obj)->nextlink;
818817
((teedataobject *)obj)->nextlink = NULL;
819818
Py_SETREF(obj, nextlink);
@@ -832,8 +831,7 @@ teedataobject_clear(teedataobject *tdo)
832831
Py_CLEAR(tdo->values[i]);
833832
tmp = tdo->nextlink;
834833
tdo->nextlink = NULL;
835-
itertools_state *state = get_module_state_by_cls(Py_TYPE(tdo));
836-
teedataobject_safe_decref(tmp, state->teedataobject_type);
834+
teedataobject_safe_decref(tmp);
837835
return 0;
838836
}
839837

0 commit comments

Comments
 (0)