warnings from use of ctype.h methods

Corinna Vinschen vinschen@redhat.com
Thu Mar 11 23:26:00 GMT 2010

On Mar 11 10:48, Joel Sherrill wrote:
> On 03/11/2010 10:27 AM, Eric Blake wrote:
> >On 03/11/2010 09:16 AM, Joel Sherrill wrote:
> >>warning: array subscript has type 'char'
> >>
> >>Most, if not all, of these appear to be from
> >>using ctype.h methods which is caused by
> >>a missing (int) case in line 57 in ctype.h:
> >>
> >>#define __ctype_lookup(__c) ((__ctype_ptr__+sizeof(""[(__c]))[(int)(__c)])
> >>
> >>Changing this to the following resolved the
> >>warnings.
> >>
> >>#define __ctype_lookup(__c)
> >>((__ctype_ptr__+sizeof(""[(int)__c]))[(int)(__c)])
> >>
> >>This is with newlib 1.18.0 with the current RTEMS
> >>patch.
> >>
> >>Does this cast need to be added?
> >Absolutely not.  The warning is _intentional_.  You have a bug in your
> >code - you are calling the ctype methods with a char argument, but char
> >is signed, and an 8-bit char is out of range and causes unspecified
> >results according to the standards.  In particular, that warning was
> >added because we had a demonstration of where isspace(255u) and
> >isspace(EOF) intentionally give different results in some locales, but
> >where the calling app had been doing isspace((char)255) and going into
> >an infinite loop because of their bug.  You should fix your code to use
> >unsigned char or int, as appropriate, in the first place.
> >
> Thanks for the explanation.  I will look
> at the cases in question and see what to do.  What
> do you recommend for the case where code is using
> a char * pointer to iterate through a string and then
> calling isXXX(*c)?  Add a cast to the call?  Or assign
> *c to an int local variable?  I can't change the pointer
> type or it won't iterate right.

You could assign the original pointer to a local unsigned char pointer
and use that for iterating.


Corinna Vinschen
Cygwin Project Co-Leader
Red Hat

More information about the Newlib mailing list