linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Subject: [PATCH next] ext4: super: Use clamp() instead of clamp_val() to bound timestamps
@ 2024-01-17 22:47 David Laight
  0 siblings, 0 replies; only message in thread
From: David Laight @ 2024-01-17 22:47 UTC (permalink / raw)
  To: linux-kernel, linux-ext4, Arnd Bergmann
  Cc: Theodore Y. Ts'o, 'adilger.kernel@dilger.ca'

Commit 6a0678a79bb3a extended the superblock timestamps to 40 bits.
The time64_t (signed 64bit) was bounded using:
	now = clamp_val(now, 0, (1ull << 40) - 1);
which is equivalent to:
	now = clamp(now, (typeof(now))0, (typeof(now))((1ull << 40) - 1));
However clamp_val() is an 'accident waiting to happen' because it
is very easy to specify bounds that get masked by the cast.
The current clamp() only requires the three values to have the same
signedness - not the same types.
So changing the upper limit to a signed value allows clamp() be used.

This is the only place in the kernel I build where replacing clamp_val()
with clamp() generates a compile-time error.

This is a similar 'problem' to code like:
	unsigned int val = ...
	u8 bounded = min_t(u8, val, 255);
which is surprisingly common and has been a real bug.

Signed-off-by: David Laight <david.laight@aculab.com>
---
 fs/ext4/super.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index 0980845c8b8f..714d51a1667b 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -425,7 +425,7 @@ void ext4_itable_unused_set(struct super_block *sb,
 
 static void __ext4_update_tstamp(__le32 *lo, __u8 *hi, time64_t now)
 {
-	now = clamp_val(now, 0, (1ull << 40) - 1);
+	now = clamp(now, 0, (1ll << 40) - 1);
 
 	*lo = cpu_to_le32(lower_32_bits(now));
 	*hi = upper_32_bits(now);
-- 
2.17.1

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2024-01-17 22:49 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-17 22:47 Subject: [PATCH next] ext4: super: Use clamp() instead of clamp_val() to bound timestamps David Laight

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).