All of lore.kernel.org
 help / color / mirror / Atom feed
From: Vyacheslav Dubeyko <slava@dubeyko.com>
To: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] fs: hfs: Fix comparison bug in hfs_cat_keycmp
Date: Wed, 10 Dec 2014 11:23:27 -0800	[thread overview]
Message-ID: <1418239407.2963.1.camel@ubuntu> (raw)
In-Reply-To: <1418229164-4132-1-git-send-email-linux@rasmusvillemoes.dk>

On Wed, 2014-12-10 at 17:32 +0100, Rasmus Villemoes wrote:
> Relying on the sign (after casting to int) of the difference of two
> quantities for comparison is usually wrong. For example, should a-b
> turn out to be 2^31, the return value of cmp(a,b) is -2^31; but that
> would also be the return value from cmp(b, a). So a compares less than
> b and b compares less than a. One can also easily find three values
> a,b,c such that a compares less than b, b compares less than c, but a
> does not compare less than c.
> 

Looks good for me. Thank you for the fix.

Reviewed-by: Vyacheslav Dubeyko <slava@dubeyko.com>

Thanks,
Vyacheslav Dubeyko.

> Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
> ---
>  fs/hfs/catalog.c | 14 ++++++++------
>  1 file changed, 8 insertions(+), 6 deletions(-)
> 
> diff --git a/fs/hfs/catalog.c b/fs/hfs/catalog.c
> index ff0316b925a5..db458ee3a546 100644
> --- a/fs/hfs/catalog.c
> +++ b/fs/hfs/catalog.c
> @@ -162,14 +162,16 @@ err2:
>   */
>  int hfs_cat_keycmp(const btree_key *key1, const btree_key *key2)
>  {
> -	int retval;
> +	__be32 k1p, k2p;
>  
> -	retval = be32_to_cpu(key1->cat.ParID) - be32_to_cpu(key2->cat.ParID);
> -	if (!retval)
> -		retval = hfs_strcmp(key1->cat.CName.name, key1->cat.CName.len,
> -				    key2->cat.CName.name, key2->cat.CName.len);
> +	k1p = key1->cat.ParID;
> +	k2p = key2->cat.ParID;
>  
> -	return retval;
> +	if (k1p != k2p)
> +		return be32_to_cpu(k1p) < be32_to_cpu(k2p) ? -1 : 1;
> +
> +	return hfs_strcmp(key1->cat.CName.name, key1->cat.CName.len,
> +			  key2->cat.CName.name, key2->cat.CName.len);
>  }
>  
>  /* Try to get a catalog entry for given catalog id */

      reply	other threads:[~2014-12-10 19:23 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-12-09 22:14 Question about hfs_cat_keycmp Rasmus Villemoes
2014-12-09 22:50 ` Vyacheslav Dubeyko
2014-12-10 16:32   ` [PATCH] fs: hfs: Fix comparison bug in hfs_cat_keycmp Rasmus Villemoes
2014-12-10 19:23     ` Vyacheslav Dubeyko [this message]

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=1418239407.2963.1.camel@ubuntu \
    --to=slava@dubeyko.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.