Skip to content

Commit 0f3819e

Browse files
konisakpm00
authored andcommitted
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 5a581b3 ("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/[email protected] Link: https://lkml.kernel.org/r/[email protected] Fixes: 9ff0512 ("nilfs2: segment constructor") Signed-off-by: Ryusuke Konishi <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 8547d11 commit 0f3819e

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

fs/nilfs2/segment.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ static void nilfs_dispose_list(struct the_nilfs *, struct list_head *, int);
136136

137137
#define nilfs_cnt32_ge(a, b) \
138138
(typecheck(__u32, a) && typecheck(__u32, b) && \
139-
((__s32)(a) - (__s32)(b) >= 0))
139+
((__s32)((a) - (b)) >= 0))
140140

141141
static int nilfs_prepare_segment_lock(struct super_block *sb,
142142
struct nilfs_transaction_info *ti)

0 commit comments

Comments
 (0)