]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #1160: Fix compiling large regular expressions on UCS2 builds.
authorAntoine Pitrou <solipsis@pitrou.net>
Tue, 20 Nov 2012 21:30:42 +0000 (22:30 +0100)
committerAntoine Pitrou <solipsis@pitrou.net>
Tue, 20 Nov 2012 21:30:42 +0000 (22:30 +0100)
Patch by Serhiy Storchaka.

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

index 5162d4e6d83711798fcb029d0ac4747989659650..39972d51e8928d61479d93fc81f1ecb7315b19cf 100644 (file)
@@ -419,6 +419,12 @@ class ReTests(unittest.TestCase):
         self.assertEqual(re.match("([\u2222\u2223])",
                                   "\u2222", re.UNICODE).group(1), "\u2222")
 
+    def test_big_codesize(self):
+        # Issue #1160
+        r = re.compile('|'.join(('%d'%x for x in range(10000))))
+        self.assertIsNotNone(r.match('1000'))
+        self.assertIsNotNone(r.match('9999'))
+
     def test_anyall(self):
         self.assertEqual(re.match("a.b", "a\nb", re.DOTALL).group(0),
                          "a\nb")
index c8580e33cb0ff3c746bd2ce93a876fa24f0acdca..7aa34d9671b19639730a4c79cf0369a4350a140f 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -167,6 +167,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #1160: Fix compiling large regular expressions on UCS2 builds.
+  Patch by Serhiy Storchaka.
+
 - Issue #14313: zipfile now raises NotImplementedError when the compression
   type is unknown.
 
index 472b5a3797d8fa1e59a3dedce64c1e5f6401b6bd..9600a080ecda7c0bd741e5c53b60c8967dae4fad 100644 (file)
@@ -2695,6 +2695,13 @@ _compile(PyObject* self_, PyObject* args)
     for (i = 0; i < n; i++) {
         PyObject *o = PyList_GET_ITEM(code, i);
         unsigned long value = PyLong_AsUnsignedLong(o);
+        if (value == (unsigned long)-1 && PyErr_Occurred()) {
+            if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
+                PyErr_SetString(PyExc_OverflowError,
+                                "regular expression code size limit exceeded");
+            }
+            break;
+        }
         self->code[i] = (SRE_CODE) value;
         if ((unsigned long) self->code[i] != value) {
             PyErr_SetString(PyExc_OverflowError,
@@ -3065,10 +3072,8 @@ _validate_inner(SRE_CODE *code, SRE_CODE *end, Py_ssize_t groups)
                 GET_ARG; max = arg;
                 if (min > max)
                     FAIL;
-#ifdef Py_UNICODE_WIDE
                 if (max > 65535)
                     FAIL;
-#endif
                 if (!_validate_inner(code, code+skip-4, groups))
                     FAIL;
                 code += skip-4;
@@ -3086,10 +3091,8 @@ _validate_inner(SRE_CODE *code, SRE_CODE *end, Py_ssize_t groups)
                 GET_ARG; max = arg;
                 if (min > max)
                     FAIL;
-#ifdef Py_UNICODE_WIDE
                 if (max > 65535)
                     FAIL;
-#endif
                 if (!_validate_inner(code, code+skip-3, groups))
                     FAIL;
                 code += skip-3;
index aec9b541dd0fc6b8fce6879dc6e8ac6ebe50eb70..d389b46dd136cac21dd0da6130d9bec9e405d897 100644 (file)
 #include "sre_constants.h"
 
 /* size of a code word (must be unsigned short or larger, and
-   large enough to hold a Py_UNICODE character) */
-#ifdef Py_UNICODE_WIDE
+   large enough to hold a UCS4 character) */
 #define SRE_CODE Py_UCS4
-#else
-#define SRE_CODE unsigned short
-#endif
 
 typedef struct {
     PyObject_VAR_HEAD