This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: egcs, warning patch for missing initializers in xsys-protos.h
- To: egcs-patches at cygnus dot com, rth at cygnus dot com
- Subject: Re: egcs, warning patch for missing initializers in xsys-protos.h
- From: "Kaveh R. Ghazi" <ghazi at caip dot rutgers dot edu>
- Date: Sun, 20 Sep 1998 13:46:25 -0400 (EDT)
> From: Richard Henderson <rth@cygnus.com>
>
> On Sat, Sep 19, 1998 at 09:01:55AM -0400, Kaveh R. Ghazi wrote:
> > IMHO, you can't safely cast ctype function arguments in the
> > ctype macros. This was partially discussed last February starting with:
> > http://www.cygnus.com/ml/egcs/1998-Feb/0643.html. It became clear, in
> > that discussion, that casting to int was bad.
>
> What I see in that thread are a lot of what-ifs. I ask those
> involved the last time round: are there any actual systems that
> display actual problems?
Yes, see below.
>
> > The warning is pointing
> > out something valid, the fact that char is often signed and when you
> > evaluate character values > 127, you get negative results. Ctype macros
> > are often defined as lookups in an array, thus the negative index is
> > bogus.
>
> Not necessarily. For instance glibc defines things like this
>
> static unsigned short ctype_b[128+256] = {...}
> unsigned short *__ctype_b = &ctype_b[128];
>
> which means that negative values do work. Do actual implementations
> really get this wrong? Virtually all systems define EOF as -1 and
> so must do something like this anyway.
> r~
I believe linux is the exception here. I wrote a small
program to test this. (Feel free to point out to me if the test
program is bogus, if it is in fact so.)
> #include <stdio.h>
> #include <ctype.h>
>
> int main()
> {
> int i;
> signed char sc;
> unsigned char uc;
>
> for (i=128; i<256; i++)
> {
> sc = uc = i;
> if (isalpha(sc) != isalpha(uc))
> printf("(isalpha(%c)[%d]=%d) != (isalpha(%c)[%d]=%d)\n",
> sc, sc, isalpha(sc), uc, uc, isalpha(uc));
> }
>
> return 0;
> }
If signed vs unsigned chars passed to ctype functions doesn't
matter, then the program should produce no output. Here are the
actual results.
sparc-sun-sunos4.1.4 FAILED
sparc-sun-solaris2.5 FAILED
alphaev5-dec-osf4.0b FAILED
powerpc-ibm-aix4.1.4.0 PASS
mips-sgi-irix4.0.5 core dumped
mips-sgi-irix6.2 FAILED
sparc-unknown-linux-gnulibc1 PASS
As you can see, most of the platforms failed.
--Kaveh
--
Kaveh R. Ghazi Engagement Manager / Project Services
ghazi@caip.rutgers.edu Icon CMT Corp.