This is the mail archive of the
newlib@sourceware.org
mailing list for the newlib project.
[Patch] Fix newlib/libc/stdio/fgetws.c issue
- From: Yvan ROUX <yvan dot roux at st dot com>
- To: "newlib at sourceware dot org" <newlib at sourceware dot org>
- Date: Wed, 30 May 2012 13:22:56 +0200
- Subject: [Patch] Fix newlib/libc/stdio/fgetws.c issue
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.
Cheers,
Yvan
2012-05-30 Yvan Roux <yvan.roux@st.com>
* libc/stdio/fgetws.c: Fix the number of multibytes converted to
wide-characters.
diff --git a/newlib/libc/stdio/fgetws.c b/newlib/libc/stdio/fgetws.c
index 2784f15..5567c53 100644
--- a/newlib/libc/stdio/fgetws.c
+++ b/newlib/libc/stdio/fgetws.c
@@ -92,6 +92,7 @@ _DEFUN(_fgetws_r, (ptr, ws, n, fp),
size_t nconv;
const char *src;
unsigned char *nl;
+ size_t wclen;
_flockfile (fp);
ORIENT (fp, 1);
@@ -106,13 +107,22 @@ _DEFUN(_fgetws_r, (ptr, ws, n, fp),
/* EOF */
goto error;
wsp = ws;
+
+ /* Leave space for the terminating null. */
+ n--;
+
do
{
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);
+
+ if (nl != NULL)
+ wclen = n < (nl - fp->_p + 1) ? n : (nl - fp->_p + 1);
+ else
+ wclen = n < fp->_r ? n : fp->_r;
+
+ nconv = _mbsrtowcs_r (ptr, wsp, &src, wclen, &fp->_mbstate);
+
if (nconv == (size_t) -1)
/* Conversion error */
goto error;