From 6b464c4aa367f36e96ecce714339d87aaa08e667 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Thu, 11 Jan 2024 08:40:48 +0900 Subject: [PATCH 1/5] gh-111968: Explicit handling for finzliaed freelist of list --- Objects/listobject.c | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/Objects/listobject.c b/Objects/listobject.c index 2fc57e13f632f8..50240dbb35d7e9 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -20,7 +20,7 @@ class list "PyListObject *" "&PyList_Type" _Py_DECLARE_STR(list_err, "list index out of range"); -#if PyList_MAXFREELIST > 0 +#ifdef WITH_FREELISTS static struct _Py_list_state * get_list_state(void) { @@ -123,7 +123,7 @@ list_preallocate_exact(PyListObject *self, Py_ssize_t size) void _PyList_ClearFreeList(_PyFreeListState *freelist_state, int is_finalization) { -#if PyList_MAXFREELIST > 0 +#ifdef WITH_FREELISTS struct _Py_list_state *state = &freelist_state->list; while (state->numfree > 0) { PyListObject *op = state->free_list[--state->numfree]; @@ -146,7 +146,7 @@ _PyList_Fini(_PyFreeListState *state) void _PyList_DebugMallocStats(FILE *out) { -#if PyList_MAXFREELIST > 0 +#ifdef WITH_FREELISTS struct _Py_list_state *state = get_list_state(); _PyDebugAllocatorStats(out, "free PyListObject", @@ -164,13 +164,9 @@ PyList_New(Py_ssize_t size) return NULL; } -#if PyList_MAXFREELIST > 0 +#ifdef WITH_FREELISTS struct _Py_list_state *state = get_list_state(); -#ifdef Py_DEBUG - // PyList_New() must not be called after _PyList_Fini() - assert(state->numfree != -1); -#endif - if (PyList_MAXFREELIST && state->numfree) { + if (PyList_MAXFREELIST && state->numfree > 0) { state->numfree--; op = state->free_list[state->numfree]; OBJECT_STAT_INC(from_freelist); @@ -360,13 +356,9 @@ list_dealloc(PyObject *self) } PyMem_Free(op->ob_item); } -#if PyList_MAXFREELIST > 0 +#ifdef WITH_FREELISTS struct _Py_list_state *state = get_list_state(); -#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)) { + if (state->numfree < PyList_MAXFREELIST && state->numfree >= 0 && PyList_CheckExact(op)) { state->free_list[state->numfree++] = op; OBJECT_STAT_INC(to_freelist); } From 4c9bbe675bae0bbf868294dc58cd4c21a242adb8 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Thu, 11 Jan 2024 08:47:33 +0900 Subject: [PATCH 2/5] Update --- Objects/floatobject.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Objects/floatobject.c b/Objects/floatobject.c index f1a09c0a94f4a6..2f5a1792e00687 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -131,11 +131,7 @@ PyFloat_FromDouble(double fval) #ifdef WITH_FREELISTS struct _Py_float_state *state = get_float_state(); op = state->free_list; - if (op != NULL) { -#ifdef Py_DEBUG - // PyFloat_FromDouble() must not be called after _PyFloat_Fini() - assert(state->numfree != -1); -#endif + if (op != NULL && state->numfree > 0) { state->free_list = (PyFloatObject *) Py_TYPE(op); state->numfree--; OBJECT_STAT_INC(from_freelist); From 9c3bae8ffe2e9cf0e9b5ff68bc08a14aa1d775e7 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Fri, 12 Jan 2024 08:56:46 +0900 Subject: [PATCH 3/5] Address code review --- Objects/floatobject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Objects/floatobject.c b/Objects/floatobject.c index 2f5a1792e00687..912c450a5e1055 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -131,7 +131,7 @@ PyFloat_FromDouble(double fval) #ifdef WITH_FREELISTS struct _Py_float_state *state = get_float_state(); op = state->free_list; - if (op != NULL && state->numfree > 0) { + if (op != NULL) { state->free_list = (PyFloatObject *) Py_TYPE(op); state->numfree--; OBJECT_STAT_INC(from_freelist); From e34e0426f94296420fe354e53ae23170c85024fa Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Fri, 12 Jan 2024 09:01:36 +0900 Subject: [PATCH 4/5] revert --- Objects/floatobject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Objects/floatobject.c b/Objects/floatobject.c index 912c450a5e1055..2f5a1792e00687 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -131,7 +131,7 @@ PyFloat_FromDouble(double fval) #ifdef WITH_FREELISTS struct _Py_float_state *state = get_float_state(); op = state->free_list; - if (op != NULL) { + if (op != NULL && state->numfree > 0) { state->free_list = (PyFloatObject *) Py_TYPE(op); state->numfree--; OBJECT_STAT_INC(from_freelist); From bbbd1b3c26852736e538ec208b6670a61690646e Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Fri, 12 Jan 2024 09:04:10 +0900 Subject: [PATCH 5/5] Address code review --- Objects/floatobject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Objects/floatobject.c b/Objects/floatobject.c index 2f5a1792e00687..912c450a5e1055 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -131,7 +131,7 @@ PyFloat_FromDouble(double fval) #ifdef WITH_FREELISTS struct _Py_float_state *state = get_float_state(); op = state->free_list; - if (op != NULL && state->numfree > 0) { + if (op != NULL) { state->free_list = (PyFloatObject *) Py_TYPE(op); state->numfree--; OBJECT_STAT_INC(from_freelist);