Skip to content

Commit 45db87c

Browse files
committed
Issue #29145: Merge 3.5.
2 parents 978fe4a + 0aca59d commit 45db87c

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

Objects/unicodeobject.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9988,7 +9988,7 @@ _PyUnicode_JoinArray(PyObject *separator, PyObject **items, Py_ssize_t seqlen)
99889988
use_memcpy = 1;
99899989
#endif
99909990
for (i = 0; i < seqlen; i++) {
9991-
const Py_ssize_t old_sz = sz;
9991+
size_t add_sz;
99929992
item = items[i];
99939993
if (!PyUnicode_Check(item)) {
99949994
PyErr_Format(PyExc_TypeError,
@@ -9999,16 +9999,18 @@ _PyUnicode_JoinArray(PyObject *separator, PyObject **items, Py_ssize_t seqlen)
99999999
}
1000010000
if (PyUnicode_READY(item) == -1)
1000110001
goto onError;
10002-
sz += PyUnicode_GET_LENGTH(item);
10002+
add_sz = PyUnicode_GET_LENGTH(item);
1000310003
item_maxchar = PyUnicode_MAX_CHAR_VALUE(item);
1000410004
maxchar = Py_MAX(maxchar, item_maxchar);
10005-
if (i != 0)
10006-
sz += seplen;
10007-
if (sz < old_sz || sz > PY_SSIZE_T_MAX) {
10005+
if (i != 0) {
10006+
add_sz += seplen;
10007+
}
10008+
if (add_sz > (size_t)(PY_SSIZE_T_MAX - sz)) {
1000810009
PyErr_SetString(PyExc_OverflowError,
1000910010
"join() result is too long for a Python string");
1001010011
goto onError;
1001110012
}
10013+
sz += add_sz;
1001210014
if (use_memcpy && last_obj != NULL) {
1001310015
if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item))
1001410016
use_memcpy = 0;
@@ -10646,7 +10648,7 @@ replace(PyObject *self, PyObject *str1,
1064610648
u = unicode_empty;
1064710649
goto done;
1064810650
}
10649-
if (new_size > (PY_SSIZE_T_MAX >> (rkind-1))) {
10651+
if (new_size > (PY_SSIZE_T_MAX / rkind)) {
1065010652
PyErr_SetString(PyExc_OverflowError,
1065110653
"replace string is too long");
1065210654
goto error;

0 commit comments

Comments
 (0)