diff options
author | Andy Shevchenko <andriy.shevchenko@linux.intel.com> | 2021-07-27 18:01:32 +0300 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2021-09-15 10:02:16 +0200 |
commit | b3bf0743a6cc4667931fae6551751ec23ed487d2 (patch) | |
tree | 8e3138d9fcbf0017fc43f8c4e9a5ee0f03f829d7 /lib | |
parent | 148bef1825ae82114a40c759d738ae243c29499d (diff) | |
download | linux-stable-b3bf0743a6cc4667931fae6551751ec23ed487d2.tar.gz linux-stable-b3bf0743a6cc4667931fae6551751ec23ed487d2.tar.bz2 linux-stable-b3bf0743a6cc4667931fae6551751ec23ed487d2.zip |
lib/test_scanf: Handle n_bits == 0 in random tests
[ Upstream commit fe8e3ee0d588566c1f44f28a555042ef50eba491 ]
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 <oliver.sang@intel.com>
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Reviewed-by: Richard Fitzgerald <rf@opensource.cirrus.com>
Signed-off-by: Petr Mladek <pmladek@suse.com>
Link: https://lore.kernel.org/r/20210727150132.28920-1-andriy.shevchenko@linux.intel.com
Signed-off-by: Sasha Levin <sashal@kernel.org>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/test_scanf.c | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/lib/test_scanf.c b/lib/test_scanf.c index 84fe09eaf55e..abae88848972 100644 --- a/lib/test_scanf.c +++ b/lib/test_scanf.c @@ -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) \ |