mirror of https://github.com/python/cpython.git
Issue #9669: Protect re against infinite loops on zero-width matching in
non-greedy repeat. Patch by Matthew Barnett.
This commit is contained in:
commit
e924ddb23e
|
@ -681,6 +681,15 @@ def test_stack_overflow(self):
|
||||||
self.assertEqual(re.match('(x)*y', 50000*'x'+'y').group(1), 'x')
|
self.assertEqual(re.match('(x)*y', 50000*'x'+'y').group(1), 'x')
|
||||||
self.assertEqual(re.match('(x)*?y', 50000*'x'+'y').group(1), 'x')
|
self.assertEqual(re.match('(x)*?y', 50000*'x'+'y').group(1), 'x')
|
||||||
|
|
||||||
|
def test_unlimited_zero_width_repeat(self):
|
||||||
|
# Issue #9669
|
||||||
|
self.assertIsNone(re.match(r'(?:a?)*y', 'z'))
|
||||||
|
self.assertIsNone(re.match(r'(?:a?)+y', 'z'))
|
||||||
|
self.assertIsNone(re.match(r'(?:a?){2,}y', 'z'))
|
||||||
|
self.assertIsNone(re.match(r'(?:a?)*?y', 'z'))
|
||||||
|
self.assertIsNone(re.match(r'(?:a?)+?y', 'z'))
|
||||||
|
self.assertIsNone(re.match(r'(?:a?){2,}?y', 'z'))
|
||||||
|
|
||||||
def test_scanner(self):
|
def test_scanner(self):
|
||||||
def s_ident(scanner, token): return token
|
def s_ident(scanner, token): return token
|
||||||
def s_operator(scanner, token): return "op%s" % token
|
def s_operator(scanner, token): return "op%s" % token
|
||||||
|
|
|
@ -255,6 +255,9 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #9669: Protect re against infinite loops on zero-width matching in
|
||||||
|
non-greedy repeat. Patch by Matthew Barnett.
|
||||||
|
|
||||||
- Issue #13169: The maximal repetition number in a regular expression has been
|
- Issue #13169: The maximal repetition number in a regular expression has been
|
||||||
increased from 65534 to 2147483647 (on 32-bit platform) or 4294967294 (on
|
increased from 65534 to 2147483647 (on 32-bit platform) or 4294967294 (on
|
||||||
64-bit).
|
64-bit).
|
||||||
|
|
|
@ -1272,13 +1272,18 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern)
|
||||||
|
|
||||||
LASTMARK_RESTORE();
|
LASTMARK_RESTORE();
|
||||||
|
|
||||||
if (ctx->count >= ctx->u.rep->pattern[2]
|
if ((ctx->count >= ctx->u.rep->pattern[2]
|
||||||
&& ctx->u.rep->pattern[2] != SRE_MAXREPEAT)
|
&& ctx->u.rep->pattern[2] != SRE_MAXREPEAT) ||
|
||||||
|
state->ptr == ctx->u.rep->last_ptr)
|
||||||
RETURN_FAILURE;
|
RETURN_FAILURE;
|
||||||
|
|
||||||
ctx->u.rep->count = ctx->count;
|
ctx->u.rep->count = ctx->count;
|
||||||
|
/* zero-width match protection */
|
||||||
|
DATA_PUSH(&ctx->u.rep->last_ptr);
|
||||||
|
ctx->u.rep->last_ptr = state->ptr;
|
||||||
DO_JUMP(JUMP_MIN_UNTIL_3,jump_min_until_3,
|
DO_JUMP(JUMP_MIN_UNTIL_3,jump_min_until_3,
|
||||||
ctx->u.rep->pattern+3);
|
ctx->u.rep->pattern+3);
|
||||||
|
DATA_POP(&ctx->u.rep->last_ptr);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
RETURN_ON_ERROR(ret);
|
RETURN_ON_ERROR(ret);
|
||||||
RETURN_SUCCESS;
|
RETURN_SUCCESS;
|
||||||
|
|
Loading…
Reference in New Issue