]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
xfs: fix sign handling problem in xfs_bmbt_diff_two_keys
authorDarrick J. Wong <darrick.wong@oracle.com>
Fri, 15 Nov 2019 22:16:21 +0000 (17:16 -0500)
committerEric Sandeen <sandeen@redhat.com>
Fri, 15 Nov 2019 22:16:21 +0000 (17:16 -0500)
Source kernel commit: b521c89027f4b86af418083cb75db60a0e7a5997

In xfs_bmbt_diff_two_keys, we perform a signed int64_t subtraction with
two unsigned 64-bit quantities.  If the second quantity is actually the
"maximum" key (all ones) as used in _query_all, the subtraction
effectively becomes addition of two positive numbers and the function
returns incorrect results.  Fix this with explicit comparisons of the
unsigned values.  Nobody needs this now, but the online repair patches
will need this to work properly.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Reviewed-by: Dave Chinner <dchinner@redhat.com>
Signed-off-by: Eric Sandeen <sandeen@sandeen.net
libxfs/xfs_bmap_btree.c

index 3c1945a16747d37aaa4674f5bb2d76237f219584..443816867af6895bc8d2137bf94e53f8a8cd6680 100644 (file)
@@ -398,8 +398,20 @@ xfs_bmbt_diff_two_keys(
        union xfs_btree_key     *k1,
        union xfs_btree_key     *k2)
 {
-       return (int64_t)be64_to_cpu(k1->bmbt.br_startoff) -
-                         be64_to_cpu(k2->bmbt.br_startoff);
+       uint64_t                a = be64_to_cpu(k1->bmbt.br_startoff);
+       uint64_t                b = be64_to_cpu(k2->bmbt.br_startoff);
+
+       /*
+        * Note: This routine previously casted a and b to int64 and subtracted
+        * them to generate a result.  This lead to problems if b was the
+        * "maximum" key value (all ones) being signed incorrectly, hence this
+        * somewhat less efficient version.
+        */
+       if (a > b)
+               return 1;
+       if (b > a)
+               return -1;
+       return 0;
 }
 
 static xfs_failaddr_t