summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2022-03-21 11:30:58 (GMT)
committerGitHub <noreply@github.com>2022-03-21 11:30:58 (GMT)
commit1b21b55ee0ea57226c52dc9f3ee3c034a5d1dc7b (patch)
treedb3168f852f91a538efb5ff3162f856ad724c75d /Modules
parent4c989e19c84ec224655bbbde9422e16d4a838a80 (diff)
downloadcpython-1b21b55ee0ea57226c52dc9f3ee3c034a5d1dc7b.zip
cpython-1b21b55ee0ea57226c52dc9f3ee3c034a5d1dc7b.tar.gz
cpython-1b21b55ee0ea57226c52dc9f3ee3c034a5d1dc7b.tar.bz2
bpo-23691: Protect the re.finditer() iterator from re-entering (GH-32012)
(cherry picked from commit 08eb754d840696914928355014c2d424131f8835) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_sre.c44
-rw-r--r--Modules/sre.h1
2 files changed, 41 insertions, 4 deletions
diff --git a/Modules/_sre.c b/Modules/_sre.c
index d21b533..911626d 100644
--- a/Modules/_sre.c
+++ b/Modules/_sre.c
@@ -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
@@ -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);
@@ -2548,6 +2574,7 @@ _sre_SRE_Scanner_match_impl(ScannerObject *self, PyTypeObject *cls)
state->start = state->ptr;
}
+ scanner_end(self);
return match;
}
@@ -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);
@@ -2590,6 +2624,7 @@ _sre_SRE_Scanner_search_impl(ScannerObject *self, PyTypeObject *cls)
state->start = state->ptr;
}
+ scanner_end(self);
return match;
}
@@ -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)) {
diff --git a/Modules/sre.h b/Modules/sre.h
index 9b0d8b1..785adbd 100644
--- a/Modules/sre.h
+++ b/Modules/sre.h
@@ -89,6 +89,7 @@ typedef struct {
PyObject_HEAD
PyObject* pattern;
SRE_STATE state;
+ int executing;
} ScannerObject;
#endif