summaryrefslogtreecommitdiffstats
path: root/Objects/listobject.c
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2020-06-08 00:14:47 (GMT)
committerGitHub <noreply@github.com>2020-06-08 00:14:47 (GMT)
commitbcb198385dee469d630a184182df9dc1463e2c47 (patch)
tree193bc5c3584732b506223a34f3215a03fd1be408 /Objects/listobject.c
parentc96a61e8163c2d25ed4ac77cf96201fd0bdb945c (diff)
downloadcpython-bcb198385dee469d630a184182df9dc1463e2c47.zip
cpython-bcb198385dee469d630a184182df9dc1463e2c47.tar.gz
cpython-bcb198385dee469d630a184182df9dc1463e2c47.tar.bz2
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 'Objects/listobject.c')
-rw-r--r--Objects/listobject.c12
1 files changed, 12 insertions, 0 deletions
diff --git a/Objects/listobject.c b/Objects/listobject.c
index 043256d..22cdbe3 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -111,6 +111,10 @@ void
_PyList_Fini(PyThreadState *tstate)
{
_PyList_ClearFreeList(tstate);
+#ifdef Py_DEBUG
+ struct _Py_list_state *state = &tstate->interp->list;
+ state->numfree = -1;
+#endif
}
/* Print summary info about the state of the optimized allocator */
@@ -135,6 +139,10 @@ PyList_New(Py_ssize_t size)
PyInterpreterState *interp = _PyInterpreterState_GET();
struct _Py_list_state *state = &interp->list;
PyListObject *op;
+#ifdef Py_DEBUG
+ // PyList_New() must not be called after _PyList_Fini()
+ assert(state->numfree != -1);
+#endif
if (state->numfree) {
state->numfree--;
op = state->free_list[state->numfree];
@@ -330,6 +338,10 @@ list_dealloc(PyListObject *op)
}
PyInterpreterState *interp = _PyInterpreterState_GET();
struct _Py_list_state *state = &interp->list;
+#ifdef Py_DEBUG
+ // list_dealloc() must not be called after _PyList_Fini()
+ assert(state->numfree != -1);
+#endif
if (state->numfree < PyList_MAXFREELIST && PyList_CheckExact(op)) {
state->free_list[state->numfree++] = op;
}