This is the mail archive of the
newlib@sourceware.org
mailing list for the newlib project.
Re: [Patch] Fix newlib/libc/stdio/fgetws.c issue
- From: Corinna Vinschen <vinschen at redhat dot com>
- To: newlib at sourceware dot org
- Date: Wed, 30 May 2012 14:34:31 +0200
- Subject: Re: [Patch] Fix newlib/libc/stdio/fgetws.c issue
- References: <20120530112254.GF25520@gnb.st.com>
- Reply-to: newlib at sourceware dot org
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