summaryrefslogtreecommitdiff
path: root/ustring.c
diff options
context:
space:
mode:
authorSimon Tatham <anakin@pobox.com>2007-06-21 14:10:01 +0000
committerSimon Tatham <anakin@pobox.com>2007-06-21 14:10:01 +0000
commitdfc9234fa55bc93174e057b90c1f665eb8cb39e4 (patch)
tree0c90a8bb121c91e9cede5113a47a99b4fad0d483 /ustring.c
parent9d14f4577f82230c176ed963cdd7675eb874ee4c (diff)
downloadhalibut-dfc9234fa55bc93174e057b90c1f665eb8cb39e4.zip
halibut-dfc9234fa55bc93174e057b90c1f665eb8cb39e4.tar.gz
halibut-dfc9234fa55bc93174e057b90c1f665eb8cb39e4.tar.bz2
halibut-dfc9234fa55bc93174e057b90c1f665eb8cb39e4.tar.xz
The length parameters in mbstowcs and wcstombs are limits on the
_output_ length, not the input length. Adjust accordingly. This has the side effect of working around what I believe to be a bug in DJGPP's mbstowcs(), namely that if you give it a string which exactly fits in the output length _without_ the trailing NUL, then it will write n-1 characters of the string plus a NUL where I believe it should write n characters of the string and no NUL. [originally from svn r7622]
Diffstat (limited to 'ustring.c')
-rw-r--r--ustring.c18
1 files changed, 10 insertions, 8 deletions
diff --git a/ustring.c b/ustring.c
index 95477a5..3d18a4f 100644
--- a/ustring.c
+++ b/ustring.c
@@ -172,17 +172,18 @@ char *utoa_locale_dup(wchar_t const *s)
* This variant uses the C library locale.
*/
char *ret;
- int len;
+ int len, outlen;
size_t siz;
len = ustrlen(s);
- ret = snewn(1 + MB_CUR_MAX * len, char);
+ outlen = 1 + MB_CUR_MAX * len;
+ ret = snewn(outlen+1, char);
- siz = wcstombs(ret, s, len);
+ siz = wcstombs(ret, s, outlen);
if (siz) {
- assert(siz <= (size_t)(MB_CUR_MAX * len));
+ assert(siz <= (size_t)(outlen));
ret[siz] = '\0';
ret = sresize(ret, siz+1, char);
return ret;
@@ -203,17 +204,18 @@ wchar_t *ufroma_locale_dup(char const *s)
* This variant uses the C library locale.
*/
wchar_t *ret;
- int len;
+ int len, outlen;
size_t siz;
len = strlen(s);
- ret = snewn(1 + 2*len, wchar_t); /* be conservative */
+ outlen = 1 + 2*len;
+ ret = snewn(outlen+1, wchar_t); /* be conservative */
- siz = mbstowcs(ret, s, len);
+ siz = mbstowcs(ret, s, outlen);
if (siz) {
- assert(siz <= (size_t)(2 * len));
+ assert(siz <= (size_t)(outlen));
ret[siz] = L'\0';
ret = sresize(ret, siz+1, wchar_t);
return ret;