diff --git a/newlib/ChangeLog b/newlib/ChangeLog index 0ed96068b..cf857246b 100644 --- a/newlib/ChangeLog +++ b/newlib/ChangeLog @@ -1,3 +1,8 @@ +2009-07-28 Corinna Vinschen + + * libc/stdlib/mbtowc_r.c (__utf8_mbtowc): Fix incrementing n in case + of handling incomplete sequences. + 2009-07-22 Eric Blake Avoid a fault from locking a closed standard file. diff --git a/newlib/libc/stdlib/mbtowc_r.c b/newlib/libc/stdlib/mbtowc_r.c index 010ce1da5..7e1362245 100644 --- a/newlib/libc/stdlib/mbtowc_r.c +++ b/newlib/libc/stdlib/mbtowc_r.c @@ -220,11 +220,7 @@ _DEFUN (__utf8_mbtowc, (r, pwc, s, n, charset, state), if (state->__count == 0) ch = t[i++]; else - { - if (n < (size_t)-1) - ++n; - ch = state->__value.__wchb[0]; - } + ch = state->__value.__wchb[0]; if (ch == '\0') { @@ -244,7 +240,10 @@ _DEFUN (__utf8_mbtowc, (r, pwc, s, n, charset, state), { /* two-byte sequence */ state->__value.__wchb[0] = ch; - state->__count = 1; + if (state->__count == 0) + state->__count = 1; + else if (n < (size_t)-1) + ++n; if (n < 2) return -2; ch = t[i++]; @@ -288,7 +287,10 @@ _DEFUN (__utf8_mbtowc, (r, pwc, s, n, charset, state), return -1; } state->__value.__wchb[1] = ch; - state->__count = 2; + if (state->__count == 1) + state->__count = 2; + else if (n < (size_t)-1) + ++n; if (n < 3) return -2; ch = t[i++]; @@ -347,7 +349,10 @@ _DEFUN (__utf8_mbtowc, (r, pwc, s, n, charset, state), return -1; } state->__value.__wchb[2] = ch; - state->__count = 3; + if (state->__count == 2) + state->__count = 3; + else if (n < (size_t)-1) + ++n; if (n < 4) return -2; ch = t[i++];