From 19585ec7ac7402c66edfa2254a5a468e102b5637 Mon Sep 17 00:00:00 2001 From: Ashley Newson Date: Tue, 28 Jan 2025 19:31:14 +0000 Subject: [PATCH] Fix overflow when calculating array quantile index Fixes a crash which happens when trying to generate very large maps, such as 256x256. ``` Exception of type `System.IndexOutOfRangeException`: Index was outside the bounds of the array. at OpenRA.Mods.Common.MapGenerator.MatrixUtils.CalibrateQuantileInPlace(Matrix`1 matrix, Int32 target, Int32 count, Int32 outOf) in /home/ashley/devel/OpenRA/OpenRA.Mods.Common/MapGenerator/MatrixUtils.cs:line 719 at OpenRA.Mods.Common.Traits.RaMapGenerator.Generate(Map map, MiniYaml settings) in /home/ashley/devel/OpenRA/OpenRA.Mods.Common/Traits/World/RaMapGenerator.cs:line 755 ... ``` --- OpenRA.Mods.Common/MapGenerator/MatrixUtils.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OpenRA.Mods.Common/MapGenerator/MatrixUtils.cs b/OpenRA.Mods.Common/MapGenerator/MatrixUtils.cs index 3c5c34b290..16161daebe 100644 --- a/OpenRA.Mods.Common/MapGenerator/MatrixUtils.cs +++ b/OpenRA.Mods.Common/MapGenerator/MatrixUtils.cs @@ -718,7 +718,7 @@ namespace OpenRA.Mods.Common.MapGenerator { var sorted = (int[])matrix.Data.Clone(); Array.Sort(sorted); - var adjustment = target - sorted[(sorted.Length - 1) * count / outOf]; + var adjustment = target - sorted[(long)(sorted.Length - 1) * count / outOf]; for (var i = 0; i < matrix.Data.Length; i++) matrix[i] += adjustment; }