Problem with multi-byte characters in str:padding

Tobias Hoffmann <[email protected]> Sun, 04 Aug 2013 15:52:34 +0200
Newsgroups gmane.comp.gnome.lib.xslt
Message-ID <[email protected]>
Hi,

when using  (e.g.)
<xsl:value-of select="str:padding(@no, '&#160;')"/>
via xsltproc I got unexpected errors:
   xmlXPathTranslateFunction: Invalid UTF8 string

AFAIKT the problem is caused by code in libexslt/strings.c
(from git.gnome.org, see my comments //):

static void
exsltStrPaddingFunction (xmlXPathParserContextPtr ctxt, int nargs) {
   int number, str_len = 0;
   xmlChar *str = NULL, *ret = NULL, *tmp;

   if ((nargs < 1) || (nargs > 2)) {
     xmlXPathSetArityError(ctxt);
     return;
   }
// now nargs == 2

   if (nargs == 2) { // always
    str = xmlXPathPopString(ctxt);
    str_len = xmlUTF8Strlen(str);
// now str_len contains number of utf-8 chars(!)
   }
   if (str_len == 0) {
     if (str != NULL) xmlFree(str);
     str = xmlStrdup((const xmlChar *) " ");
     str_len = 1;
   }

   number = (int) xmlXPathPopNumber(ctxt);

   if (number <= 0) {
     xmlXPathReturnEmptyString(ctxt);
     xmlFree(str);
     return;
   }

   while (number >= str_len) {
// xmlStrncat takes _bytes_ as 3rd argument!
     ret = xmlStrncat(ret, str, str_len);
// now multi-byte characters are corrupt
     number -= str_len;
   }
--- snip ---