]> granicus.if.org Git - python/commitdiff
fix integer overflow in unicode case operations (closes #22643)
authorBenjamin Peterson <benjamin@python.org>
Wed, 15 Oct 2014 15:47:36 +0000 (11:47 -0400)
committerBenjamin Peterson <benjamin@python.org>
Wed, 15 Oct 2014 15:47:36 +0000 (11:47 -0400)
Lib/test/test_unicode.py
Misc/NEWS
Objects/unicodeobject.c

index c2ede07a83444f04bcb719a347ca37438a7cff70..e1ccd5c1da40cd92a68067b3857446cb95fd7406 100644 (file)
@@ -661,6 +661,11 @@ class UnicodeTest(string_tests.CommonTest,
         self.assertEqual('x'.center(4, '\U0010FFFF'),
                          '\U0010FFFFx\U0010FFFF\U0010FFFF')
 
+    @unittest.skipUnless(sys.maxsize == 2**31 - 1, "requires 32-bit system")
+    def test_case_operation_overflow(self):
+        # Issue #22643
+        self.assertRaises(OverflowError, ("ΓΌ"*(2**32//12 + 1)).upper)
+
     def test_contains(self):
         # Testing Unicode contains method
         self.assertIn('a', 'abdb')
index 8fbf58bd666bc7628ba13150e3e81311621c242e..7676c9051dd382a70cc2abb67769601ef6f582c5 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.3.6?
 Core and Builtins
 -----------------
 
+- Issue #22643: Fix integer overflow in Unicode case operations (upper, lower,
+  title, swapcase, casefold).
+
 - Issue #22518: Fixed integer overflow issues in "backslashreplace",
   "xmlcharrefreplace", and "surrogatepass" error handlers.
 
index 1ce5caafdce1c24c47c604a907840ee45fdafde9..35da4575308d01d70f8f66e8a28bbb5a91777f91 100644 (file)
@@ -9484,6 +9484,11 @@ case_operation(PyObject *self,
     kind = PyUnicode_KIND(self);
     data = PyUnicode_DATA(self);
     length = PyUnicode_GET_LENGTH(self);
+    if (length > PY_SSIZE_T_MAX / 3 ||
+        length > PY_SIZE_MAX / (3 * sizeof(Py_UCS4))) {
+        PyErr_SetString(PyExc_OverflowError, "string is too long");
+        return NULL;
+    }
     tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length);
     if (tmp == NULL)
         return PyErr_NoMemory();