nilfs2: avoid undefined behavior in nilfs_cnt32_ge macro
authorRyusuke Konishi <konishi.ryusuke@gmail.com>
Tue, 2 Jul 2024 18:35:12 +0000 (03:35 +0900)
committerAndrew Morton <akpm@linux-foundation.org>
Fri, 5 Jul 2024 06:43:11 +0000 (23:43 -0700)
commit0f3819e8c483771a59cf9d3190cd68a7a990083c
treec6a3bc6e9169aa0546a234c502b9ae59cd4894e7
parent8547d1150f0dbd1d04f397c780182fc83ec2ab16
nilfs2: avoid undefined behavior in nilfs_cnt32_ge macro

According to the C standard 3.4.3p3, the result of signed integer overflow
is undefined.  The macro nilfs_cnt32_ge(), which compares two sequence
numbers, uses signed integer subtraction that can overflow, and therefore
the result of the calculation may differ from what is expected due to
undefined behavior in different environments.

Similar to an earlier change to the jiffies-related comparison macros in
commit 5a581b367b5d ("jiffies: Avoid undefined behavior from signed
overflow"), avoid this potential issue by changing the definition of the
macro to perform the subtraction as unsigned integers, then cast the
result to a signed integer for comparison.

Link: https://lkml.kernel.org/r/20130727225828.GA11864@linux.vnet.ibm.com
Link: https://lkml.kernel.org/r/20240702183512.6390-1-konishi.ryusuke@gmail.com
Fixes: 9ff05123e3bf ("nilfs2: segment constructor")
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
fs/nilfs2/segment.c