Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH] xfs: fix diff_two_keys calculation for cnt btree
@ 2025-04-26 13:42 Fedor Pchelkin
  2025-04-26 15:03 ` Darrick J. Wong
  0 siblings, 1 reply; 5+ messages in thread
From: Fedor Pchelkin @ 2025-04-26 13:42 UTC (permalink / raw)
  To: Carlos Maiolino, Darrick J. Wong
  Cc: Fedor Pchelkin, Chandan Babu R, Brian Foster, linux-xfs,
	linux-kernel, lvc-project, Alexey Nepomnyashih, stable

Currently the difference is computed on 32-bit unsigned values although
eventually it is stored in a variable of int64_t type. This gives awkward
results, e.g. when the diff _should_ be negative, it is represented as
some large positive int64_t value.

Perform the calculations directly in int64_t as all other diff_two_keys
routines actually do.

Found by Linux Verification Center (linuxtesting.org) with Svace static
analysis tool.

Fixes: 08438b1e386b ("xfs: plumb in needed functions for range querying of the freespace btrees")
Cc: stable@vger.kernel.org
Signed-off-by: Fedor Pchelkin <pchelkin@ispras.ru>
---
 fs/xfs/libxfs/xfs_alloc_btree.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/fs/xfs/libxfs/xfs_alloc_btree.c b/fs/xfs/libxfs/xfs_alloc_btree.c
index a4ac37ba5d51..b3c54ae90e25 100644
--- a/fs/xfs/libxfs/xfs_alloc_btree.c
+++ b/fs/xfs/libxfs/xfs_alloc_btree.c
@@ -238,13 +238,13 @@ xfs_cntbt_diff_two_keys(
 	ASSERT(!mask || (mask->alloc.ar_blockcount &&
 			 mask->alloc.ar_startblock));
 
-	diff =  be32_to_cpu(k1->alloc.ar_blockcount) -
-		be32_to_cpu(k2->alloc.ar_blockcount);
+	diff = (int64_t)be32_to_cpu(k1->alloc.ar_blockcount) -
+			be32_to_cpu(k2->alloc.ar_blockcount);
 	if (diff)
 		return diff;
 
-	return  be32_to_cpu(k1->alloc.ar_startblock) -
-		be32_to_cpu(k2->alloc.ar_startblock);
+	return (int64_t)be32_to_cpu(k1->alloc.ar_startblock) -
+			be32_to_cpu(k2->alloc.ar_startblock);
 }
 
 static xfs_failaddr_t
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-05-05 10:56 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-26 13:42 [PATCH] xfs: fix diff_two_keys calculation for cnt btree Fedor Pchelkin
2025-04-26 15:03 ` Darrick J. Wong
2025-04-27 11:56   ` Fedor Pchelkin
2025-04-27 14:25     ` Kent Overstreet
2025-05-05 10:47   ` Fedor Pchelkin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox