Re: [PATCH] CacheFiles: Cleanup redundant tests on unsigned

From: Linus Torvalds
Date: Thu Oct 29 2009 - 11:31:56 EST




On Mon, 26 Oct 2009, David Howells wrote:
>
> From: Roel Kluin <roel.kluin@xxxxxxxxx>
>
> The variables are unsigned so the test `>= 0' is always true,
> the `< 0' test always fails. The other part of
> the test catches wrapped values.

This is an excellent example of why I think that some gcc warnings are
pure and utter sh*t, and why just blindly trying to avoid them then leads
to worse code.

> - if (datalen < 0 || datalen > PAGE_SIZE - 1)
> + if (datalen > PAGE_SIZE - 1)
> - if (fstop < 0 || fstop >= cache->fcull_percent)
> + if (fstop >= cache->fcull_percent)
> - if (bstop < 0 || bstop >= cache->bcull_percent)
> + if (bstop >= cache->bcull_percent)

You've now actively made the code more fragile, only to avoid a warning.

The old code was clearly correct. The new code subtle depends on the type
of comparison.

I _hate_ those idiotic warnings, and in this case the "warning-free" code
is actively worse than the original.

A smart compiler would see that it's a range check, and one that could
have been done as an unsigned comparison (well, for the constant compare
case) regardless of the type of the variable being tested. So a _smart_
compiler wouldn't complain, but it might use the signedness information to
silently simplify the comparison.

A _stupid_ compiler complains, and thus forces people to either ignore the
warning, or make the code worse.

Which one would you prefer?

Linus
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/