Handle boundary cases more correctly; mblen(s, 0) and mbtowc(NULL, s, 0)

return -1 regardless of what s points to, mbtowc(&w, s, 1) sets w to a
null wide character when s points to a null byte. This seems to be closer
to what most other implementations do, but the C99 standard contradicts
itself for these cases.
This commit is contained in:
Tim J. Robbins 2002-10-28 08:24:46 +00:00
parent 8c847e9020
commit c5929b304e
2 changed files with 6 additions and 8 deletions

View File

@ -47,13 +47,12 @@ mblen(const char *s, size_t n)
{
const char *e;
if (s == NULL || *s == '\0')
if (s == NULL)
/* No support for state dependent encodings. */
return (0);
if (sgetrune(s, n, &e) == _INVALID_RUNE) {
errno = EILSEQ;
return (s - e);
return (-1);
}
return (e - s);
return (*s == '\0' ? 0 : e - s);
}

View File

@ -48,15 +48,14 @@ mbtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n)
const char *e;
rune_t r;
if (s == NULL || *s == '\0')
if (s == NULL)
/* No support for state dependent encodings. */
return (0);
if ((r = sgetrune(s, n, &e)) == _INVALID_RUNE) {
errno = EILSEQ;
return (s - e);
return (-1);
}
if (pwc != NULL)
*pwc = r;
return (e - s);
return (r == 0 ? 0 : e - s);
}