Re: Style question: comparison between signed and unsigned?

Ulrich Drepper (drepper@ipd.info.uni-karlsruhe.de)
24 Sep 1997 00:03:11 +0200


"Theodore Y. Ts'o" <tytso@MIT.EDU> writes:

> OK, so your argument *is* to throw around casts with abandon. This
> causes another problem --- folks who add casts simply to shut up GCC.
>
> Linus's argument was that a programming style which encourages huge
> numbers of casts merely to shut up the warning mechanism is a bad idea.
> Here I will stand with Linus.

I don't say to use that casts should be used all over the place. I
guess that in about 80% of all signed<->unsigned changes we made for
glibc simply the type of the involved variables was wrong. And if
somebody knows C enough I also see no problem in using casts. This at
least shows we that this line of code caught the attention of the
programmer at some time and if s/he is good enough in her/his work I
assume the cast is correct.

> I had a proposal (which means a more work for the gcc developers) which
> would avoid the false positives without requiring huge numbers of casts.

I cannot judge about your proposal at this time since it certainly
requires more analysis.

What I would like to see and which catches most cases I came across is
that gcc does not warn in this case:

ssize_t n = read (...);
if (n < 0)
...
else
{
if (n < sizeof (foo))
...
}

At least when optimizing gcc knows that when executing the second `if'
the variable `n' cannot be negative and since
sizeof(ssize_t)==sizeof(size_t) there is no reason for a warning.

-- Uli
---------------. drepper@cygnus.com ,-. Rubensstrasse 5
Ulrich Drepper \ ,-------------------' \ 76149 Karlsruhe/Germany
Cygnus Solutions `--' drepper@gnu.ai.mit.edu `------------------------