[Linux-cachefs] Re: [PATCH] CacheFiles: Cleanup redundant tests on unsigned

Linus Torvalds torvalds at linux-foundation.org
Thu Oct 29 15:31:11 UTC 2009



On Mon, 26 Oct 2009, David Howells wrote:
>
> From: Roel Kluin <roel.kluin at gmail.com>
> 
> 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




More information about the Linux-cachefs mailing list