Skip to content

Commit ddbc22e

Browse files
Villemoestorvalds
authored andcommitted
fs/hfs/catalog.c: fix comparison bug in hfs_cat_keycmp
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>
1 parent 705304a commit ddbc22e

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

‎fs/hfs/catalog.c‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,16 @@ int hfs_cat_create(u32 cnid, struct inode *dir, struct qstr *str, struct inode *
162162
*/
163163
int hfs_cat_keycmp(const btree_key *key1, const btree_key *key2)
164164
{
165-
int retval;
165+
__be32 k1p, k2p;
166166

167-
retval = be32_to_cpu(key1->cat.ParID) - be32_to_cpu(key2->cat.ParID);
168-
if (!retval)
169-
retval = hfs_strcmp(key1->cat.CName.name, key1->cat.CName.len,
170-
key2->cat.CName.name, key2->cat.CName.len);
167+
k1p = key1->cat.ParID;
168+
k2p = key2->cat.ParID;
171169

172-
return retval;
170+
if (k1p != k2p)
171+
return be32_to_cpu(k1p) < be32_to_cpu(k2p) ? -1 : 1;
172+
173+
return hfs_strcmp(key1->cat.CName.name, key1->cat.CName.len,
174+
key2->cat.CName.name, key2->cat.CName.len);
173175
}
174176

175177
/* Try to get a catalog entry for given catalog id */

0 commit comments

Comments
 (0)