]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-115874: Don't use module state in teedataobject tp_dealloc (#116204)
authorErlend E. Aasland <erlend@python.org>
Mon, 18 Mar 2024 12:24:24 +0000 (13:24 +0100)
committerGitHub <noreply@github.com>
Mon, 18 Mar 2024 12:24:24 +0000 (13:24 +0100)
Co-authored-by: Brandt Bucher <brandtbucher@microsoft.com>
Lib/test/test_itertools.py
Modules/itertoolsmodule.c

index 9af0730ea9800490e69bee36e8b145d5f4a71dbc..95e67911db6a7f2df3dc40af42caa760ee35cea1 100644 (file)
@@ -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
@@ -1699,6 +1699,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))
index d377c2c57006aa8107200270fa2a975c4d438fab..44b92f8dcffe4dfb98a999dbd99768ad435644cf 100644 (file)
@@ -815,10 +815,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);
@@ -837,8 +836,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;
 }