summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authoranimalize <animalize@users.noreply.github.com>2019-02-18 13:26:37 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2019-02-18 13:26:37 (GMT)
commit4a7f44a2ed49ff1e87db062e7177a56c6e4bbdb0 (patch)
tree6daa91592d1d09f5b8ed34a1121e600a8bb03d74 /Modules
parent02c04f26dfa637db7091b7e16036a980bbf40893 (diff)
downloadcpython-4a7f44a2ed49ff1e87db062e7177a56c6e4bbdb0.zip
cpython-4a7f44a2ed49ff1e87db062e7177a56c6e4bbdb0.tar.gz
cpython-4a7f44a2ed49ff1e87db062e7177a56c6e4bbdb0.tar.bz2
bpo-34294: re module, fix wrong capturing groups in rare cases. (GH-11546)
Need to reset capturing groups between two SRE(match) callings in loops, this fixes wrong capturing groups in rare cases. Also add a missing index in re.rst.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_sre.c2
-rw-r--r--Modules/sre_lib.h8
2 files changed, 9 insertions, 1 deletions
diff --git a/Modules/_sre.c b/Modules/_sre.c
index 75f030c..21c41b5 100644
--- a/Modules/_sre.c
+++ b/Modules/_sre.c
@@ -340,7 +340,7 @@ _sre_unicode_tolower_impl(PyObject *module, int character)
LOCAL(void)
state_reset(SRE_STATE* state)
{
- /* FIXME: dynamic! */
+ /* state->mark will be set to 0 in SRE_OP_MARK dynamically. */
/*memset(state->mark, 0, sizeof(*state->mark) * SRE_MARK_SIZE);*/
state->lastmark = -1;
diff --git a/Modules/sre_lib.h b/Modules/sre_lib.h
index 44948e2..437ab43 100644
--- a/Modules/sre_lib.h
+++ b/Modules/sre_lib.h
@@ -1363,6 +1363,10 @@ exit:
return ret; /* should never get here */
}
+/* need to reset capturing groups between two SRE(match) callings in loops */
+#define RESET_CAPTURE_GROUP() \
+ do { state->lastmark = state->lastindex = -1; } while (0)
+
LOCAL(Py_ssize_t)
SRE(search)(SRE_STATE* state, SRE_CODE* pattern)
{
@@ -1440,6 +1444,7 @@ SRE(search)(SRE_STATE* state, SRE_CODE* pattern)
if (status != 0)
return status;
++ptr;
+ RESET_CAPTURE_GROUP();
}
return 0;
}
@@ -1487,6 +1492,7 @@ SRE(search)(SRE_STATE* state, SRE_CODE* pattern)
/* close but no cigar -- try again */
if (++ptr >= end)
return 0;
+ RESET_CAPTURE_GROUP();
}
i = overlap[i];
} while (i != 0);
@@ -1510,6 +1516,7 @@ SRE(search)(SRE_STATE* state, SRE_CODE* pattern)
if (status != 0)
break;
ptr++;
+ RESET_CAPTURE_GROUP();
}
} else {
/* general case */
@@ -1520,6 +1527,7 @@ SRE(search)(SRE_STATE* state, SRE_CODE* pattern)
state->must_advance = 0;
while (status == 0 && ptr < end) {
ptr++;
+ RESET_CAPTURE_GROUP();
TRACE(("|%p|%p|SEARCH\n", pattern, ptr));
state->start = state->ptr = ptr;
status = SRE(match)(state, pattern, 0);