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

Skip to content

Commit fa25a7d

Browse files
author
Fredrik Lundh
committed
-- don't use recursion for unbounded non-greedy repeat
(bugs #115903, #115696) This is based on a patch by Darrel Gallion. I'm not 100% sure about this fix, but I haven't managed to come up with any test case it cannot handle...
1 parent 07e99cb commit fa25a7d

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

Lib/test/test_sre.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def bump_num(matchobj):
248248
test(r"""sre.match(r'(x)*y', 50000*'x'+'y').span()""",
249249
(0, 50001), RuntimeError)
250250
test(r"""sre.match(r'(x)*?y', 50000*'x'+'y').span()""",
251-
(0, 50001), RuntimeError)
251+
(0, 50001)) # this works in 2.1
252252

253253
from re_tests import *
254254

Modules/_sre.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* 2000-10-24 fl really fixed assert_not; reset groups in findall
2525
* 2000-12-21 fl fixed memory leak in groupdict
2626
* 2001-01-02 fl properly reset pointer after failed assertion in MIN_UNTIL
27+
* 2001-01-15 fl don't use recursion for unbounded MIN_UTIL
2728
*
2829
* Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved.
2930
*
@@ -38,7 +39,7 @@
3839

3940
#ifndef SRE_RECURSIVE
4041

41-
char copyright[] = " SRE 0.9.9 Copyright (c) 1997-2000 by Secret Labs AB ";
42+
char copyright[] = " SRE 0.9.9 Copyright (c) 1997-2001 by Secret Labs AB ";
4243

4344
#include "Python.h"
4445

@@ -1012,11 +1013,21 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern, int level)
10121013

10131014
/* see if the tail matches */
10141015
state->repeat = rp->prev;
1015-
i = SRE_MATCH(state, pattern, level + 1);
1016+
if (rp->pattern[2] == 65535) {
1017+
/* unbounded repeat */
1018+
for (;;) {
1019+
i = SRE_MATCH(state, pattern, level + 1);
1020+
if (i || ptr >= end)
1021+
break;
1022+
state->ptr = ++ptr;
1023+
}
1024+
} else
1025+
i = SRE_MATCH(state, pattern, level + 1);
10161026
if (i) {
10171027
/* free(rp); */
10181028
return i;
10191029
}
1030+
10201031
state->ptr = ptr;
10211032
state->repeat = rp;
10221033

0 commit comments

Comments
 (0)