Skip to content

Commit

Permalink
_struct: fix race condition in cache_struct_converter
Browse files Browse the repository at this point in the history
  • Loading branch information
colesbury committed Apr 23, 2023
1 parent 22eca6e commit ada9b73
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions Modules/_struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -2239,15 +2239,9 @@ cache_struct_converter(PyObject *module, PyObject *fmt, PyStructObject **ptr)
return 1;
}

if (state->cache == NULL) {
state->cache = PyDict_New();
if (state->cache == NULL)
return 0;
}

s_object = PyDict_GetItemWithError(state->cache, fmt);
s_object = PyDict_FetchItemWithError(state->cache, fmt);
if (s_object != NULL) {
*ptr = (PyStructObject *)Py_NewRef(s_object);
*ptr = (PyStructObject *)s_object;
return Py_CLEANUP_SUPPORTED;
}
else if (PyErr_Occurred()) {
Expand Down Expand Up @@ -2277,7 +2271,7 @@ static PyObject *
_clearcache_impl(PyObject *module)
/*[clinic end generated code: output=ce4fb8a7bf7cb523 input=463eaae04bab3211]*/
{
Py_CLEAR(get_struct_state(module)->cache);
PyDict_Clear(get_struct_state(module)->cache);
Py_RETURN_NONE;
}

Expand Down Expand Up @@ -2506,6 +2500,12 @@ _structmodule_exec(PyObject *m)
if (state->PyStructType == NULL) {
return -1;
}

state->cache = PyDict_New();
if (state->cache == NULL) {
return -1;
}

if (PyModule_AddType(m, (PyTypeObject *)state->PyStructType) < 0) {
return -1;
}
Expand Down

0 comments on commit ada9b73

Please sign in to comment.