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

Skip to content

Commit e1bd38c

Browse files
committed
fix integer overflow in unicode case operations (closes #22643)
1 parent 77a75b3 commit e1bd38c

3 files changed

Lines changed: 13 additions & 0 deletions

File tree

Lib/test/test_unicode.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,11 @@ def test_center(self):
661661
self.assertEqual('x'.center(4, '\U0010FFFF'),
662662
'\U0010FFFFx\U0010FFFF\U0010FFFF')
663663

664+
@unittest.skipUnless(sys.maxsize == 2**31 - 1, "requires 32-bit system")
665+
def test_case_operation_overflow(self):
666+
# Issue #22643
667+
self.assertRaises(OverflowError, ("ü"*(2**32//12 + 1)).upper)
668+
664669
def test_contains(self):
665670
# Testing Unicode contains method
666671
self.assertIn('a', 'abdb')

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.3.6?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #22643: Fix integer overflow in Unicode case operations (upper, lower,
14+
title, swapcase, casefold).
15+
1316
- Issue #22518: Fixed integer overflow issues in "backslashreplace",
1417
"xmlcharrefreplace", and "surrogatepass" error handlers.
1518

Objects/unicodeobject.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9484,6 +9484,11 @@ case_operation(PyObject *self,
94849484
kind = PyUnicode_KIND(self);
94859485
data = PyUnicode_DATA(self);
94869486
length = PyUnicode_GET_LENGTH(self);
9487+
if (length > PY_SSIZE_T_MAX / 3 ||
9488+
length > PY_SIZE_MAX / (3 * sizeof(Py_UCS4))) {
9489+
PyErr_SetString(PyExc_OverflowError, "string is too long");
9490+
return NULL;
9491+
}
94879492
tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length);
94889493
if (tmp == NULL)
94899494
return PyErr_NoMemory();

0 commit comments

Comments
 (0)