Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1f35ae0

Browse files
Issue #17998: Fix an internal error in regular expression engine.
1 parent 1dfb918 commit 1f35ae0

4 files changed

Lines changed: 19 additions & 7 deletions

File tree

Lib/test/test_re.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,16 @@ def test_group_name_in_exception(self):
10401040
with self.assertRaisesRegex(sre_constants.error, '\?foo'):
10411041
re.compile('(?P<?foo>)')
10421042

1043+
def test_issue17998(self):
1044+
for reps in '*', '+', '?', '{1}':
1045+
for mod in '', '?':
1046+
pattern = '.' + reps + mod + 'yz'
1047+
self.assertEqual(re.compile(pattern, re.S).findall('xyz'),
1048+
['xyz'], msg=pattern)
1049+
pattern = pattern.encode()
1050+
self.assertEqual(re.compile(pattern, re.S).findall(b'xyz'),
1051+
[b'xyz'], msg=pattern)
1052+
10431053

10441054
def run_re_tests():
10451055
from test.re_tests import tests, SUCCEED, FAIL, SYNTAX_ERROR

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ Core and Builtins
5959
Library
6060
-------
6161

62+
- Issue #17998: Fix an internal error in regular expression engine.
63+
6264
- Issue #17557: Fix os.getgroups() to work with the modified behavior of
6365
getgroups(2) on OS X 10.8. Original patch by Mateusz Lenik.
6466

Modules/_sre.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,7 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern)
997997
TRACE(("|%p|%p|REPEAT_ONE %d %d\n", ctx->pattern, ctx->ptr,
998998
ctx->pattern[1], ctx->pattern[2]));
999999

1000-
if (ctx->pattern[1] > (end - ctx->ptr) / state->charsize)
1000+
if ((Py_ssize_t) ctx->pattern[1] > (end - ctx->ptr) / state->charsize)
10011001
RETURN_FAILURE; /* cannot match */
10021002

10031003
state->ptr = ctx->ptr;
@@ -1081,7 +1081,7 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern)
10811081
TRACE(("|%p|%p|MIN_REPEAT_ONE %d %d\n", ctx->pattern, ctx->ptr,
10821082
ctx->pattern[1], ctx->pattern[2]));
10831083

1084-
if (ctx->pattern[1] > (end - ctx->ptr) / state->charsize)
1084+
if ((Py_ssize_t) ctx->pattern[1] > (end - ctx->ptr) / state->charsize)
10851085
RETURN_FAILURE; /* cannot match */
10861086

10871087
state->ptr = ctx->ptr;
@@ -1180,7 +1180,7 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern)
11801180
TRACE(("|%p|%p|MAX_UNTIL %d\n", ctx->pattern,
11811181
ctx->ptr, ctx->count));
11821182

1183-
if (ctx->count < ctx->u.rep->pattern[1]) {
1183+
if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) {
11841184
/* not enough matches */
11851185
ctx->u.rep->count = ctx->count;
11861186
DO_JUMP(JUMP_MAX_UNTIL_1, jump_max_until_1,
@@ -1194,7 +1194,7 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern)
11941194
RETURN_FAILURE;
11951195
}
11961196

1197-
if ((ctx->count < ctx->u.rep->pattern[2] ||
1197+
if ((ctx->count < (Py_ssize_t) ctx->u.rep->pattern[2] ||
11981198
ctx->u.rep->pattern[2] == SRE_MAXREPEAT) &&
11991199
state->ptr != ctx->u.rep->last_ptr) {
12001200
/* we may have enough matches, but if we can
@@ -1243,7 +1243,7 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern)
12431243
TRACE(("|%p|%p|MIN_UNTIL %d %p\n", ctx->pattern,
12441244
ctx->ptr, ctx->count, ctx->u.rep->pattern));
12451245

1246-
if (ctx->count < ctx->u.rep->pattern[1]) {
1246+
if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) {
12471247
/* not enough matches */
12481248
ctx->u.rep->count = ctx->count;
12491249
DO_JUMP(JUMP_MIN_UNTIL_1, jump_min_until_1,
@@ -1272,7 +1272,7 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern)
12721272

12731273
LASTMARK_RESTORE();
12741274

1275-
if ((ctx->count >= ctx->u.rep->pattern[2]
1275+
if ((ctx->count >= (Py_ssize_t) ctx->u.rep->pattern[2]
12761276
&& ctx->u.rep->pattern[2] != SRE_MAXREPEAT) ||
12771277
state->ptr == ctx->u.rep->last_ptr)
12781278
RETURN_FAILURE;

Modules/sre.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#if SIZEOF_SIZE_T > 4
2020
# define SRE_MAXREPEAT (~(SRE_CODE)0)
2121
#else
22-
# define SRE_MAXREPEAT ((SRE_CODE)PY_SSIZE_T_MAX + 1u)
22+
# define SRE_MAXREPEAT ((SRE_CODE)PY_SSIZE_T_MAX)
2323
#endif
2424

2525
typedef struct {

0 commit comments

Comments
 (0)