]> granicus.if.org Git - python/commitdiff
Issue #17998: Fix an internal error in regular expression engine.
authorSerhiy Storchaka <storchaka@gmail.com>
Sat, 3 Aug 2013 16:18:38 +0000 (19:18 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Sat, 3 Aug 2013 16:18:38 +0000 (19:18 +0300)
Lib/test/test_re.py
Misc/NEWS
Modules/_sre.c
Modules/sre.h

index 8bc74a2dfe1b2a2e95b02e2be2afd7336fbe5311..c84d4edeb452f3fc189d074873370fc6d0a27586 100644 (file)
@@ -1040,6 +1040,16 @@ class ReTests(unittest.TestCase):
         with self.assertRaisesRegex(sre_constants.error, '\?foo'):
             re.compile('(?P<?foo>)')
 
+    def test_issue17998(self):
+        for reps in '*', '+', '?', '{1}':
+            for mod in '', '?':
+                pattern = '.' + reps + mod + 'yz'
+                self.assertEqual(re.compile(pattern, re.S).findall('xyz'),
+                                 ['xyz'], msg=pattern)
+                pattern = pattern.encode()
+                self.assertEqual(re.compile(pattern, re.S).findall(b'xyz'),
+                                 [b'xyz'], msg=pattern)
+
 
 def run_re_tests():
     from test.re_tests import tests, SUCCEED, FAIL, SYNTAX_ERROR
index 27f97b07678dcfa667d12110a28d2b4ed69ef095..00b5a69d86e615b1893b1313391d03836a12c121 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -59,6 +59,8 @@ Core and Builtins
 Library
 -------
 
+- Issue #17998: Fix an internal error in regular expression engine.
+
 - Issue #17557: Fix os.getgroups() to work with the modified behavior of
   getgroups(2) on OS X 10.8.  Original patch by Mateusz Lenik.
 
index 19571fbf07aa1b80581b0a934520d978ece15b45..2ecbcc0be81ae2639e43b9dccf0a6c6d636d8c93 100644 (file)
@@ -997,7 +997,7 @@ entrance:
             TRACE(("|%p|%p|REPEAT_ONE %d %d\n", ctx->pattern, ctx->ptr,
                    ctx->pattern[1], ctx->pattern[2]));
 
-            if (ctx->pattern[1] > (end - ctx->ptr) / state->charsize)
+            if ((Py_ssize_t) ctx->pattern[1] > (end - ctx->ptr) / state->charsize)
                 RETURN_FAILURE; /* cannot match */
 
             state->ptr = ctx->ptr;
@@ -1081,7 +1081,7 @@ entrance:
             TRACE(("|%p|%p|MIN_REPEAT_ONE %d %d\n", ctx->pattern, ctx->ptr,
                    ctx->pattern[1], ctx->pattern[2]));
 
-            if (ctx->pattern[1] > (end - ctx->ptr) / state->charsize)
+            if ((Py_ssize_t) ctx->pattern[1] > (end - ctx->ptr) / state->charsize)
                 RETURN_FAILURE; /* cannot match */
 
             state->ptr = ctx->ptr;
@@ -1180,7 +1180,7 @@ entrance:
             TRACE(("|%p|%p|MAX_UNTIL %d\n", ctx->pattern,
                    ctx->ptr, ctx->count));
 
-            if (ctx->count < ctx->u.rep->pattern[1]) {
+            if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) {
                 /* not enough matches */
                 ctx->u.rep->count = ctx->count;
                 DO_JUMP(JUMP_MAX_UNTIL_1, jump_max_until_1,
@@ -1194,7 +1194,7 @@ entrance:
                 RETURN_FAILURE;
             }
 
-            if ((ctx->count < ctx->u.rep->pattern[2] ||
+            if ((ctx->count < (Py_ssize_t) ctx->u.rep->pattern[2] ||
                 ctx->u.rep->pattern[2] == SRE_MAXREPEAT) &&
                 state->ptr != ctx->u.rep->last_ptr) {
                 /* we may have enough matches, but if we can
@@ -1243,7 +1243,7 @@ entrance:
             TRACE(("|%p|%p|MIN_UNTIL %d %p\n", ctx->pattern,
                    ctx->ptr, ctx->count, ctx->u.rep->pattern));
 
-            if (ctx->count < ctx->u.rep->pattern[1]) {
+            if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) {
                 /* not enough matches */
                 ctx->u.rep->count = ctx->count;
                 DO_JUMP(JUMP_MIN_UNTIL_1, jump_min_until_1,
@@ -1272,7 +1272,7 @@ entrance:
 
             LASTMARK_RESTORE();
 
-            if ((ctx->count >= ctx->u.rep->pattern[2]
+            if ((ctx->count >= (Py_ssize_t) ctx->u.rep->pattern[2]
                 && ctx->u.rep->pattern[2] != SRE_MAXREPEAT) ||
                 state->ptr == ctx->u.rep->last_ptr)
                 RETURN_FAILURE;
index 1a6cd5678c773c597081155560f2dd6e99097b18..0a8f0cfe3d92de2b6087fb120ba101573a40b508 100644 (file)
@@ -19,7 +19,7 @@
 #if SIZEOF_SIZE_T > 4
 # define SRE_MAXREPEAT (~(SRE_CODE)0)
 #else
-# define SRE_MAXREPEAT ((SRE_CODE)PY_SSIZE_T_MAX + 1u)
+# define SRE_MAXREPEAT ((SRE_CODE)PY_SSIZE_T_MAX)
 #endif
 
 typedef struct {