LibTest: Fix integer overflow in Gen::unsigned_int(u32)

NumericLimits<u32>::max + 1 overflowing to 0 caused us to call
AK::get_random_uniform(0) which doesn't make sense (the argument is an
_exclusive_ bound).
This commit is contained in:
Martin Janiczek 2023-10-31 20:29:09 +01:00 committed by Tim Schumacher
parent 6e928e426a
commit 70ac6918d1
Notes: sideshowbarker 2024-07-17 16:23:55 +09:00

View file

@ -49,7 +49,9 @@ inline u32 unsigned_int(u32 max)
return 0;
u32 random = Test::randomness_source().draw_value(max, [&]() {
return AK::get_random_uniform(max + 1);
// `clamp` to guard against integer overflow and calling get_random_uniform(0).
u32 exclusive_bound = AK::clamp(max + 1, max, NumericLimits<u32>::max());
return AK::get_random_uniform(exclusive_bound);
});
return random;
}