From 515283959aae470a6ee5033deb9f977a489e7578 Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Sun, 4 Aug 2013 16:42:51 +0200 Subject: [PATCH 06/14] Fix str:padding to work with UTF-8 strings Thanks to Tobias Hoffmann for the report. Also add some tests. --- libexslt/strings.c | 16 +++++++++------- tests/exslt/strings/padding.1.out | 27 ++++++++++++++++++++++++++ tests/exslt/strings/padding.1.xml | 10 ++++++++++ tests/exslt/strings/padding.1.xsl | 40 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 7 deletions(-) create mode 100644 tests/exslt/strings/padding.1.out create mode 100644 tests/exslt/strings/padding.1.xml create mode 100644 tests/exslt/strings/padding.1.xsl diff --git a/libexslt/strings.c b/libexslt/strings.c index c0c7a18..3c702ad 100644 --- a/libexslt/strings.c +++ b/libexslt/strings.c @@ -351,8 +351,8 @@ exsltStrDecodeUriFunction (xmlXPathParserContextPtr ctxt, int nargs) { */ static void exsltStrPaddingFunction (xmlXPathParserContextPtr ctxt, int nargs) { - int number, str_len = 0; - xmlChar *str = NULL, *ret = NULL, *tmp; + int number, str_len = 0, str_size = 0; + xmlChar *str = NULL, *ret = NULL; if ((nargs < 1) || (nargs > 2)) { xmlXPathSetArityError(ctxt); @@ -362,11 +362,13 @@ exsltStrPaddingFunction (xmlXPathParserContextPtr ctxt, int nargs) { if (nargs == 2) { str = xmlXPathPopString(ctxt); str_len = xmlUTF8Strlen(str); + str_size = xmlStrlen(str); } if (str_len == 0) { if (str != NULL) xmlFree(str); str = xmlStrdup((const xmlChar *) " "); str_len = 1; + str_size = 1; } number = (int) xmlXPathPopNumber(ctxt); @@ -378,13 +380,13 @@ exsltStrPaddingFunction (xmlXPathParserContextPtr ctxt, int nargs) { } while (number >= str_len) { - ret = xmlStrncat(ret, str, str_len); + ret = xmlStrncat(ret, str, str_size); number -= str_len; } - tmp = xmlUTF8Strndup (str, number); - ret = xmlStrcat(ret, tmp); - if (tmp != NULL) - xmlFree (tmp); + if (number > 0) { + str_size = xmlUTF8Strsize(str, number); + ret = xmlStrncat(ret, str, str_size); + } xmlXPathReturnString(ctxt, ret); diff --git a/tests/exslt/strings/padding.1.out b/tests/exslt/strings/padding.1.out new file mode 100644 index 0000000..1833a65 --- /dev/null +++ b/tests/exslt/strings/padding.1.out @@ -0,0 +1,27 @@ + + + + + OK + + + -------- + OK + + + abcabcabca + OK + + + ––––––– + OK + + + – – – – – – + OK + + + ÄÖÜÄÖÜ + OK + + diff --git a/tests/exslt/strings/padding.1.xml b/tests/exslt/strings/padding.1.xml new file mode 100644 index 0000000..16d0824 --- /dev/null +++ b/tests/exslt/strings/padding.1.xml @@ -0,0 +1,10 @@ + + + + - + abc + + –  + ÄÖÜ + + diff --git a/tests/exslt/strings/padding.1.xsl b/tests/exslt/strings/padding.1.xsl new file mode 100644 index 0000000..c5d621d --- /dev/null +++ b/tests/exslt/strings/padding.1.xsl @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + OK + FAIL + + + + + + + -- 1.8.4.1