[PATCH] C undefined behavior fix

Paul Mackerras paulus@samba.org
Fri Jan 4 15:26:00 GMT 2002


Linus Torvalds writes:

> >I don't have a copy of the C standard handy, but that sounds to me
> >like the result of (unsigned long)(&x) - KERNELBASE is undefined.
> 
> It is not. It is implementation-defined, because the cast from the
> pointer to the "unsigned long" is implementation defined (and the
> subtraction is 100% well-defined, of course ;)

Ah, thank you.

What I find amusing is that if I write a function add() in assembly to
return the sum of its two arguments, then

	*(char *)add((unsigned long)("foo"), offset)

is implementation-defined, whereas

	*(char *)((unsigned long)("foo") + offset)

is supposedly undefined, for offset > 3. :)

In other words, _hiding_ some information from the compiler turns
undefined behaviour into implementation-defined behaviour.  Weird.

Paul.



More information about the Gcc mailing list