forked from Mirrorlandia_minetest/minetest
Fix signed overflow UB in PseudoRandom::next()
This commit is contained in:
parent
7c9706fdcf
commit
e416c99419
@ -56,8 +56,9 @@ public:
|
|||||||
|
|
||||||
inline u32 next()
|
inline u32 next()
|
||||||
{
|
{
|
||||||
m_next = m_next * 1103515245 + 12345;
|
m_next = static_cast<u32>(m_next) * 1103515245U + 12345U;
|
||||||
return (u32)(m_next / 65536) % (RANDOM_RANGE + 1);
|
// Signed division is required due to backwards compatibility
|
||||||
|
return static_cast<u32>(m_next / 65536) % (RANDOM_RANGE + 1U);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline s32 range(s32 min, s32 max)
|
inline s32 range(s32 min, s32 max)
|
||||||
@ -70,7 +71,7 @@ public:
|
|||||||
PcgRandom, we cannot modify this RNG's range as it would change the
|
PcgRandom, we cannot modify this RNG's range as it would change the
|
||||||
output of this RNG for reverse compatibility.
|
output of this RNG for reverse compatibility.
|
||||||
*/
|
*/
|
||||||
if ((u32)(max - min) > (RANDOM_RANGE + 1) / 5)
|
if (static_cast<u32>(max - min) > (RANDOM_RANGE + 1) / 5)
|
||||||
throw PrngException("Range too large");
|
throw PrngException("Range too large");
|
||||||
|
|
||||||
return (next() % (max - min + 1)) + min;
|
return (next() % (max - min + 1)) + min;
|
||||||
|
Loading…
Reference in New Issue
Block a user