mirror of https://github.com/python/cpython
gh-115874: Don't use module state in teedataobject tp_dealloc (#116204)
Co-authored-by: Brandt Bucher <brandtbucher@microsoft.com>
This commit is contained in:
parent
cd2ed91780
commit
e2fcaf19d3
|
@ -1,7 +1,7 @@
|
||||||
import doctest
|
import doctest
|
||||||
import unittest
|
import unittest
|
||||||
from test import support
|
from test import support
|
||||||
from test.support import threading_helper
|
from test.support import threading_helper, script_helper
|
||||||
from itertools import *
|
from itertools import *
|
||||||
import weakref
|
import weakref
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
|
@ -1699,6 +1699,14 @@ class TestBasicOps(unittest.TestCase):
|
||||||
self.pickletest(proto, a, compare=ans)
|
self.pickletest(proto, a, compare=ans)
|
||||||
self.pickletest(proto, b, 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()
|
# Issue 13454: Crash when deleting backward iterator from tee()
|
||||||
def test_tee_del_backward(self):
|
def test_tee_del_backward(self):
|
||||||
forward, backward = tee(repeat(None, 20000000))
|
forward, backward = tee(repeat(None, 20000000))
|
||||||
|
|
|
@ -815,10 +815,9 @@ teedataobject_traverse(teedataobject *tdo, visitproc visit, void * arg)
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
teedataobject_safe_decref(PyObject *obj, PyTypeObject *tdo_type)
|
teedataobject_safe_decref(PyObject *obj)
|
||||||
{
|
{
|
||||||
while (obj && Py_IS_TYPE(obj, tdo_type) &&
|
while (obj && Py_REFCNT(obj) == 1) {
|
||||||
Py_REFCNT(obj) == 1) {
|
|
||||||
PyObject *nextlink = ((teedataobject *)obj)->nextlink;
|
PyObject *nextlink = ((teedataobject *)obj)->nextlink;
|
||||||
((teedataobject *)obj)->nextlink = NULL;
|
((teedataobject *)obj)->nextlink = NULL;
|
||||||
Py_SETREF(obj, nextlink);
|
Py_SETREF(obj, nextlink);
|
||||||
|
@ -837,8 +836,7 @@ teedataobject_clear(teedataobject *tdo)
|
||||||
Py_CLEAR(tdo->values[i]);
|
Py_CLEAR(tdo->values[i]);
|
||||||
tmp = tdo->nextlink;
|
tmp = tdo->nextlink;
|
||||||
tdo->nextlink = NULL;
|
tdo->nextlink = NULL;
|
||||||
itertools_state *state = get_module_state_by_cls(Py_TYPE(tdo));
|
teedataobject_safe_decref(tmp);
|
||||||
teedataobject_safe_decref(tmp, state->teedataobject_type);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue