bpo-23691: Protect the re.finditer() iterator from re-entering (GH-32012)

(cherry picked from commit 08eb754d84)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Miss Islington (bot) 2022-03-21 04:28:31 -07:00 committed by GitHub
parent 58a7e13037
commit f89949ec67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 4 deletions

View File

@ -0,0 +1 @@
Protect the :func:`re.finditer` iterator from re-entering.

View File

@ -2391,6 +2391,25 @@ scanner_dealloc(ScannerObject* self)
PyObject_DEL(self);
}
static int
scanner_begin(ScannerObject* self)
{
if (self->executing) {
PyErr_SetString(PyExc_ValueError,
"regular expression scanner already executing");
return 0;
}
self->executing = 1;
return 1;
}
static void
scanner_end(ScannerObject* self)
{
assert(self->executing);
self->executing = 0;
}
/*[clinic input]
_sre.SRE_Scanner.match
@ -2404,16 +2423,23 @@ _sre_SRE_Scanner_match_impl(ScannerObject *self)
PyObject* match;
Py_ssize_t status;
if (state->start == NULL)
if (!scanner_begin(self)) {
return NULL;
}
if (state->start == NULL) {
scanner_end(self);
Py_RETURN_NONE;
}
state_reset(state);
state->ptr = state->start;
status = sre_match(state, PatternObject_GetCode(self->pattern));
if (PyErr_Occurred())
if (PyErr_Occurred()) {
scanner_end(self);
return NULL;
}
match = pattern_new_match((PatternObject*) self->pattern,
state, status);
@ -2425,6 +2451,7 @@ _sre_SRE_Scanner_match_impl(ScannerObject *self)
state->start = state->ptr;
}
scanner_end(self);
return match;
}
@ -2442,16 +2469,23 @@ _sre_SRE_Scanner_search_impl(ScannerObject *self)
PyObject* match;
Py_ssize_t status;
if (state->start == NULL)
if (!scanner_begin(self)) {
return NULL;
}
if (state->start == NULL) {
scanner_end(self);
Py_RETURN_NONE;
}
state_reset(state);
state->ptr = state->start;
status = sre_search(state, PatternObject_GetCode(self->pattern));
if (PyErr_Occurred())
if (PyErr_Occurred()) {
scanner_end(self);
return NULL;
}
match = pattern_new_match((PatternObject*) self->pattern,
state, status);
@ -2463,6 +2497,7 @@ _sre_SRE_Scanner_search_impl(ScannerObject *self)
state->start = state->ptr;
}
scanner_end(self);
return match;
}
@ -2476,6 +2511,7 @@ pattern_scanner(PatternObject *self, PyObject *string, Py_ssize_t pos, Py_ssize_
if (!scanner)
return NULL;
scanner->pattern = NULL;
scanner->executing = 0;
/* create search state object */
if (!state_init(&scanner->state, self, string, pos, endpos)) {

View File

@ -89,6 +89,7 @@ typedef struct {
PyObject_HEAD
PyObject* pattern;
SRE_STATE state;
int executing;
} ScannerObject;
#endif