Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 6621ade

Browse files
committed
_struct: fix race condition in cache_struct_converter
Fixes #106
1 parent fa773e7 commit 6621ade

1 file changed

Lines changed: 6 additions & 9 deletions

File tree

Modules/_struct.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2081,15 +2081,8 @@ cache_struct_converter(PyObject *fmt, PyStructObject **ptr)
20812081
return 1;
20822082
}
20832083

2084-
if (cache == NULL) {
2085-
cache = PyDict_New();
2086-
if (cache == NULL)
2087-
return 0;
2088-
}
2089-
2090-
s_object = PyDict_GetItemWithError(cache, fmt);
2084+
s_object = PyDict_GetItemWithError2(cache, fmt);
20912085
if (s_object != NULL) {
2092-
Py_INCREF(s_object);
20932086
*ptr = (PyStructObject *)s_object;
20942087
return Py_CLEANUP_SUPPORTED;
20952088
}
@@ -2120,7 +2113,7 @@ static PyObject *
21202113
_clearcache_impl(PyObject *module)
21212114
/*[clinic end generated code: output=ce4fb8a7bf7cb523 input=463eaae04bab3211]*/
21222115
{
2123-
Py_CLEAR(cache);
2116+
PyDict_Clear(cache);
21242117
Py_RETURN_NONE;
21252118
}
21262119

@@ -2352,6 +2345,10 @@ PyInit__struct(void)
23522345
if (m == NULL)
23532346
return NULL;
23542347

2348+
cache = PyDict_New();
2349+
if (cache == NULL)
2350+
return NULL;
2351+
23552352
PyObject *PyStructType = PyType_FromSpec(&PyStructType_spec);
23562353
if (PyStructType == NULL) {
23572354
return NULL;

0 commit comments

Comments
 (0)