]> granicus.if.org Git - python/commitdiff
Issue #9669: Protect re against infinite loops on zero-width matching in
authorSerhiy Storchaka <storchaka@gmail.com>
Sat, 16 Feb 2013 19:23:01 +0000 (21:23 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Sat, 16 Feb 2013 19:23:01 +0000 (21:23 +0200)
non-greedy repeat.  Patch by Matthew Barnett.

Lib/test/test_re.py
Misc/NEWS
Modules/_sre.c

index a9d6f2cffe0a049c1b883ddabb94c4590168d857..6ddddda9e1bd2eb0161a7edb287c49544cc90756 100644 (file)
@@ -628,6 +628,15 @@ class ReTests(unittest.TestCase):
         self.assertEqual(re.match('(x)*y', 50000*'x'+'y').group(1), 'x')
         self.assertEqual(re.match('(x)*?y', 50000*'x'+'y').group(1), 'x')
 
+    def test_unlimited_zero_width_repeat(self):
+        # Issue #9669
+        self.assertIsNone(re.match(r'(?:a?)*y', 'z'))
+        self.assertIsNone(re.match(r'(?:a?)+y', 'z'))
+        self.assertIsNone(re.match(r'(?:a?){2,}y', 'z'))
+        self.assertIsNone(re.match(r'(?:a?)*?y', 'z'))
+        self.assertIsNone(re.match(r'(?:a?)+?y', 'z'))
+        self.assertIsNone(re.match(r'(?:a?){2,}?y', 'z'))
+
     def test_scanner(self):
         def s_ident(scanner, token): return token
         def s_operator(scanner, token): return "op%s" % token
index ee43643aa7486daea0ee2499f414ab6c61a2dcb6..5924366d953a3e807da4f2a33e4b942cc6c003c9 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -205,6 +205,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #9669: Protect re against infinite loops on zero-width matching in
+  non-greedy repeat.  Patch by Matthew Barnett.
+
 - 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).
index 73e5aacab7f7389ea700e57b8ac3311ae287c481..f3a29931a7a01fd3ea0ce090f71bf712bca90b29 100644 (file)
@@ -1302,13 +1302,18 @@ entrance:
 
             LASTMARK_RESTORE();
 
-            if (ctx->count >= ctx->u.rep->pattern[2]
-                && ctx->u.rep->pattern[2] != SRE_MAXREPEAT)
+            if ((ctx->count >= ctx->u.rep->pattern[2]
+                && ctx->u.rep->pattern[2] != SRE_MAXREPEAT) ||
+                state->ptr == ctx->u.rep->last_ptr)
                 RETURN_FAILURE;
 
             ctx->u.rep->count = ctx->count;
+            /* zero-width match protection */
+            DATA_PUSH(&ctx->u.rep->last_ptr);
+            ctx->u.rep->last_ptr = state->ptr;
             DO_JUMP(JUMP_MIN_UNTIL_3,jump_min_until_3,
                     ctx->u.rep->pattern+3);
+            DATA_POP(&ctx->u.rep->last_ptr);
             if (ret) {
                 RETURN_ON_ERROR(ret);
                 RETURN_SUCCESS;