Re: SOLUTION (Re: Style question: comparison between signed and unsigned?)

Tom Dailey (dailey@deltanet.com)
Sat, 27 Sep 1997 09:46:59 -0700


Gerard Roudier wrote:
>
> On Fri, 26 Sep 1997, Tom Dailey wrote:
>
> > Morten Welinder wrote:
> > >
> > > Oops, I didn't look to see what was inside that sizeof()! Anyway,
> > > the idea is still sound as long as the two sizeof()s agree which,
> > > I claim, is common.
> > >
> > > Morten
> > > ("simplify the problem as much as possible, but no more than that"
> > > -- freely after Einstein.)
> >
> > Your solution is close to being correct, POSIXwise. The completely
> > "compliant" way of doing this is
> >
> > ssize_t read_return_value;
> > size_t nr_of_bytes_read;
> > ...
> > read_return_value = read ( socket, buffer, sizeof buffer );
> > if ( read_return_value == -1 ) {
> > ... /* Handle the error. */
> > }
> > nr_of_bytes_read = (size_t) read_return_value;
>
> Your cast is just stupid in my opinion.
> Your are just assuming that the called function only returns > 0 when it
> does not return -1.
> You probably deduce that from 'man read' and I am not sure it is
> reasonnable to expect from a compiler to do the same. ;-)
>
> Imagine the function that is called returns -2. Then your 'cast'
> has no sense at all.

I am making no assumptions, other than that this code is executing on
a POSIX-compliant UNIX system. According to ANSI/IEEE Std 1003.1,
section 6.4.1.3, "Upon successful completion, read() shall return an
integer indicating the number of bytes actually read. Otherwise, read()
shall return a value of -1 and set errno to indicate the error...".
>
> In the initial post the construct was:
>
> if (n < 0)
> ....
> else
> ...
>
> At the else statement, it is trivial that 'n' is never negative and
> the compiler warning is quite _useless_. No need for the compiler to
> be as knowledgeable as you are in order to handle this.
>
> > Then, nr_of_bytes_read can be dealt with in whatever way desired. I know
> > this is clumsy, but that is the price one pays for semantic overloading
> > of the return value from read. (By the way, ()s are not required around
> > sizeof's argument if it is a variable name -- only if it is a type
> > name.)
>
> My understanding of your proposal is: to use 2 variables and a signed to
> unsigned cast each time we call a function that returns a signed value
> that could be potentially compared to a sizeof() or some other unsigned
> value. I just hope I just misunderstood your suggestion.

No; you have understood my suggestion correctly. This seems to me to be
the
clearest way to deal with such an unfortunate situation. When comparing
the
return value to -1, it clearly makes the most sense for the return value
to be of a
signed type, as in fact the POSIX standard decrees (in this case,
ssize_t).
On the other hand, if the return value is to be compared to an unsigned
value,
such as a value of type size_t, then it makes the most sense for the
return
value to be of an unsigned type.

>
> Gerard.