mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 12:05:15 +00:00
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:
parent
6e928e426a
commit
70ac6918d1
Notes:
sideshowbarker
2024-07-17 16:23:55 +09:00
Author: https://github.com/Janiczek Commit: https://github.com/SerenityOS/serenity/commit/70ac6918d1 Pull-request: https://github.com/SerenityOS/serenity/pull/21618 Reviewed-by: https://github.com/ADKaster Reviewed-by: https://github.com/timschumi ✅
1 changed files with 3 additions and 1 deletions
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue