mirror of
https://github.com/python/cpython.git
synced 2025-07-07 19:35:27 +00:00
bpo-40887: Don't use finalized free lists (GH-20700)
In debug mode, ensure that free lists are no longer used after being finalized. Set numfree to -1 in finalization functions (eg. _PyList_Fini()), and then check that numfree is not equal to -1 before using a free list (e.g list_dealloc()).
This commit is contained in:
parent
c96a61e816
commit
bcb198385d
6 changed files with 88 additions and 3 deletions
|
@ -335,6 +335,10 @@ _context_alloc(void)
|
|||
PyInterpreterState *interp = _PyInterpreterState_GET();
|
||||
struct _Py_context_state *state = &interp->context;
|
||||
PyContext *ctx;
|
||||
#ifdef Py_DEBUG
|
||||
// _context_alloc() must not be called after _PyContext_Fini()
|
||||
assert(state->numfree != -1);
|
||||
#endif
|
||||
if (state->numfree) {
|
||||
state->numfree--;
|
||||
ctx = state->freelist;
|
||||
|
@ -460,6 +464,10 @@ context_tp_dealloc(PyContext *self)
|
|||
|
||||
PyInterpreterState *interp = _PyInterpreterState_GET();
|
||||
struct _Py_context_state *state = &interp->context;
|
||||
#ifdef Py_DEBUG
|
||||
// _context_alloc() must not be called after _PyContext_Fini()
|
||||
assert(state->numfree != -1);
|
||||
#endif
|
||||
if (state->numfree < CONTEXT_FREELIST_MAXLEN) {
|
||||
state->numfree++;
|
||||
self->ctx_weakreflist = (PyObject *)state->freelist;
|
||||
|
@ -1290,6 +1298,10 @@ _PyContext_Fini(PyThreadState *tstate)
|
|||
{
|
||||
Py_CLEAR(_token_missing);
|
||||
_PyContext_ClearFreeList(tstate);
|
||||
#ifdef Py_DEBUG
|
||||
struct _Py_context_state *state = &tstate->interp->context;
|
||||
state->numfree = -1;
|
||||
#endif
|
||||
_PyHamt_Fini();
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue