summaryrefslogtreecommitdiff
path: root/Modules
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2019-02-18 05:48:23 -0800
committerGitHub <noreply@github.com>2019-02-18 05:48:23 -0800
commit0e379d43acc25277f02262212932d3c589a2031b (patch)
tree2c2d4461326f63766cd3c1bd1b5c79dce19b1100 /Modules
parenta01065a3588d3f0d0c57ea35107aa97e722fe2b2 (diff)
downloadcpython-git-0e379d43acc25277f02262212932d3c589a2031b.tar.gz
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>
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 d2ea62d55a..a97ce7790e 100644
--- a/Modules/_sre.c
+++ b/Modules/_sre.c
@@ -347,7 +347,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 44948e21ad..437ab43f43 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);