mirror of
https://github.com/python/cpython.git
synced 2025-07-24 03:35:53 +00:00
bpo-44184: Fix subtype_dealloc() for freed type (GH-26274) (GH-26290)
Fix a crash at Python exit when a deallocator function removes the
last strong reference to a heap type.
Don't read type memory after calling basedealloc() since
basedealloc() can deallocate the type and free its memory.
_PyMem_IsPtrFreed() argument is now constant.
(cherry picked from commit 615069eb08
)
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
parent
856958d0e7
commit
50b0d148a6
4 changed files with 46 additions and 4 deletions
|
@ -1446,6 +1446,12 @@ subtype_dealloc(PyObject *self)
|
|||
if (_PyType_IS_GC(base)) {
|
||||
_PyObject_GC_TRACK(self);
|
||||
}
|
||||
|
||||
// Don't read type memory after calling basedealloc() since basedealloc()
|
||||
// can deallocate the type and free its memory.
|
||||
int type_needs_decref = (type->tp_flags & Py_TPFLAGS_HEAPTYPE
|
||||
&& !(base->tp_flags & Py_TPFLAGS_HEAPTYPE));
|
||||
|
||||
assert(basedealloc);
|
||||
basedealloc(self);
|
||||
|
||||
|
@ -1453,8 +1459,9 @@ subtype_dealloc(PyObject *self)
|
|||
our type from a HEAPTYPE to a non-HEAPTYPE, so be careful about
|
||||
reference counting. Only decref if the base type is not already a heap
|
||||
allocated type. Otherwise, basedealloc should have decref'd it already */
|
||||
if (type->tp_flags & Py_TPFLAGS_HEAPTYPE && !(base->tp_flags & Py_TPFLAGS_HEAPTYPE))
|
||||
Py_DECREF(type);
|
||||
if (type_needs_decref) {
|
||||
Py_DECREF(type);
|
||||
}
|
||||
|
||||
endlabel:
|
||||
Py_TRASHCAN_END
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue