Suggested warning: "negating an expression of unsigned type does not yield a negative value"

Joe Buck jbuck@synopsys.com
Mon Oct 6 17:48:00 GMT 2003


On Mon, Oct 06, 2003 at 06:11:05PM +0200, Falk Hueffner wrote:
> Joe Buck <jbuck@synopsys.com> writes:
> > The C and C++ standards require that unsigned values obey modulo 2**N
> > arithmetic, so the value of -x is rigorously defined.
> 
> Sure it is. But it is not what is intended. Example: x = 5, then
> -x=4294967291, i.e., p will be advanced by 4294967291 bytes, which is
> way beyond the legal range of p, but happens to work anyway on 32 bit
> architectures (but not on 64 bit architectures).

Good catch.  It seems that the real problem is not negation of unsigned
values, but negation followed by extension to a potentially larger word size.
If you can figure out how to warn specifically about that, it might
be worth adding.  But it would be tricky.

 



More information about the Gcc mailing list