This is the mail archive of the gcc@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: constant signed/unsigned comparison warning


> Matthias Urlichs wrote:
> 
> > 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).
> 
> The problem is that, for all the compiler knows, this might be
> equivalent to
> 
>     int i = random_func_which_returns_minus_10000();
> 
>     if (i == -1) {
> 	...
>     }
>     else if (i < sizeof(foo)) {
> 	...
>     }
> 
> This case CAN be a problem, since the second comparison would be false,
> and this could very well be a thinko on the part of the programmer.

What about:

  ssize_t i = random_func_which_returns_minus_1_or_non_negative();

  if (i == -1) {
	...
  }
  else if (i < sizeof(foo)) {
	...
  }

The above is perfectly reasonable code, which produces a warning.
The random function could be anything returning ssize_t like read.
Casting to get rid of the warning is ugly.

The only solutions I can see are:

1) Cast. Ugly.
2) Disable warning. This is what happens in the real world.
   Unfortunately the usual method may be to avoid -Wall.
3) Make ssize_t a 'special type' (that suppresses this warning
   when comparing with a constant) in gcc. Ugly for gcc
   implementers. Nice for gcc users.

-- 
Erik Corry erik@arbat.com http://inet.uni-c.dk/~ehcorry/


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