Browse Source

Fix signed overflow UB in PseudoRandom::next()

DS 3 months ago
parent
commit
e416c99419
1 changed files with 4 additions and 3 deletions
  1. 4 3
      src/noise.h

+ 4 - 3
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<u32>(m_next) * 1103515245U + 12345U;
+		// 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)
@@ -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<u32>(max - min) > (RANDOM_RANGE + 1) / 5)
 			throw PrngException("Range too large");
 
 		return (next() % (max - min + 1)) + min;