Re: [PATCH] Remove pointless <0 comparison for unsigned variable in fs/fcntl.c

Timur Tabi
2004-11-23T19:20:09Z

Linus Torvalds wrote:

> The warning is sometimes useful, but when it comes to a construct like
>
> if (a < 0 || a > X)
>
> the fact that "a" is unsigned does not make the construct silly. First
> off, it's (a) very readable and (b) the type of "a" may not be immediately
> obvious if it's a user typedef, for example.
>
> In fact, the type of "a" might depend on the architecture, or even
> compiler flags. Think about "char" - which may or may not be signed
> depending on ABI and things like -funsigned-char.

If 'a' could be signed on some architectures and unsigned on others,
then I agree that "a < 0" is not silly. But if it's always going to be
unsigned, then I can't see how it's not silly.

> which might warn on an architecture where "pid_t" is just sixteen bits
> wide. Does that make the code wrong? Hell no.

Wouldn't something like "sizeof(pid_t) > 2" be a better test? It
certainly would be a lot easier to understand than comparing with 0xffff.

--
Timur Tabi
Staff Software Engineer
timur.tabi@ammasso.com