|
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 |
2 | 3 | import io |
3 | 4 | import re |
4 | 5 | from re import Scanner |
@@ -883,6 +884,37 @@ def test_large_subn(self, size): |
883 | 884 | self.assertEqual(n, size + 1) |
884 | 885 |
|
885 | 886 |
|
| 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 | + |
886 | 918 | def run_re_tests(): |
887 | 919 | from test.re_tests import tests, SUCCEED, FAIL, SYNTAX_ERROR |
888 | 920 | if verbose: |
|
0 commit comments