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

Skip to content

Commit b779bfb

Browse files
committed
fix possible overflow bugs in unicodedata (closes #23367)
1 parent 03f8612 commit b779bfb

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Core and Builtins
1616
Library
1717
-------
1818

19+
- Issue #23367: Fix possible overflows in the unicodedata module.
20+
1921
- Issue #23361: Fix possible overflow in Windows subprocess creation code.
2022

2123
- Issue #23363: Fix possible overflow in itertools.permutations.

Modules/unicodedata.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,10 +507,17 @@ nfd_nfkd(PyObject *self, PyObject *input, int k)
507507

508508
stackptr = 0;
509509
isize = PyUnicode_GET_LENGTH(input);
510+
space = isize;
510511
/* Overallocate at most 10 characters. */
511-
space = (isize > 10 ? 10 : isize) + isize;
512+
if (space > 10) {
513+
if (space <= PY_SSIZE_T_MAX - 10)
514+
space += 10;
515+
}
516+
else {
517+
space *= 2;
518+
}
512519
osize = space;
513-
output = PyMem_Malloc(space * sizeof(Py_UCS4));
520+
output = PyMem_NEW(Py_UCS4, space);
514521
if (!output) {
515522
PyErr_NoMemory();
516523
return NULL;
@@ -657,7 +664,7 @@ nfc_nfkc(PyObject *self, PyObject *input, int k)
657664
/* We allocate a buffer for the output.
658665
If we find that we made no changes, we still return
659666
the NFD result. */
660-
output = PyMem_Malloc(len * sizeof(Py_UCS4));
667+
output = PyMem_NEW(Py_UCS4, len);
661668
if (!output) {
662669
PyErr_NoMemory();
663670
Py_DECREF(result);

0 commit comments

Comments
 (0)