From 333e8bae4208dc6ed3528b51366235ccb3bfc0cd Mon Sep 17 00:00:00 2001 From: UnknownProgrammer Date: Mon, 15 Aug 2016 22:52:12 +0200 Subject: [PATCH 1/2] 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(); } From 65fe88daefc57f0d54c02c4a4e0290de3286060b Mon Sep 17 00:00:00 2001 From: UnknownProgrammer Date: Sat, 3 Sep 2016 15:27:04 +0200 Subject: [PATCH 2/2] used clamp instead of Math.Min/Math.Max construct clarified naming restored comments removed blank line --- 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 c29c336240..6ea08e0b78 100644 --- a/OpenRA.Game/WPos.cs +++ b/OpenRA.Game/WPos.cs @@ -63,11 +63,12 @@ namespace OpenRA if (pitch.Angle == 0) return ret; - var tempoffset = (decimal)(b - a).Length * pitch.Tan() * mul * (div - mul) / (1024 * div * div); + // Add an additional quadratic variation to height + // Uses decimal to avoid integer overflow + var offset = (decimal)(b - a).Length * pitch.Tan() * mul * (div - mul) / (1024 * div * div); + var clampedOffset = (int)(offset + (decimal)ret.Z).Clamp((decimal)int.MinValue, (decimal)int.MaxValue); - 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); + return new WPos(ret.X, ret.Y, clampedOffset); } public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode() ^ Z.GetHashCode(); }