{"record":{"id":"dbf8459819e0a6b6","repo":"spectreconsole/spectre.console","slug":"array-does-not-contain-enough-space-for-items","errorCode":null,"errorMessage":"Array does not contain enough space for items","messagePattern":"Array does not contain enough space for items","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Spectre.Console/Internal/CircularBuffer.cs","lineNumber":260,"sourceCode":"                _start = _end;\n            }\n        }\n    }\n\n    public void Clear()\n    {\n        _start = 0;\n        _end = 0;\n        _buffer.Clear();\n    }\n\n    public bool Contains(T item) => IndexOf(item) != -1;\n\n    public void CopyTo(T[] array, int arrayIndex)\n    {\n        if (array.Length - arrayIndex < Count)\n        {\n            throw new ArgumentException(\"Array does not contain enough space for items\");\n        }\n\n        for (var index = 0; index < Count; ++index)\n        {\n            array[index + arrayIndex] = this[index];\n        }\n    }\n\n    public T[] ToArray()\n    {\n        if (IsEmpty)\n        {\n            return Array.Empty<T>();\n        }\n\n        var array = new T[Count];\n        for (var index = 0; index < Count; ++index)\n        {","sourceCodeStart":242,"sourceCodeEnd":278,"githubUrl":"https://github.com/spectreconsole/spectre.console/blob/0acc92fada6c42f13984e79c2b5f3d993bdfb099/src/Spectre.Console/Internal/CircularBuffer.cs#L242-L278","documentation":"Thrown by CircularBuffer<T>.CopyTo(T[] array, int arrayIndex) when the destination array does not have enough space starting at arrayIndex to hold all elements currently in the buffer. CircularBuffer is Spectre.Console's internal ring-buffer collection used for progress samples and other bounded history tracking. The check is array.Length - arrayIndex < Count.","triggerScenarios":"Calling CopyTo with a destination array smaller than the buffer's current element count, or with an arrayIndex that leaves insufficient remaining slots. For example: buffer.CopyTo(new T[5], 3) when buffer.Count is 4 (only 2 slots available from index 3).","commonSituations":"Pre-allocating a destination array with the wrong size (e.g., using a default capacity instead of buffer.Count); passing an arrayIndex that was computed incorrectly; resizing logic that shrinks the array after the buffer grew. This is typically hit in LINQ or collection-copy scenarios, or when code manually implements ICollection<T>.CopyTo.","solutions":["Allocate the destination array with size >= buffer.Count: var array = new T[buffer.Count]; buffer.CopyTo(array, 0);","Correct the arrayIndex so that array.Length - arrayIndex >= buffer.Count.","Use buffer.ToArray() instead of CopyTo when you just need a snapshot — it allocates the correct size internally.","If implementing CopyTo yourself, validate arrayIndex >= 0 and the remaining capacity before delegating."],"exampleFix":"// before\nvar dest = new T[10];\nbuffer.CopyTo(dest, 8); // fails if buffer.Count > 2\n\n// after\nvar dest = new T[buffer.Count + startIndex];\nbuffer.CopyTo(dest, startIndex);","handlingStrategy":"validation","validationCode":"// Validate array capacity before CopyTo\nif (array.Length - arrayIndex < buffer.Count)\n{\n    throw new ArgumentException(\"Destination array too small.\");\n}\nbuffer.CopyTo(array, arrayIndex);\n\n// Or simply use ToArray() which handles sizing:\nvar snapshot = buffer.ToArray();","typeGuard":null,"tryCatchPattern":"try\n{\n    buffer.CopyTo(array, arrayIndex);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"enough space\"))\n{\n    // Re-allocate with correct size and retry\n    array = new T[buffer.Count];\n    buffer.CopyTo(array, 0);\n}","preventionTips":["Always size the destination array to at least buffer.Count.","Prefer buffer.ToArray() over manual CopyTo when you need a fresh snapshot.","Double-check arrayIndex arithmetic: ensure array.Length - arrayIndex >= buffer.Count."],"tags":["argumentexception","circularbuffer","collections","array-bounds"],"backgroundTag":null,"analyzedSha":"0acc92fada6c42f13984e79c2b5f3d993bdfb099","analyzedAt":"2026-08-13T18:27:21.983Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}