Skip to content

Commit 65dede8

Browse files
committed
Lock someplaces outside of dicts & review feedback
1 parent c0aed69 commit 65dede8

File tree

4 files changed

+298
-279
lines changed

4 files changed

+298
-279
lines changed

Modules/_sre/sre.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@ static const char copyright[] =
3939
" SRE 2.2.2 Copyright (c) 1997-2002 by Secret Labs AB ";
4040

4141
#include "Python.h"
42-
#include "pycore_dict.h" // _PyDict_Next()
43-
#include "pycore_long.h" // _PyLong_GetZero()
44-
#include "pycore_moduleobject.h" // _PyModule_GetState()
42+
#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION
43+
#include "pycore_dict.h" // _PyDict_Next()
44+
#include "pycore_long.h" // _PyLong_GetZero()
45+
#include "pycore_moduleobject.h" // _PyModule_GetState()
4546

46-
#include "sre.h" // SRE_CODE
47+
#include "sre.h" // SRE_CODE
4748

48-
#include <ctype.h> // tolower(), toupper(), isalnum()
49+
#include <ctype.h> // tolower(), toupper(), isalnum()
4950

5051
#define SRE_CODE_BITS (8 * sizeof(SRE_CODE))
5152

@@ -2349,26 +2350,28 @@ _sre_SRE_Match_groupdict_impl(MatchObject *self, PyObject *default_value)
23492350
if (!result || !self->pattern->groupindex)
23502351
return result;
23512352

2353+
Py_BEGIN_CRITICAL_SECTION(self->pattern->groupindex);
23522354
while (_PyDict_Next(self->pattern->groupindex, &pos, &key, &value, &hash)) {
23532355
int status;
23542356
Py_INCREF(key);
23552357
value = match_getslice(self, key, default_value);
23562358
if (!value) {
23572359
Py_DECREF(key);
2358-
goto failed;
2360+
Py_CLEAR(result);
2361+
goto exit;
23592362
}
23602363
status = _PyDict_SetItem_KnownHash(result, key, value, hash);
23612364
Py_DECREF(value);
23622365
Py_DECREF(key);
2363-
if (status < 0)
2364-
goto failed;
2366+
if (status < 0) {
2367+
Py_CLEAR(result);
2368+
goto exit;
2369+
}
23652370
}
2371+
exit:
2372+
Py_END_CRITICAL_SECTION();
23662373

23672374
return result;
2368-
2369-
failed:
2370-
Py_DECREF(result);
2371-
return NULL;
23722375
}
23732376

23742377
/*[clinic input]

0 commit comments

Comments
 (0)