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

Skip to content

Commit 0506c64

Browse files
committed
Fixing bug #817234, which made SRE get into an infinite loop on
empty final matches with finditer(). New test cases included for this bug and for #581080.
1 parent a01a2ee commit 0506c64

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

Lib/test/test_re.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,22 @@ def test_bug_931848(self):
567567
self.assertEqual(re.compile(pattern).split("a.b.c"),
568568
['a','b','c'])
569569

570+
def test_bug_581080(self):
571+
iter = re.finditer(r"\s", "a b")
572+
self.assertEqual(iter.next().span(), (1,2))
573+
self.assertRaises(StopIteration, iter.next)
574+
575+
scanner = re.compile(r"\s").scanner("a b")
576+
self.assertEqual(scanner.search().span(), (1, 2))
577+
self.assertEqual(scanner.search(), None)
578+
579+
def test_bug_817234(self):
580+
iter = re.finditer(r".*", "asdf")
581+
self.assertEqual(iter.next().span(), (0, 4))
582+
self.assertEqual(iter.next().span(), (4, 4))
583+
self.assertRaises(StopIteration, iter.next)
584+
585+
570586
def run_re_tests():
571587
from test.re_tests import benchmarks, tests, SUCCEED, FAIL, SYNTAX_ERROR
572588
if verbose:

Modules/_sre.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ SRE_COUNT(SRE_STATE* state, SRE_CODE* pattern, int maxcount)
539539
break;
540540

541541
case SRE_OP_ANY_ALL:
542-
/* repeated dot wildcare. skip to the end of the target
542+
/* repeated dot wildcard. skip to the end of the target
543543
string, and backtrack from there */
544544
TRACE(("|%p|%p|COUNT ANY_ALL\n", pattern, ptr));
545545
ptr = end;
@@ -3244,8 +3244,7 @@ scanner_match(ScannerObject* self, PyObject* args)
32443244
match = pattern_new_match((PatternObject*) self->pattern,
32453245
state, status);
32463246

3247-
if ((status == 0 || state->ptr == state->start) &&
3248-
state->ptr < state->end)
3247+
if (status == 0 || state->ptr == state->start)
32493248
state->start = (void*) ((char*) state->ptr + state->charsize);
32503249
else
32513250
state->start = state->ptr;
@@ -3276,8 +3275,7 @@ scanner_search(ScannerObject* self, PyObject* args)
32763275
match = pattern_new_match((PatternObject*) self->pattern,
32773276
state, status);
32783277

3279-
if ((status == 0 || state->ptr == state->start) &&
3280-
state->ptr < state->end)
3278+
if (status == 0 || state->ptr == state->start)
32813279
state->start = (void*) ((char*) state->ptr + state->charsize);
32823280
else
32833281
state->start = state->ptr;

0 commit comments

Comments
 (0)