This is the mail archive of the
newlib@sourceware.org
mailing list for the newlib project.
[PATCH/ob]: Cast in toupper/tolower.
- From: Corinna Vinschen <vinschen at redhat dot com>
- To: newlib at sourceware dot org
- Date: Tue, 9 Jun 2009 13:34:00 +0200
- Subject: [PATCH/ob]: Cast in toupper/tolower.
- Reply-to: newlib at sourceware dot org
Hi,
I applied the below patch as obvious. The gawk testsuite uncovered the
bug. The new implementation of tolower/toupper is using mbtowc/wctomb
to convert characters >=0x80 to their upper/lower case counterparts.
The problem is that the return value is copied from a char to an int,
which will result in negative return values. As long as the result is
copied into a char, it won't get noticed. However, if it gets copied
into an int, and the int is used as index value, things get funny.
The below patch fixes this by casting the result to unsigned char when
copying it into the returned variable c.
Corinna
* libc/ctype/tolower.c (tolower): Cast conversion result from
mbtowc/wctomb to unsigned char to avoid negative return values.
* libc/ctype/toupper.c (toupper): Ditto.
Index: libc/ctype/tolower.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/ctype/tolower.c,v
retrieving revision 1.3
diff -u -p -r1.3 tolower.c
--- libc/ctype/tolower.c 31 Mar 2009 09:31:38 -0000 1.3
+++ libc/ctype/tolower.c 9 Jun 2009 11:32:10 -0000
@@ -67,7 +67,7 @@ _DEFUN(tolower,(c),int c)
wchar_t wc;
if (mbtowc (&wc, s, 1) >= 0
&& wctomb (s, (wchar_t) towlower ((wint_t) wc)) == 1)
- c = s[0];
+ c = (unsigned char) s[0];
}
return c;
#else
Index: libc/ctype/toupper.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/ctype/toupper.c,v
retrieving revision 1.3
diff -u -p -r1.3 toupper.c
--- libc/ctype/toupper.c 31 Mar 2009 09:31:38 -0000 1.3
+++ libc/ctype/toupper.c 9 Jun 2009 11:32:10 -0000
@@ -66,7 +66,7 @@ _DEFUN(toupper,(c),int c)
wchar_t wc;
if (mbtowc (&wc, s, 1) >= 0
&& wctomb (s, (wchar_t) towupper ((wint_t) wc)) == 1)
- c = s[0];
+ c = (unsigned char) s[0];
}
return c;
#else
--
Corinna Vinschen
Cygwin Project Co-Leader
Red Hat