diff options
author | Victor Stinner <victor.stinner@haypocalc.com> | 2011-10-03 23:36:02 +0200 |
---|---|---|
committer | Victor Stinner <victor.stinner@haypocalc.com> | 2011-10-03 23:36:02 +0200 |
commit | 9ce5a835bbb33c8a7f22e8ab4c6c226ecb27b0be (patch) | |
tree | 7f91e204d547fc86c83298daf3b672e18cb43af8 | |
parent | b80389535581300295758f3a9b50e9628b77ae3c (diff) | |
download | cpython-git-9ce5a835bbb33c8a7f22e8ab4c6c226ecb27b0be.tar.gz |
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.
-rw-r--r-- | Objects/unicodeobject.c | 30 |
1 files changed, 21 insertions, 9 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index e045b6bcfa..6aebdc0cff 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -8890,20 +8890,32 @@ PyUnicode_Join(PyObject *separator, PyObject *seq) /* Catenate everything. */ for (i = 0, res_offset = 0; i < seqlen; ++i) { - Py_ssize_t itemlen; + Py_ssize_t itemlen, copied; item = items[i]; - itemlen = PyUnicode_GET_LENGTH(item); /* Copy item, and maybe the separator. */ - if (i) { - if (PyUnicode_CopyCharacters(res, res_offset, - sep, 0, seplen) < 0) + if (i && seplen != 0) { + copied = PyUnicode_CopyCharacters(res, res_offset, + sep, 0, seplen); + if (copied < 0) goto onError; +#ifdef Py_DEBUG + res_offset += copied; +#else res_offset += seplen; +#endif + } + itemlen = PyUnicode_GET_LENGTH(item); + if (itemlen != 0) { + copied = PyUnicode_CopyCharacters(res, res_offset, + item, 0, itemlen); + if (copied < 0) + goto onError; +#ifdef Py_DEBUG + res_offset += copied; +#else + res_offset += itemlen; +#endif } - if (PyUnicode_CopyCharacters(res, res_offset, - item, 0, itemlen) < 0) - goto onError; - res_offset += itemlen; } assert(res_offset == PyUnicode_GET_LENGTH(res)); |