Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c35f3a9

Browse files
Issue #16335: Fix integer overflow in unicode-escape decoder.
2 parents e4aa08e + 4f5f0e5 commit c35f3a9

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

Lib/test/test_ucn.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import unittest
1111
import unicodedata
12+
import _testcapi
1213

1314
from test import support
1415
from http.client import HTTPException
@@ -215,6 +216,21 @@ def test_strict_error_handling(self):
215216
str, b"\\NSPACE", 'unicode-escape', 'strict'
216217
)
217218

219+
@unittest.skipUnless(_testcapi.INT_MAX < _testcapi.PY_SSIZE_T_MAX,
220+
"needs UINT_MAX < SIZE_MAX")
221+
def test_issue16335(self):
222+
# very very long bogus character name
223+
try:
224+
x = b'\\N{SPACE' + b'x' * (_testcapi.UINT_MAX + 1) + b'}'
225+
except MemoryError:
226+
raise unittest.SkipTest("not enough memory")
227+
self.assertEqual(len(x), len(b'\\N{SPACE}') + (_testcapi.UINT_MAX + 1))
228+
self.assertRaisesRegex(UnicodeError,
229+
'unknown Unicode character name',
230+
x.decode, 'unicode-escape'
231+
)
232+
233+
218234
def test_main():
219235
support.run_unittest(UnicodeNamesTest)
220236

Objects/unicodeobject.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5696,7 +5696,8 @@ PyUnicode_DecodeUnicodeEscape(const char *s,
56965696
/* found a name. look it up in the unicode database */
56975697
message = "unknown Unicode character name";
56985698
s++;
5699-
if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1),
5699+
if (s - start - 1 <= INT_MAX &&
5700+
ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1),
57005701
&chr, 0))
57015702
goto store;
57025703
}

0 commit comments

Comments
 (0)