From 333e8bae4208dc6ed3528b51366235ccb3bfc0cd Mon Sep 17 00:00:00 2001 From: UnknownProgrammer Date: Mon, 15 Aug 2016 22:52:12 +0200 Subject: [PATCH] Fix for System Overflow exception caused by casting a greater than int.MaxValue decimal to int. Fix limits offset to int.MaxValue respectively int.MinValue. --- OpenRA.Game/WPos.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/OpenRA.Game/WPos.cs b/OpenRA.Game/WPos.cs index 0966d6c849..c29c336240 100644 --- a/OpenRA.Game/WPos.cs +++ b/OpenRA.Game/WPos.cs @@ -63,10 +63,11 @@ namespace OpenRA if (pitch.Angle == 0) return ret; - // Add an additional quadratic variation to height - // Uses decimal to avoid integer overflow - var offset = (int)((decimal)(b - a).Length * pitch.Tan() * mul * (div - mul) / (1024 * div * div)); - return new WPos(ret.X, ret.Y, ret.Z + offset); + var tempoffset = (decimal)(b - a).Length * pitch.Tan() * mul * (div - mul) / (1024 * div * div); + + var offset = (int)Math.Min(Math.Max((decimal)int.MinValue, tempoffset + (decimal)ret.Z), (decimal)int.MaxValue); + + return new WPos(ret.X, ret.Y, offset); } public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode() ^ Z.GetHashCode(); }