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


On Sat, Jul 21, 2001 at 09:32:14AM -0700, Geoff Keating wrote:
> > 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();

As fputc takes an int as well, it makes more sense to write:

FILE* fp = fopen( "test", "w+" );
int ch = 0xF6;
fputc( ch, fp );
rewind( fp );
int inch = fgetc( fp );
if (ch != inch)
  abort();

Just get rid of the 'char' completely.

-- 
Carlo Wood <carlo@alinoe.com>


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