{"record":{"id":"203a6086fe2f46b0","repo":"MonoGame/MonoGame","slug":"startindex-must-be-at-least-zero-and-smaller-than-203a60","errorCode":null,"errorMessage":"startIndex must be at least zero and smaller than data.Length.","messagePattern":"startIndex must be at least zero and smaller than data\\.Length\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MonoGame.Framework/Graphics/Texture3D.cs","lineNumber":418,"sourceCode":"            var width = right - left;\n            var height = bottom - top;\n            var depth = back - front;\n\n            if (left < 0 || top < 0 || back < 0 || right > texWidth || bottom > texHeight || front > texDepth)\n                throw new ArgumentException(\"Area must remain inside texture bounds\");\n            // Disallow negative box size\n            if (left >= right || top >= bottom || front >= back)\n                throw new ArgumentException(\"Neither box size nor box position can be negative\");\n            if (level < 0 || level >= LevelCount)\n                throw new ArgumentException(\"level must be smaller than the number of levels in this texture.\");\n            if (data == null)\n                throw new ArgumentNullException(\"data\");\n            var tSize = ReflectionHelpers.FastSizeOf<T>();\n            var fSize = Format.GetSize();\n            if (tSize > fSize || fSize % tSize != 0)\n                throw new ArgumentException(\"Type T is of an invalid size for the format of this texture.\", \"T\");\n            if (startIndex < 0 || startIndex >= data.Length)\n                throw new ArgumentException(\"startIndex must be at least zero and smaller than data.Length.\", \"startIndex\");\n            if (data.Length < startIndex + elementCount)\n                throw new ArgumentException(\"The data array is too small.\");\n\n            var dataByteSize = width*height*depth*fSize;\n            if (elementCount * tSize != dataByteSize)\n                throw new ArgumentException(string.Format(\"elementCount is not the right size, \" +\n                                            \"elementCount * sizeof(T) is {0}, but data size is {1}.\",\n                                            elementCount * tSize, dataByteSize), \"elementCount\");\n        }\n\t}\n}\n\n","sourceCodeStart":400,"sourceCodeEnd":431,"githubUrl":"https://github.com/MonoGame/MonoGame/blob/1d71bbd0ff4071a03caa583aa3bc56b85924a819/MonoGame.Framework/Graphics/Texture3D.cs#L400-L431","documentation":"ArgumentException('startIndex') from Texture3D.ValidateParams when startIndex < 0 or startIndex >= data.Length. The read/write window must start inside the array before the volume copy proceeds.","triggerScenarios":"Calling a box-level SetData/GetData with startIndex < 0, startIndex >= data.Length, or with an empty array (data.Length == 0 fails every startIndex >= 0).","commonSituations":"Off-by-one offset into a shared volume buffer; negative index; zero-length array; offset not reset after resizing the buffer.","solutions":["Clamp or verify startIndex to [0, data.Length - 1] before the call.","Ensure data has at least one element when elementCount > 0.","Recompute the offset whenever the buffer is resized or sliced."],"exampleFix":"// before\ntexture.SetData(0, l, t, r, b, f, bk, data, offset, count); // offset may be out of range\n\n// after\nint start = Math.Clamp(offset, 0, data.Length - 1);\ntexture.SetData(0, l, t, r, b, f, bk, data, start, count);","handlingStrategy":"validation","validationCode":"if (data == null || data.Length == 0)\n    throw new InvalidOperationException(\"data must be non-empty\");\nif (startIndex < 0 || startIndex >= data.Length)\n    throw new ArgumentOutOfRangeException(nameof(startIndex));\ntexture.SetData(level, l, t, r, b, f, bk, data, startIndex, elementCount);","typeGuard":null,"tryCatchPattern":"try\n{\n    texture.SetData(level, l, t, r, b, f, bk, data, startIndex, elementCount);\n}\ncatch (ArgumentException ex) when (ex.ParamName == \"startIndex\")\n{\n    // clamp and retry\n}","preventionTips":["Bounds-check startIndex against data.Length before the call.","Never pass a zero-length array.","Reset the offset after resizing the buffer."],"tags":["texture3d","setdata","getdata","argument-validation","monogame"],"backgroundTag":null,"analyzedSha":"1d71bbd0ff4071a03caa583aa3bc56b85924a819","analyzedAt":"2026-08-13T17:10:33.625Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}