From 4be7d8b43a68664b6c349918ab79174ec6a3a24c Mon Sep 17 00:00:00 2001 From: SmallJoker Date: Sat, 29 Jul 2017 19:01:14 +0200 Subject: [PATCH] Noise: Prevent unittest crash caused by division by zero --- src/noise.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/noise.cpp b/src/noise.cpp index b918c9936..abd29f3ec 100644 --- a/src/noise.cpp +++ b/src/noise.cpp @@ -130,7 +130,9 @@ s32 PcgRandom::range(s32 min, s32 max) if (max < min) throw PrngException("Invalid range (max < min)"); - u32 bound = max - min + 1; + // We have to cast to s64 because otherwise this could overflow, + // and signed overflow is undefined behavior. + u32 bound = (s64)max - (s64)min + 1; return range(bound) + min; } -- 2.25.1