Subject: Re: i386: miscompilaton or bogus C source?
To: Jonathan Stone <jonathan@DSG.Stanford.EDU>
From: Simon Burge <simonb@netbsd.org>
List: tech-toolchain
Date: 06/09/1999 11:11:07
Jonathan Stone wrote:

> 
> The following Intenret checksum code fragment (which is from Craig
> Partrdige, tuned for portability, rather than speed) worked fine on an
> i386 with gcc 2.7.2.2+myc1.
> 
> With our egcs-1.1.1, it produces incorrect results. If I #define'ing
> DEBUG (turning on the debugging fprintf()) i get correct results
> again.
> 
> Is this a bug in EGCS, or is the C code tripping over ANSI
> end-of-array rules?

> ...

>     if (extra)
>     {
> 	u_short rem;
> #if defined(DEBUG)
>   fprintf(stderr, "!\n");
> #endif
> 
> 	rem = 0;
> 	*(char *)&rem = buf[len-1];
> 	sum += rem;
>     }

Is this something that goes away without optimisation?  I had a case where some
code was casting between two types, and this was the reply I got from egcs-bugs:

	> It is incorrect to access the same hunk of memory using
	> different types like this.  Put them in an union or access
	> them with char * or void * pointers.

Now in your case you're accessing it with a char * pointer, so this
doesn't seem to be the problem.  What happens if you declare rem as a
u_char and just use "rem = buf[len-1];"?  Also, should the char * cast
be a u_char * cast, or doesn't that make a difference?

This may not help at all, but reminds me of a problem I once had.

Simon.