]> Git Repo - linux.git/commitdiff
lib/test_scanf: Handle n_bits == 0 in random tests
authorAndy Shevchenko <[email protected]>
Tue, 27 Jul 2021 15:01:32 +0000 (18:01 +0300)
committerPetr Mladek <[email protected]>
Thu, 29 Jul 2021 07:52:33 +0000 (09:52 +0200)
UBSAN reported (via LKP)

[   11.021349][    T1] UBSAN: shift-out-of-bounds in lib/test_scanf.c:275:51
[   11.022782][    T1] shift exponent 32 is too large for 32-bit type 'unsigned int'

When n_bits == 0, the shift is out of range. Switch code to use GENMASK
to handle this case.

Fixes: 50f530e176ea ("lib: test_scanf: Add tests for sscanf number conversion")
Reported-by: kernel test robot <[email protected]>
Signed-off-by: Andy Shevchenko <[email protected]>
Reviewed-by: Richard Fitzgerald <[email protected]>
Signed-off-by: Petr Mladek <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
lib/test_scanf.c

index 48ff5747a4da00332db69381797951a0abf93460..6a828dfc8ea16d4047dccce58566c034760e58dd 100644 (file)
@@ -271,7 +271,7 @@ static u32 __init next_test_random(u32 max_bits)
 {
        u32 n_bits = hweight32(prandom_u32_state(&rnd_state)) % (max_bits + 1);
 
-       return prandom_u32_state(&rnd_state) & (UINT_MAX >> (32 - n_bits));
+       return prandom_u32_state(&rnd_state) & GENMASK(n_bits, 0);
 }
 
 static unsigned long long __init next_test_random_ull(void)
@@ -280,7 +280,7 @@ static unsigned long long __init next_test_random_ull(void)
        u32 n_bits = (hweight32(rand1) * 3) % 64;
        u64 val = (u64)prandom_u32_state(&rnd_state) * rand1;
 
-       return val & (ULLONG_MAX >> (64 - n_bits));
+       return val & GENMASK_ULL(n_bits, 0);
 }
 
 #define random_for_type(T)                             \
This page took 0.060917 seconds and 4 git commands to generate.