diff --git a/newlib/ChangeLog b/newlib/ChangeLog index 7d60fcce3..c3e6662d7 100644 --- a/newlib/ChangeLog +++ b/newlib/ChangeLog @@ -1,3 +1,10 @@ +2010-05-02 Corinna Vinschen + + * libc/stdlib/wctob.c (wctob): Reorganize and fix WEOF check. Rename + pwc to pmb and convert to array to avoid buffer overflow. Rename c to + wc. Check wc for WEOF instead of for EOF. Return first byte of pmb if + __wctomb conversion returned exactly one byte, EOF otherwise. + 2010-04-30 Corinna Vinschen * libc/include/langinfo.h: Fix #endif positioning. diff --git a/newlib/libc/stdlib/wctob.c b/newlib/libc/stdlib/wctob.c index 927c1a7a8..d97c01f24 100644 --- a/newlib/libc/stdlib/wctob.c +++ b/newlib/libc/stdlib/wctob.c @@ -1,26 +1,24 @@ #include #include -#include #include #include +#include #include "local.h" int -wctob (wint_t c) +wctob (wint_t wc) { mbstate_t mbs; - int retval = 0; - unsigned char pwc; + unsigned char pmb[MB_LEN_MAX]; + + if (wc == WEOF) + return EOF; /* Put mbs in initial state. */ memset (&mbs, '\0', sizeof (mbs)); _REENT_CHECK_MISC(_REENT); - retval = __wctomb (_REENT, &pwc, c, __locale_charset (), &mbs); - - if (c == EOF || retval != 1) - return WEOF; - else - return (int)pwc; + return __wctomb (_REENT, (char *) pmb, wc, __locale_charset (), &mbs) == 1 + ? (int) pmb[0] : EOF; }