diff mbox series

Subject: [PATCH next] ext4: super: Use clamp() instead of clamp_val() to bound timestamps

Message ID 364a2f251cab4b2fbd43c661d51fcdd5@AcuMS.aculab.com
State New
Headers show
Series Subject: [PATCH next] ext4: super: Use clamp() instead of clamp_val() to bound timestamps | expand

Commit Message

David Laight Jan. 17, 2024, 10:47 p.m. UTC
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 mbox series

Patch

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);