constant signed/unsigned comparison warning

Matthias Urlichs smurf@noris.de
Tue Sep 23 23:59:00 GMT 1997


This is from a recent email (from me) in the linux-kernel mailing list.

I believe it is also relevant to you folks.

Matthias Urlichs:
> 
>   Linus Torvalds <torvalds@transmeta.com> writes:
> > 
> > 	/* check that we got a valid packet */
> > 	if (i < sizeof(struct pkthdr))
> > 		return SHORT_PACKET;
> > 
> > Now, the PROBLEM is that if gcc warns about signed/unsigned comparisons
> > (which some versions of gcc do), gcc will totally needlessly (and in my
> > opinion incorrectly) warn about the second test (ie "i < sizeof(struct
> > pkthdr)"). Because "i" is signed, but "sizeof" is unsigned.
> 
> And because GCC knows how to figure out the size of the struct, but then
> forgets to check if the high bit of that size is set (otherwise you cannot
> run into a problem in the first place).
> 
> In other words, that warning _is_ a compiler bug. Fix the bug, please,
> don't play with removing the symptoms. Especially since they're 'only'
> warnings, anyway.
> 

-- 
Matthias Urlichs
noris network GmbH



More information about the Gcc mailing list