{"record":{"id":"a400701915d237a3","repo":"stride3d/stride","slug":"indexoutofrangeexception-index","errorCode":null,"errorMessage":"IndexOutOfRangeException: index","messagePattern":"IndexOutOfRangeException: index","errorType":"exception","errorClass":"IndexOutOfRangeException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Physics/HeightStickArraySourceFromHeightmap.cs","lineNumber":40,"sourceCode":"\n        [DataMemberIgnore]\n        public Int2 HeightStickSize => Heightmap?.Size ?? default;\n\n        [DataMemberIgnore]\n        public Vector2 HeightRange => Heightmap?.HeightRange ?? default;\n\n        [DataMemberIgnore]\n        public float HeightScale => Heightmap?.HeightScale ?? default;\n\n        public bool IsValid() => Heightmap?.IsValid() ?? false;\n\n        public void CopyTo<T>(UnmanagedArray<T> heightStickArray, int index) where T : struct\n        {\n            if (Heightmap == null) throw new InvalidOperationException($\"{ nameof(Heightmap) } is a null\");\n            if (heightStickArray == null) throw new ArgumentNullException(nameof(heightStickArray));\n\n            var heightStickArrayLength = heightStickArray.Length - index;\n            if (heightStickArrayLength <= 0) throw new IndexOutOfRangeException(nameof(index));\n\n            var typeOfT = typeof(T);\n            T[] heights;\n\n            if (typeOfT == typeof(float))\n            {\n                if (Heightmap.Floats == null) throw new InvalidOperationException($\"{ nameof(Heightmap.Floats) } is a null.\");\n                heights = (T[])(object)Heightmap.Floats;\n            }\n            else if (typeOfT == typeof(short))\n            {\n                if (Heightmap.Shorts == null) throw new InvalidOperationException($\"{ nameof(Heightmap.Shorts) } is a null.\");\n                heights = (T[])(object)Heightmap.Shorts;\n            }\n            else if (typeOfT == typeof(byte))\n            {\n                if (Heightmap.Bytes == null) throw new InvalidOperationException($\"{ nameof(Heightmap.Bytes) } is a null.\");\n                heights = (T[])(object)Heightmap.Bytes;","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Physics/HeightStickArraySourceFromHeightmap.cs#L22-L58","documentation":"HeightStickArraySourceFromHeightmap.CopyTo requires the index to leave at least one writable element in the destination (heightStickArray.Length - index > 0). If index equals or exceeds the array length, it throws IndexOutOfRangeException naming the index parameter.","triggerScenarios":"Calling CopyTo with an index >= heightStickArray.Length, or an empty destination array with index 0.","commonSituations":"Copy offsets computed from wrong dimensions (e.g. multiplying index by wrong element size); copying multiple heightfields into a shared buffer with cumulative offsets that overrun; length-0 arrays.","solutions":["Ensure index < heightStickArray.Length before calling CopyTo.","Recompute the offset using the correct element count/dimensions.","Validate that the destination buffer is sized HeightStickSize.X * HeightStickSize.Y (plus offset)."],"exampleFix":"// before\nsource.CopyTo(buffer, buffer.Length); // throws\n// after\nint index = batch * source.HeightStickSize.X * source.HeightStickSize.Y;\nif (index < buffer.Length)\n    source.CopyTo(buffer, index);","handlingStrategy":"validation","validationCode":"if (index < 0 || index >= heightStickArray.Length)\n    throw new ArgumentOutOfRangeException(nameof(index));","typeGuard":null,"tryCatchPattern":"try { source.CopyTo(heightStickArray, index); }\ncatch (IndexOutOfRangeException ex) { /* clamp/recompute the index */ }","preventionTips":["Ensure index < array.Length before each CopyTo","Compute offsets from correct element counts","Never copy into zero-length buffers"],"tags":["physics","heightfield","index-out-of-range"],"backgroundTag":"argument-out-of-range","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}