Commit 9ce5a835 authored by Victor Stinner's avatar Victor Stinner

PyUnicode_Join() checks output length in debug mode

PyUnicode_CopyCharacters() may copies less character than requested size, if
the input string is smaller than the argument. (This is very unlikely, but who
knows!?)

Avoid also calling PyUnicode_CopyCharacters() if the string is empty.
parent b8038953
...@@ -8890,20 +8890,32 @@ PyUnicode_Join(PyObject *separator, PyObject *seq) ...@@ -8890,20 +8890,32 @@ PyUnicode_Join(PyObject *separator, PyObject *seq)
/* Catenate everything. */ /* Catenate everything. */
for (i = 0, res_offset = 0; i < seqlen; ++i) { for (i = 0, res_offset = 0; i < seqlen; ++i) {
Py_ssize_t itemlen; Py_ssize_t itemlen, copied;
item = items[i]; item = items[i];
itemlen = PyUnicode_GET_LENGTH(item);
/* Copy item, and maybe the separator. */ /* Copy item, and maybe the separator. */
if (i) { if (i && seplen != 0) {
if (PyUnicode_CopyCharacters(res, res_offset, copied = PyUnicode_CopyCharacters(res, res_offset,
sep, 0, seplen) < 0) sep, 0, seplen);
if (copied < 0)
goto onError; goto onError;
#ifdef Py_DEBUG
res_offset += copied;
#else
res_offset += seplen; res_offset += seplen;
#endif
} }
if (PyUnicode_CopyCharacters(res, res_offset, itemlen = PyUnicode_GET_LENGTH(item);
item, 0, itemlen) < 0) if (itemlen != 0) {
copied = PyUnicode_CopyCharacters(res, res_offset,
item, 0, itemlen);
if (copied < 0)
goto onError; goto onError;
#ifdef Py_DEBUG
res_offset += copied;
#else
res_offset += itemlen; res_offset += itemlen;
#endif
}
} }
assert(res_offset == PyUnicode_GET_LENGTH(res)); assert(res_offset == PyUnicode_GET_LENGTH(res));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment