In the absence of proper validation, at least check that null bytes

do not appear as anything but the first byte of a multibyte character.
This commit is contained in:
Tim J. Robbins 2004-05-11 14:08:22 +00:00
parent bbf15239ed
commit 88af941a73
4 changed files with 19 additions and 1 deletions

View File

@ -122,6 +122,10 @@ _BIG5_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
if (n == 0 || (size_t)(len = _big5_check(*s)) > n)
/* Incomplete multibyte sequence */
return ((size_t)-2);
if (n == 2 && s[1] == '\0') {
errno = EILSEQ;
return ((size_t)-1);
}
wc = 0;
i = len;
while (i-- > 0)

View File

@ -185,8 +185,14 @@ _EUC_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
/* FALLTHROUGH */
case 1:
case 0:
while (remain-- > 0)
wc = (unsigned char)*s++;
while (--remain > 0) {
if (*s == '\0') {
errno = EILSEQ;
return ((size_t)-1);
}
wc = (wc << 8) | (unsigned char)*s++;
}
break;
}
wc = (wc & ~CEI->mask) | CEI->bits[set];

View File

@ -119,6 +119,10 @@ _GBK_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
if (n == 0 || (size_t)(len = _gbk_check(*s)) > n)
/* Incomplete multibyte sequence */
return ((size_t)-2);
if (n == 2 && s[1] == '\0') {
errno = EILSEQ;
return ((size_t)-1);
}
wc = 0;
i = len;
while (i-- > 0)

View File

@ -118,6 +118,10 @@ _MSKanji_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
if (n < 2)
/* Incomplete multibyte sequence */
return ((size_t)-2);
if (*s == '\0') {
errno = EILSEQ;
return ((size_t)-1);
}
wc = (wc << 8) | (*s++ & 0xff);
len = 2;
}