Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.10] bpo-23691: Protect the re.finditer() iterator from re-entering (GH-32012) #32025

Merged
merged 1 commit into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Protect the :func:`re.finditer` iterator from re-entering.
44 changes: 40 additions & 4 deletions Modules/_sre.c
Original file line number Diff line number Diff line change
Expand Up @@ -2510,6 +2510,25 @@ scanner_dealloc(ScannerObject* self)
Py_DECREF(tp);
}

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

Expand All @@ -2527,16 +2546,23 @@ _sre_SRE_Scanner_match_impl(ScannerObject *self, PyTypeObject *cls)
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(module_state, (PatternObject*) self->pattern,
state, status);
Expand All @@ -2548,6 +2574,7 @@ _sre_SRE_Scanner_match_impl(ScannerObject *self, PyTypeObject *cls)
state->start = state->ptr;
}

scanner_end(self);
return match;
}

Expand All @@ -2569,16 +2596,23 @@ _sre_SRE_Scanner_search_impl(ScannerObject *self, PyTypeObject *cls)
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(module_state, (PatternObject*) self->pattern,
state, status);
Expand All @@ -2590,6 +2624,7 @@ _sre_SRE_Scanner_search_impl(ScannerObject *self, PyTypeObject *cls)
state->start = state->ptr;
}

scanner_end(self);
return match;
}

Expand All @@ -2607,6 +2642,7 @@ pattern_scanner(_sremodulestate *module_state,
if (!scanner)
return NULL;
scanner->pattern = NULL;
scanner->executing = 0;

/* create search state object */
if (!state_init(&scanner->state, self, string, pos, endpos)) {
Expand Down
1 change: 1 addition & 0 deletions Modules/sre.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ typedef struct {
PyObject_HEAD
PyObject* pattern;
SRE_STATE state;
int executing;
} ScannerObject;

#endif