[Python-Dev] cpython: PyUnicode_Join() checks output length in debug mode (original) (raw)

Georg Brandl g.brandl at gmx.net
Tue Oct 4 23:41:54 CEST 2011


On 10/03/11 23:35, victor.stinner wrote:

http://hg.python.org/cpython/rev/bfd8b5d35f9c changeset: 72623:bfd8b5d35f9c user: Victor Stinner <victor.stinner at haypocalc.com> date: Mon Oct 03 23:36:02 2011 +0200 summary: PyUnicodeJoin() checks output length in debug mode

PyUnicodeCopyCharacters() 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 PyUnicodeCopyCharacters() if the string is empty. files: Objects/unicodeobject.c | 34 +++++++++++++++++++--------- 1 files changed, 23 insertions(+), 11 deletions(-)

diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -8890,20 +8890,32 @@ /* Catenate everything. */ for (i = 0, resoffset = 0; i < seqlen; ++i) { - Pyssizet itemlen; + Pyssizet itemlen, copied; item = items[i]; + /* Copy item, and maybe the separator. */ + if (i && seplen != 0) { + copied = PyUnicodeCopyCharacters(res, resoffset, + sep, 0, seplen); + if (copied < 0) + goto onError; +#ifdef PyDEBUG + resoffset += copied; +#else + resoffset += seplen; +#endif + } itemlen = PyUnicodeGETLENGTH(item); - /* Copy item, and maybe the separator. */ - if (i) { - if (PyUnicodeCopyCharacters(res, resoffset, - sep, 0, seplen) < 0) + if (itemlen != 0) { + copied = PyUnicodeCopyCharacters(res, resoffset, + item, 0, itemlen); + if (copied < 0) goto onError; - resoffset += seplen; - } - if (PyUnicodeCopyCharacters(res, resoffset, - item, 0, itemlen) < 0) - goto onError; - resoffset += itemlen; +#ifdef PyDEBUG + resoffset += copied; +#else + resoffset += itemlen; +#endif + } } assert(resoffset == PyUnicodeGETLENGTH(res));

I don't understand this change. Why would you not always add "copied" once you already have it? It seems to be the more correct version anyway.

Georg



More information about the Python-Dev mailing list