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.
(cherry picked from commit 4a7f44a2ed49ff1e87db062e7177a56c6e4bbdb0)
Co-authored-by: animalize <animalize@users.noreply.github.com>
diff --git a/Modules/_sre.c b/Modules/_sre.c
index d2ea62d..a97ce77 100644
--- a/Modules/_sre.c
+++ b/Modules/_sre.c
@@ -347,7 +347,7 @@
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 @@
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 @@
if (status != 0)
return status;
++ptr;
+ RESET_CAPTURE_GROUP();
}
return 0;
}
@@ -1487,6 +1492,7 @@
/* close but no cigar -- try again */
if (++ptr >= end)
return 0;
+ RESET_CAPTURE_GROUP();
}
i = overlap[i];
} while (i != 0);
@@ -1510,6 +1516,7 @@
if (status != 0)
break;
ptr++;
+ RESET_CAPTURE_GROUP();
}
} else {
/* general case */
@@ -1520,6 +1527,7 @@
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);