mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 15:57:45 +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
1 changed files with 3 additions and 1 deletions
|
@ -49,7 +49,9 @@ inline u32 unsigned_int(u32 max)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
u32 random = Test::randomness_source().draw_value(max, [&]() {
|
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;
|
return random;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue