{"record":{"id":"348288ed80459748","repo":"stride3d/stride","slug":"not-enough-space-in-array-from-arrayindex-to-end-of-array","errorCode":null,"errorMessage":"Not enough space in array from arrayIndex to end of array","messagePattern":"Not enough space in array from arrayIndex to end of array","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core/Collections/SortedList.cs","lineNumber":278,"sourceCode":"        this.table = new KeyValuePair<TKey, TValue>[defaultCapacity];\n        inUse = 0;\n        modificationCount++;\n    }\n\n    void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)\n    {\n        if (Count == 0)\n            return;\n\n        ArgumentNullException.ThrowIfNull(array);\n\n        if (arrayIndex < 0)\n            throw new ArgumentOutOfRangeException();\n\n        if (arrayIndex >= array.Length)\n            throw new ArgumentNullException(\"arrayIndex is greater than or equal to array.Length\");\n        if (Count > (array.Length - arrayIndex))\n            throw new ArgumentNullException(\"Not enough space in array from arrayIndex to end of array\");\n\n        var i = arrayIndex;\n        foreach (var pair in this)\n            array[i++] = pair;\n    }\n\n    void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> keyValuePair)\n    {\n        Add(keyValuePair.Key, keyValuePair.Value);\n    }\n\n    bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> keyValuePair)\n    {\n        var i = Find(keyValuePair.Key);\n\n        if (i >= 0)\n            return Comparer<KeyValuePair<TKey, TValue>>.Default.Compare(table[i], keyValuePair) == 0;\n        return false;","sourceCodeStart":260,"sourceCodeEnd":296,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core/Collections/SortedList.cs#L260-L296","documentation":"CopyTo throws this when the number of elements in the SortedList exceeds the space available in the destination array starting at arrayIndex (Count > array.Length - arrayIndex). The guard prevents partially overwriting the caller's array. The exception type used is ArgumentNullException (a mislabel), but the condition is an insufficient-capacity problem.","triggerScenarios":"CopyTo(array, arrayIndex) where array.Length - arrayIndex < sortedList.Count, e.g. a 5-element list copied into a 3-element array.","commonSituations":"Destination array reused across iterations as the list grew, off-by-one in offset computation, or snapshotting a collection whose Count changed since the array was allocated.","solutions":["Reallocate the destination array with length >= arrayIndex + sortedList.Count.","Call ToArray() on the SortedList instead of manual CopyTo when you just need a snapshot.","Recompute Count immediately before CopyTo if the collection may be modified concurrently."],"exampleFix":"// before\nvar buf = new KeyValuePair<string,int>[3];\nsl.CopyTo(buf, 0); // sl.Count == 5\n// after\nvar buf = new KeyValuePair<string,int>[sl.Count];\nsl.CopyTo(buf, 0);","handlingStrategy":"validation","validationCode":"int required = sortedList.Count + arrayIndex;\nif (array == null || array.Length < required)\n    array = new KeyValuePair<TKey,TValue>[required];\nsortedList.CopyTo(array, arrayIndex);","typeGuard":"static bool HasRoom(int arrayLength, int arrayIndex, int count) =>\n    arrayLength - arrayIndex >= count;","tryCatchPattern":"try { sortedList.CopyTo(array, arrayIndex); }\ncatch (ArgumentNullException) when (array.Length - arrayIndex < sortedList.Count)\n{ array = new KeyValuePair<TKey,TValue>[sortedList.Count + arrayIndex]; sortedList.CopyTo(array, arrayIndex); }","preventionTips":["Recompute Count immediately before copying if the collection may change.","Allocate buffers as new KeyValuePair<TKey,TValue>[list.Count].","Track offsets in batch-copy code and assert remaining space per batch."],"tags":["collections","argument-validation","insufficient-capacity"],"backgroundTag":"value-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"}