Skip to content

Commit 70ca021

Browse files
Issue #13169: The maximal repetition number in a regular expression has been
increased from 65534 to 2147483647 (on 32-bit platform) or 4294967294 (on 64-bit).
1 parent b19ed57 commit 70ca021

File tree

7 files changed

+62
-13
lines changed

7 files changed

+62
-13
lines changed

Lib/sre_compile.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import _sre, sys
1414
import sre_parse
1515
from sre_constants import *
16+
from _sre import MAXREPEAT
1617

1718
assert _sre.MAGIC == MAGIC, "SRE module mismatch"
1819

Lib/sre_constants.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515

1616
MAGIC = 20031017
1717

18-
# max code word in this release
19-
20-
MAXREPEAT = 65535
21-
2218
# SRE standard exception (access as sre.error)
2319
# should this really be here?
2420

Lib/sre_parse.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import sys
1616

1717
from sre_constants import *
18+
from _sre import MAXREPEAT
1819

1920
SPECIAL_CHARS = ".\\[{()*+?^$|"
2021
REPEAT_CHARS = "*+?{"
@@ -505,10 +506,14 @@ def _parse(source, state):
505506
continue
506507
if lo:
507508
min = int(lo)
509+
if min >= MAXREPEAT:
510+
raise OverflowError("the repetition number is too large")
508511
if hi:
509512
max = int(hi)
510-
if max < min:
511-
raise error("bad repeat interval")
513+
if max >= MAXREPEAT:
514+
raise OverflowError("the repetition number is too large")
515+
if max < min:
516+
raise error("bad repeat interval")
512517
else:
513518
raise error("not supported")
514519
# figure out which item to repeat

Lib/test/test_re.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from test.support import verbose, run_unittest, gc_collect, bigmemtest, _2G
1+
from test.support import verbose, run_unittest, gc_collect, bigmemtest, _2G, \
2+
cpython_only
23
import io
34
import re
45
from re import Scanner
@@ -883,6 +884,37 @@ def test_large_subn(self, size):
883884
self.assertEqual(n, size + 1)
884885

885886

887+
def test_repeat_minmax_overflow(self):
888+
# Issue #13169
889+
string = "x" * 100000
890+
self.assertEqual(re.match(r".{65535}", string).span(), (0, 65535))
891+
self.assertEqual(re.match(r".{,65535}", string).span(), (0, 65535))
892+
self.assertEqual(re.match(r".{65535,}?", string).span(), (0, 65535))
893+
self.assertEqual(re.match(r".{65536}", string).span(), (0, 65536))
894+
self.assertEqual(re.match(r".{,65536}", string).span(), (0, 65536))
895+
self.assertEqual(re.match(r".{65536,}?", string).span(), (0, 65536))
896+
# 2**128 should be big enough to overflow both SRE_CODE and Py_ssize_t.
897+
self.assertRaises(OverflowError, re.compile, r".{%d}" % 2**128)
898+
self.assertRaises(OverflowError, re.compile, r".{,%d}" % 2**128)
899+
self.assertRaises(OverflowError, re.compile, r".{%d,}?" % 2**128)
900+
self.assertRaises(OverflowError, re.compile, r".{%d,%d}" % (2**129, 2**128))
901+
902+
@cpython_only
903+
def test_repeat_minmax_overflow_maxrepeat(self):
904+
try:
905+
from _sre import MAXREPEAT
906+
except ImportError:
907+
self.skipTest('requires _sre.MAXREPEAT constant')
908+
string = "x" * 100000
909+
self.assertIsNone(re.match(r".{%d}" % (MAXREPEAT - 1), string))
910+
self.assertEqual(re.match(r".{,%d}" % (MAXREPEAT - 1), string).span(),
911+
(0, 100000))
912+
self.assertIsNone(re.match(r".{%d,}?" % (MAXREPEAT - 1), string))
913+
self.assertRaises(OverflowError, re.compile, r".{%d}" % MAXREPEAT)
914+
self.assertRaises(OverflowError, re.compile, r".{,%d}" % MAXREPEAT)
915+
self.assertRaises(OverflowError, re.compile, r".{%d,}?" % MAXREPEAT)
916+
917+
886918
def run_re_tests():
887919
from test.re_tests import tests, SUCCEED, FAIL, SYNTAX_ERROR
888920
if verbose:

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ Core and Builtins
224224
Library
225225
-------
226226

227+
- Issue #13169: The maximal repetition number in a regular expression has been
228+
increased from 65534 to 2147483647 (on 32-bit platform) or 4294967294 (on
229+
64-bit).
230+
227231
- Issue #16743: Fix mmap overflow check on 32 bit Windows.
228232

229233
- Issue #16800: tempfile.gettempdir() no longer left temporary files when

Modules/_sre.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ SRE_COUNT(SRE_STATE* state, SRE_CODE* pattern, Py_ssize_t maxcount)
517517
Py_ssize_t i;
518518

519519
/* adjust end */
520-
if (maxcount < end - ptr && maxcount != 65535)
520+
if (maxcount < end - ptr && maxcount != SRE_MAXREPEAT)
521521
end = ptr + maxcount;
522522

523523
switch (pattern[0]) {
@@ -1132,7 +1132,7 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern)
11321132
} else {
11331133
/* general case */
11341134
LASTMARK_SAVE();
1135-
while ((Py_ssize_t)ctx->pattern[2] == 65535
1135+
while ((Py_ssize_t)ctx->pattern[2] == SRE_MAXREPEAT
11361136
|| ctx->count <= (Py_ssize_t)ctx->pattern[2]) {
11371137
state->ptr = ctx->ptr;
11381138
DO_JUMP(JUMP_MIN_REPEAT_ONE,jump_min_repeat_one,
@@ -1218,7 +1218,7 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern)
12181218
}
12191219

12201220
if ((ctx->count < ctx->u.rep->pattern[2] ||
1221-
ctx->u.rep->pattern[2] == 65535) &&
1221+
ctx->u.rep->pattern[2] == SRE_MAXREPEAT) &&
12221222
state->ptr != ctx->u.rep->last_ptr) {
12231223
/* we may have enough matches, but if we can
12241224
match another item, do so */
@@ -1296,7 +1296,7 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern)
12961296
LASTMARK_RESTORE();
12971297

12981298
if (ctx->count >= ctx->u.rep->pattern[2]
1299-
&& ctx->u.rep->pattern[2] != 65535)
1299+
&& ctx->u.rep->pattern[2] != SRE_MAXREPEAT)
13001300
RETURN_FAILURE;
13011301

13021302
ctx->u.rep->count = ctx->count;
@@ -3072,7 +3072,7 @@ _validate_inner(SRE_CODE *code, SRE_CODE *end, Py_ssize_t groups)
30723072
GET_ARG; max = arg;
30733073
if (min > max)
30743074
FAIL;
3075-
if (max > 65535)
3075+
if (max > SRE_MAXREPEAT)
30763076
FAIL;
30773077
if (!_validate_inner(code, code+skip-4, groups))
30783078
FAIL;
@@ -3091,7 +3091,7 @@ _validate_inner(SRE_CODE *code, SRE_CODE *end, Py_ssize_t groups)
30913091
GET_ARG; max = arg;
30923092
if (min > max)
30933093
FAIL;
3094-
if (max > 65535)
3094+
if (max > SRE_MAXREPEAT)
30953095
FAIL;
30963096
if (!_validate_inner(code, code+skip-3, groups))
30973097
FAIL;
@@ -3979,6 +3979,12 @@ PyMODINIT_FUNC PyInit__sre(void)
39793979
Py_DECREF(x);
39803980
}
39813981

3982+
x = PyLong_FromUnsignedLong(SRE_MAXREPEAT);
3983+
if (x) {
3984+
PyDict_SetItemString(d, "MAXREPEAT", x);
3985+
Py_DECREF(x);
3986+
}
3987+
39823988
x = PyUnicode_FromString(copyright);
39833989
if (x) {
39843990
PyDict_SetItemString(d, "copyright", x);

Modules/sre.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
/* size of a code word (must be unsigned short or larger, and
1717
large enough to hold a UCS4 character) */
1818
#define SRE_CODE Py_UCS4
19+
#if SIZEOF_SIZE_T > 4
20+
# define SRE_MAXREPEAT (~(SRE_CODE)0)
21+
#else
22+
# define SRE_MAXREPEAT ((SRE_CODE)PY_SSIZE_T_MAX + 1u)
23+
#endif
1924

2025
typedef struct {
2126
PyObject_VAR_HEAD

0 commit comments

Comments
 (0)