fs/hfs/catalog.c: fix comparison bug in hfs_cat_keycmp
authorRasmus Villemoes <linux@rasmusvillemoes.dk>
Wed, 10 Dec 2014 23:54:37 +0000 (15:54 -0800)
committerLinus Torvalds <torvalds@linux-foundation.org>
Thu, 11 Dec 2014 01:41:16 +0000 (17:41 -0800)
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.

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Reviewed-by: Vyacheslav Dubeyko <slava@dubeyko.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
fs/hfs/catalog.c

index ff0316b925a55b55beb8d241d2ef14fcfe75b060..db458ee3a546757947a909d1073cb0ac690f66a5 100644 (file)
@@ -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 */