diff options
author | Victor Stinner <vstinner@python.org> | 2020-06-08 02:14:47 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-08 02:14:47 +0200 |
commit | bcb198385dee469d630a184182df9dc1463e2c47 (patch) | |
tree | 193bc5c3584732b506223a34f3215a03fd1be408 /Python/context.c | |
parent | c96a61e8163c2d25ed4ac77cf96201fd0bdb945c (diff) | |
download | cpython-bcb198385dee469d630a184182df9dc1463e2c47.tar.gz cpython-bcb198385dee469d630a184182df9dc1463e2c47.zip |
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()).
Diffstat (limited to 'Python/context.c')
-rw-r--r-- | Python/context.c | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/Python/context.c b/Python/context.c index 3cf8db4c90c..dedbca99384 100644 --- a/Python/context.c +++ b/Python/context.c @@ -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(); } |