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

Skip to content

Commit e55181f

Browse files
Issue #23490: Fixed possible crashes related to interoperability between
old-style and new API for string with 2**30-1 characters.
1 parent babc688 commit e55181f

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

Objects/unicodeobject.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,6 +1535,10 @@ _PyUnicode_Ready(PyObject *unicode)
15351535
/* in case the native representation is 2-bytes, we need to allocate a
15361536
new normalized 4-byte version. */
15371537
length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates;
1538+
if (length_wo_surrogates > PY_SSIZE_T_MAX / 4 - 1) {
1539+
PyErr_NoMemory();
1540+
return -1;
1541+
}
15381542
_PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1));
15391543
if (!_PyUnicode_DATA_ANY(unicode)) {
15401544
PyErr_NoMemory();
@@ -3846,6 +3850,11 @@ PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size)
38463850
#endif
38473851
}
38483852
else {
3853+
if ((size_t)_PyUnicode_LENGTH(unicode) >
3854+
PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) {
3855+
PyErr_NoMemory();
3856+
return NULL;
3857+
}
38493858
_PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) *
38503859
(_PyUnicode_LENGTH(unicode) + 1));
38513860
if (!_PyUnicode_WSTR(unicode)) {

0 commit comments

Comments
 (0)