From: SmallJoker Date: Sat, 29 Jul 2017 17:01:14 +0000 (+0200) Subject: Noise: Prevent unittest crash caused by division by zero X-Git-Tag: 5.0.0~989 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=765fd9a0bc0bb9f08d12713dc586e7f4c59c421d;p=oweals%2Fminetest.git Noise: Prevent unittest crash caused by division by zero --- diff --git a/src/noise.cpp b/src/noise.cpp index f67771b88..e6ca8a495 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; }