From e416c99419ec879aa12d069753f1fc254df73d99 Mon Sep 17 00:00:00 2001 From: DS Date: Fri, 19 Jan 2024 11:52:53 +0100 Subject: [PATCH] Fix signed overflow UB in PseudoRandom::next() --- src/noise.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/noise.h b/src/noise.h index 1302e1bef..c864c8fbb 100644 --- a/src/noise.h +++ b/src/noise.h @@ -56,8 +56,9 @@ public: inline u32 next() { - m_next = m_next * 1103515245 + 12345; - return (u32)(m_next / 65536) % (RANDOM_RANGE + 1); + m_next = static_cast(m_next) * 1103515245U + 12345U; + // Signed division is required due to backwards compatibility + return static_cast(m_next / 65536) % (RANDOM_RANGE + 1U); } 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 output of this RNG for reverse compatibility. */ - if ((u32)(max - min) > (RANDOM_RANGE + 1) / 5) + if (static_cast(max - min) > (RANDOM_RANGE + 1) / 5) throw PrngException("Range too large"); return (next() % (max - min + 1)) + min;