This is the mail archive of the
newlib@sourceware.org
mailing list for the newlib project.
[patch/ob]: Fix invalid char handling in _wcstombs_r
- From: Corinna Vinschen <vinschen at redhat dot com>
- To: newlib at sourceware dot org
- Date: Tue, 19 Jan 2010 22:14:56 +0100
- Subject: [patch/ob]: Fix invalid char handling in _wcstombs_r
- Reply-to: newlib at sourceware dot org
Hi,
I just applied the below patch as obvious.
So far the _wcstombs_r function didn't handle invalid characters
correctly in the s == NULL case. Consider a simple case like this one:
ret = wcstombs (NULL, L"\0x33\0xffff", 0);
For instance, in the ASCII charset the wide character U+ffff has no
ASCII representation. So the string cannot be converted and the
expected return value per POSIX is -1. Without my patch this is ignored
and wcstombs/_wcstombs_r returns 0.
Corinna
* libc/stdlib/wcstombs_r.c (_wcstombs_r): Handle invalid characters
correctly also in the s==NULL case.
Index: libc/stdlib/wcstombs_r.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdlib/wcstombs_r.c,v
retrieving revision 1.4
diff -u -p -r1.4 wcstombs_r.c
--- libc/stdlib/wcstombs_r.c 18 Nov 2009 09:49:57 -0000 1.4
+++ libc/stdlib/wcstombs_r.c 19 Jan 2010 21:14:27 -0000
@@ -13,20 +13,25 @@ _DEFUN (_wcstombs_r, (reent, s, pwcs, n,
char *ptr = s;
size_t max = n;
char buff[8];
- int i, num_to_copy;
+ int i, bytes, num_to_copy;
if (s == NULL)
{
size_t num_bytes = 0;
while (*pwcs != 0)
- num_bytes += __wctomb (r, buff, *pwcs++, __locale_charset (), state);
+ {
+ bytes = __wctomb (r, buff, *pwcs++, __locale_charset (), state);
+ if (bytes == -1)
+ return -1;
+ num_bytes += bytes;
+ }
return num_bytes;
}
else
{
while (n > 0)
{
- int bytes = __wctomb (r, buff, *pwcs, __locale_charset (), state);
+ bytes = __wctomb (r, buff, *pwcs, __locale_charset (), state);
if (bytes == -1)
return -1;
num_to_copy = (n > bytes ? bytes : (int)n);
--
Corinna Vinschen
Cygwin Project Co-Leader
Red Hat