mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Linus Torvalds <torvalds@linux-foundation.org>
To: David Howells <dhowells@redhat.com>
Cc: akpm@linux-foundation.org, linux-cachefs@redhat.com,
	linux-kernel@vger.kernel.org, Roel Kluin <roel.kluin@gmail.com>
Subject: Re: [PATCH] CacheFiles: Cleanup redundant tests on unsigned
Date: Thu, 29 Oct 2009 08:31:11 -0700 (PDT)	[thread overview]
Message-ID: <alpine.LFD.2.01.0910290826350.31845@localhost.localdomain> (raw)
In-Reply-To: <20091026155839.15099.72735.stgit@warthog.procyon.org.uk>



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

  reply	other threads:[~2009-10-29 15:31 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-10-26 15:58 David Howells
2009-10-29 15:31 ` Linus Torvalds [this message]
  -- strict thread matches above, loose matches on Subject: below --
2009-10-23 16:30 Roel Kluin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.LFD.2.01.0910290826350.31845@localhost.localdomain \
    --to=torvalds@linux-foundation.org \
    --cc=akpm@linux-foundation.org \
    --cc=dhowells@redhat.com \
    --cc=linux-cachefs@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=roel.kluin@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®