Commits

Serhiy Storchaka committed 7625866 Draft

Issue #16335: Fix integer overflow in unicode-escape decoder.

  • Participants
  • Parent commits 260a9af
  • Branches 3.2

Comments (0)

Files changed (2)

File Lib/test/test_ucn.py

 """#"
 
 import unittest
+import _testcapi
 
 from test import support
 
             str, b"\\NSPACE", 'unicode-escape', 'strict'
         )
 
+    @unittest.skipUnless(_testcapi.INT_MAX < _testcapi.PY_SSIZE_T_MAX,
+                         "needs UINT_MAX < SIZE_MAX")
+    def test_issue16335(self):
+        # very very long bogus character name
+        try:
+            x = b'\\N{SPACE' + b'x' * (_testcapi.UINT_MAX + 1) + b'}'
+        except MemoryError:
+            raise unittest.SkipTest("not enough memory")
+        self.assertEqual(len(x), len(b'\\N{SPACE}') + (_testcapi.UINT_MAX + 1))
+        self.assertRaisesRegex(UnicodeError,
+            'unknown Unicode character name',
+            x.decode, 'unicode-escape'
+        )
+
+
 def test_main():
     support.run_unittest(UnicodeNamesTest)
 

File Objects/unicodeobject.c

                     /* found a name.  look it up in the unicode database */
                     message = "unknown Unicode character name";
                     s++;
-                    if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr))
+                    if (s - start - 1 <= INT_MAX &&
+                        ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr))
                         goto store;
                 }
             }