diff -r 8b181c75792f Lib/test/test_ucn.py --- a/Lib/test/test_ucn.py Fri Nov 09 01:03:44 2012 +0200 +++ b/Lib/test/test_ucn.py Fri Nov 09 16:37:11 2012 +0200 @@ -8,6 +8,7 @@ """#" import unittest +import _testcapi from test import test_support @@ -137,6 +138,20 @@ unicode, "\\NSPACE", 'unicode-escape', 'strict' ) + @unittest.skipUnless(_testcapi.INT_MAX < _testcapi.PY_SSIZE_T_MAX, + "needs UINT_MAX < SIZE_MAX") + @test_support.bigmemtest(minsize=_testcapi.UINT_MAX + 1 + len(b'\\N{SPACE}') + 1, + memuse=1 + 4) + def test_issue16335(self, size): + # very very long bogus character name + x = b'\\N{SPACE' + b'x' * (_testcapi.UINT_MAX + 1) + b'}' + 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(): test_support.run_unittest(UnicodeNamesTest) diff -r 8b181c75792f Objects/unicodeobject.c --- a/Objects/unicodeobject.c Fri Nov 09 01:03:44 2012 +0200 +++ b/Objects/unicodeobject.c Fri Nov 09 16:37:11 2012 +0200 @@ -2896,7 +2896,8 @@ /* 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; } }