This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: GCC char* bug


> Date: Sat, 21 Jul 2001 06:59:23 -0700
> From: Bruce Korb <bkorb@pacbell.net>
> Cc: gcc-bugs@gcc.gnu.org, bkorb@pacbell.net

> The problem is this:
> 
>   char ch = 0xF6;
>   FILE* fp = fopen( "test", "w+" );
>   int inch;
>   fputc( ch, fp );
>   rewind( fp );
>   inch = fgetc( fp );
>   if (ch != inch)
>     abort();
> 
> This should _never_ abort on a platform where libc and
> the compiler agree on the sign-ed-ness of char.  It
> aborts on Linux with GCC.  My claim is that that is wrong.
> Either getc/fgetc needs fixing, or GCC does.  Either way.

Unfortunately, the code above may fail on any platform that
has a signed 'char'.  The C standard says that fgetc
returns "character as an *unsigned char* converted to an *int*."

You probably need to write

if ((unsigned char)ch != inch)
  abort();

note that the apparently-equivalent

if (ch != (char)inch)

is not guaranteed to work due to overflow (although I think it will be
fine on platforms where unix derivatives are used).

-- 
- Geoffrey Keating <geoffk@geoffk.org>


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]