Added check for n<0 in SDL_rand_n()

This commit is contained in:
John Kaniarz 2024-06-19 16:51:21 -04:00 committed by Sam Lantinga
parent 38cac043af
commit 8a80f41b77
1 changed files with 8 additions and 0 deletions

View File

@ -67,6 +67,14 @@ Sint32 SDL_rand_n(Sint32 n)
// fixed point number. Multiply by the 31.0 bit n to get a 31.32 bit
// result. Shift right by 32 to get the 31 bit integer that we want.
if (n < 0) {
// The algorithm looks like it works for numbers < 0 but it has an
// infintesimal chance of returning a value out of range.
// Returning -SDL_rand_n(abs(n)) blows up at INT_MIN instead.
// It's easier to just say no.
return 0;
}
// On 32-bit arch, the compiler will optimize to a single 32-bit multiply
Uint64 val = (Uint64)SDL_rand_bits() * n;
return (Sint32)(val >> 32);