mirror of
https://github.com/python/cpython.git
synced 2025-12-23 09:19:18 +00:00
[3.12] GH-115874: Fix segfault in FutureIter_dealloc (GH-118114)
GH-115874: Fix segfault in FutureIter_dealloc (GH-117741)
(cherry picked from commit d8f350309d)
Co-authored-by: Savannah Ostrowski <savannahostrowski@gmail.com>
This commit is contained in:
parent
ecb8e2bdac
commit
23192aba20
2 changed files with 17 additions and 2 deletions
|
|
@ -0,0 +1 @@
|
|||
Fixed a possible segfault during garbage collection of ``_asyncio.FutureIter`` objects
|
||||
|
|
@ -1590,11 +1590,25 @@ static void
|
|||
FutureIter_dealloc(futureiterobject *it)
|
||||
{
|
||||
PyTypeObject *tp = Py_TYPE(it);
|
||||
asyncio_state *state = get_asyncio_state_by_def((PyObject *)it);
|
||||
|
||||
// FutureIter is a heap type so any subclass must also be a heap type.
|
||||
assert(_PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE));
|
||||
|
||||
PyObject *module = ((PyHeapTypeObject*)tp)->ht_module;
|
||||
asyncio_state *state = NULL;
|
||||
|
||||
PyObject_GC_UnTrack(it);
|
||||
tp->tp_clear((PyObject *)it);
|
||||
|
||||
if (state->fi_freelist_len < FI_FREELIST_MAXLEN) {
|
||||
// GH-115874: We can't use PyType_GetModuleByDef here as the type might have
|
||||
// already been cleared, which is also why we must check if ht_module != NULL.
|
||||
// Due to this restriction, subclasses that belong to a different module
|
||||
// will not be able to use the free list.
|
||||
if (module && _PyModule_GetDef(module) == &_asynciomodule) {
|
||||
state = get_asyncio_state(module);
|
||||
}
|
||||
|
||||
if (state && state->fi_freelist_len < FI_FREELIST_MAXLEN) {
|
||||
state->fi_freelist_len++;
|
||||
it->future = (FutureObj*) state->fi_freelist;
|
||||
state->fi_freelist = it;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue