Skip to content

Commit fe8e3ee

Browse files
andy-shevpmladek
authored andcommitted
lib/test_scanf: Handle n_bits == 0 in random tests
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: 50f530e ("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]
1 parent e563592 commit fe8e3ee

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

lib/test_scanf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ static u32 __init next_test_random(u32 max_bits)
271271
{
272272
u32 n_bits = hweight32(prandom_u32_state(&rnd_state)) % (max_bits + 1);
273273

274-
return prandom_u32_state(&rnd_state) & (UINT_MAX >> (32 - n_bits));
274+
return prandom_u32_state(&rnd_state) & GENMASK(n_bits, 0);
275275
}
276276

277277
static unsigned long long __init next_test_random_ull(void)
@@ -280,7 +280,7 @@ static unsigned long long __init next_test_random_ull(void)
280280
u32 n_bits = (hweight32(rand1) * 3) % 64;
281281
u64 val = (u64)prandom_u32_state(&rnd_state) * rand1;
282282

283-
return val & (ULLONG_MAX >> (64 - n_bits));
283+
return val & GENMASK_ULL(n_bits, 0);
284284
}
285285

286286
#define random_for_type(T) \

0 commit comments

Comments
 (0)