{"record":{"id":"ddce2aaa6ad8126d","repo":"AvaloniaUI/Avalonia","slug":"invalid-index","errorCode":null,"errorMessage":"Invalid index.","messagePattern":"Invalid index\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Collections/AvaloniaList.cs","lineNumber":676,"sourceCode":"        {\n            if (array == null)\n            {\n                throw new ArgumentNullException(nameof(array));\n            }\n\n            if (array.Rank != 1)\n            {\n                throw new ArgumentException(\"Multi-dimensional arrays are not supported.\");\n            }\n\n            if (array.GetLowerBound(0) != 0)\n            {\n                throw new ArgumentException(\"Non-zero lower bounds are not supported.\");\n            }\n\n            if (index < 0)\n            {\n                throw new ArgumentException(\"Invalid index.\");\n            }\n\n            if (array.Length - index < Count)\n            {\n                throw new ArgumentException(\"The target array is too small.\");\n            }\n\n            if (array is T[] tArray)\n            {\n                _inner.CopyTo(tArray, index);\n            }\n            else\n            {\n                //\n                // Catch the obvious case assignment will fail.\n                // We can't find all possible problems by doing the check though.\n                // For example, if the element type of the Array is derived from T,\n                // we can't figure out if we can successfully copy the element beforehand.","sourceCodeStart":658,"sourceCodeEnd":694,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Collections/AvaloniaList.cs#L658-L694","documentation":"ArgumentException thrown by AvaloniaList.ICollection.CopyTo when the starting index is negative. The destination array index must be a valid non-negative offset where copying begins.","triggerScenarios":"Calling ((ICollection)list).CopyTo(array, -1) or passing a computed index that underflows to a negative value.","commonSituations":"An off-by-one or subtraction that yields a negative index; a default int parameter left at -1 as a sentinel and forwarded unchecked.","solutions":["Pass a non-negative index (typically 0) into the destination array.","Validate/clamp the index before calling CopyTo.","Review the computation producing the index for underflow bugs."],"exampleFix":"// before\n((ICollection)list).CopyTo(arr, offset - 1);\n\n// after\nvar idx = Math.Max(0, offset);\n((ICollection)list).CopyTo(arr, idx);","handlingStrategy":"validation","validationCode":"if (index < 0) throw new ArgumentOutOfRangeException(nameof(index), index, \"Index must be non-negative.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass a non-negative index (usually 0) to CopyTo.","Clamp or validate computed indices before copying.","Avoid sentinel values like -1 forwarded into CopyTo."],"tags":["avalonia","collections","list","copyto","index-validation"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}