fs: Clear a UBSAN shift-out-of-bounds warning
PATCH 1/1 fs: Clear a UBSAN shift-out-of-bounds warning
UBSAN: shift-out-of-bounds in fs/locks.c:2572:16
left shift of 1 by 63 places cannot be represented in type 'long long int'
Switch the calculation method to ((quarter - 1) * 2 + 1) can help us eliminate this false positive.
On the other hand, the old implementation has problems with char and short types, although not currently involved.printf("%d: %x\n", sizeof(char), INT_LIMIT(char)); printf("%d: %x\n", sizeof(short), INT_LIMIT(short));
1: ffffff7f
2: ffff7fff
--- include/linux/fs.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/fs.h +1 -1
@@ -1131,7 +1131,7 @@ struct file_lock_context {/* The following constant reflects the upper bound of the file/locking space */ #ifndef OFFSET_MAX -#define INT_LIMIT (x) (~((x)1 << (sizeof(x)*8 - 1))) +#define INT_LIMIT (x) ((((x)1 << (sizeof(x) * 8 - 2)) - 1) * 2 + 1)#define OFFSET_MAX INT_LIMIT(loff_t)#define OFFT_OFFSET_MAX INT_LIMIT(off_t)#endif
Signature · 1 line
2.25.1
Quoted 23 lines · Zhen Lei
UBSAN: shift-out-of-bounds in fs/locks.c:2572:16left shift of 1 by 63 places cannot be represented in type 'long long int'
Switch the calculation method to ((quarter - 1) * 2 + 1) can help us eliminate this false positive.
On the other hand, the old implementation has problems with char and short types, although not currently involved.
printf("%d: %x\n", sizeof(char), INT_LIMIT(char));printf("%d: %x\n", sizeof(short), INT_LIMIT(short));
1:ffffff7f
2:ffff7fffSigned-off-by Zhen Lei <[email protected]>--- include/linux/fs.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/linux/fs.h +0
-0
include/linux/fs.h struct file_lock_context { · line 1137
Quoted 8 lines
include/linux/fs.h +1
-1@@ -1131,7 +1131,7 @@ struct file_lock_context {/* The following constant reflects the upper bound of the file/locking space */ #ifndef OFFSET_MAX -#define INT_LIMIT (x) (~((x)1 << (sizeof(x)*8 - 1))) +#define INT_LIMIT (x) ((((x)1 << (sizeof(x) * 8 - 2)) - 1) * 2 + 1)#define OFFSET_MAX INT_LIMIT(loff_t)#define OFFT_OFFSET_MAX INT_LIMIT(off_t)#endif
This problem has already been solved by type_max() in include/linux/overflow.h. How about removing INT_LIMIT() and using type_max() instead?
- Eric
Quoted 25 lines · Eric Biggers
Quoted 24 lines · Zhen Lei
UBSAN: shift-out-of-bounds in fs/locks.c:2572:16left shift of 1 by 63 places cannot be represented in type 'long long int'
Switch the calculation method to ((quarter - 1) * 2 + 1) can help us eliminate this false positive.
On the other hand, the old implementation has problems with char and short types, although not currently involved.
printf("%d: %x\n", sizeof(char), INT_LIMIT(char));printf("%d: %x\n", sizeof(short), INT_LIMIT(short));
1:ffffff7f
2:ffff7fffSigned-off-by Zhen Lei <[email protected]>--- include/linux/fs.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/linux/fs.h +0
-0@@ -1131,7 +1131,7 @@ struct file_lock_context {
Quoted 10 lines
include/linux/fs.h struct file_lock_context { · line 1137
Quoted 7 lines
include/linux/fs.h +1
-1/* The following constant reflects the upper bound of the file/locking space */ #ifndef OFFSET_MAX -#define INT_LIMIT (x) (~((x)1 << (sizeof(x)*8 - 1))) +#define INT_LIMIT (x) ((((x)1 << (sizeof(x) * 8 - 2)) - 1) * 2 + 1)#define OFFSET_MAX INT_LIMIT(loff_t)#define OFFT_OFFSET_MAX INT_LIMIT(off_t)#endif This problem has already been solved by
type_max()ininclude/linux/overflow.h. How about removingINT_LIMIT()and usingtype_max()instead?
It's better to use type_max(). INT_LIMIT() is currently valid only for signed type. Besides, my method is actually the same as type_max(). Using type_max() does not result in code duplication.
Quoted 4 lines
- Eric
.
Signature · 2 lines
Regards, Zhen Lei