_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.
git-svn-id: svn://svn.tartarus.org/sgt/halibut@7622
cda61777-01e9-0310-a592-
d414129be87e
* This variant uses the C library locale.
*/
char *ret;
* This variant uses the C library locale.
*/
char *ret;
size_t siz;
len = ustrlen(s);
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);
- assert(siz <= (size_t)(MB_CUR_MAX * len));
+ assert(siz <= (size_t)(outlen));
ret[siz] = '\0';
ret = sresize(ret, siz+1, char);
return ret;
ret[siz] = '\0';
ret = sresize(ret, siz+1, char);
return ret;
* This variant uses the C library locale.
*/
wchar_t *ret;
* This variant uses the C library locale.
*/
wchar_t *ret;
size_t siz;
len = strlen(s);
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);
- assert(siz <= (size_t)(2 * len));
+ assert(siz <= (size_t)(outlen));
ret[siz] = L'\0';
ret = sresize(ret, siz+1, wchar_t);
return ret;
ret[siz] = L'\0';
ret = sresize(ret, siz+1, wchar_t);
return ret;