[Patch] Fix newlib/libc/stdio/fgetws.c issue
Yvan ROUX
yvan.roux@st.com
Wed Jun 6 06:47:00 GMT 2012
Hi Corinna,
I wasn't aware of the mbsnrtowcs function existence, but it is indeed a far
better solution.
Thanks
Yvan
On Wed, May 30, 2012 at 02:34:31PM +0200, Corinna Vinschen wrote:
> On May 30 13:22, Yvan ROUX wrote:
> > Hi,
> >
> > The actual behaviour of the fgetws function doesn't match with its specs as it
> > doesn't read *at most* n-1 wide characters until a newline, but *every* wide
> > character until a newline or in buffers if no newline are found. This patch
> > fixes this issue.
>
> Thanks for the patch, but I think the bug in fgetws is a bit more
> convoluted. The expressions:
>
> nl = memchr (fp->_p, '\n', fp->_r);
>
> and
>
> nl != NULL ? (nl - fp->_p + 1) : fp->_r
>
> determine the number of bytes to read from the input stream, but `n'
> determines the number of maximum wide chars to produce. So using
> the result of the `nl' expressions to compute the number of wide chars
> is plain wrong.
>
> So, what about this instead? It uses the `nl' expressions to compute
> the maximum number of input bytes and n to determine the maximum number
> of wide chars in a call to mbsnrtowcs:
>
> Index: libc/stdio/fgetws.c
> ===================================================================
> RCS file: /cvs/src/src/newlib/libc/stdio/fgetws.c,v
> retrieving revision 1.5
> diff -u -p -r1.5 fgetws.c
> --- libc/stdio/fgetws.c 30 May 2012 08:58:42 -0000 1.5
> +++ libc/stdio/fgetws.c 30 May 2012 12:33:08 -0000
> @@ -110,9 +110,13 @@ _DEFUN(_fgetws_r, (ptr, ws, n, fp),
> {
> src = (char *) fp->_p;
> nl = memchr (fp->_p, '\n', fp->_r);
> - nconv = _mbsrtowcs_r (ptr, wsp, &src,
> - nl != NULL ? (nl - fp->_p + 1) : fp->_r,
> - &fp->_mbstate);
> + nconv = _mbsnrtowcs_r (ptr, wsp, &src,
> + /* Read all bytes up to the next NL, or up to the
> + end of the buffer if there is no NL. */
> + nl != NULL ? (nl - fp->_p + 1) : fp->_r,
> + /* But never more than n - 1 wide chars. */
> + n - 1,
> + &fp->_mbstate);
> if (nconv == (size_t) -1)
> /* Conversion error */
> goto error;
>
>
> Corinna
>
> --
> Corinna Vinschen
> Cygwin Project Co-Leader
> Red Hat
More information about the Newlib
mailing list