{"record":{"id":"df426627e66beaba","repo":"dotnetcore/CAP","slug":"arrayindex","errorCode":null,"errorMessage":"arrayIndex","messagePattern":"arrayIndex","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/DotNetCore.CAP.Dashboard/CircularBuffer.cs","lineNumber":164,"sourceCode":"\n    public void Clear()\n    {\n        _firstIndex = 0;\n        Count = 0;\n    }\n\n    public bool Contains(T item)\n    {\n        throw new NotImplementedException();\n    }\n\n    public void CopyTo(T[] array, int arrayIndex)\n    {\n        if (array == null) throw new ArgumentNullException(nameof(array));\n\n        if (arrayIndex < 0) throw new ArgumentOutOfRangeException(nameof(arrayIndex));\n\n        if (Count > array.Length - arrayIndex) throw new ArgumentException(\"arrayIndex\");\n\n        // Iterate through the buffer in correct order.\n        foreach (var item in this)\n        {\n            array[arrayIndex++] = item;\n        }\n    }\n\n    public bool Remove(T item)\n    {\n        throw new NotImplementedException();\n    }\n\n    #endregion\n}","sourceCodeStart":146,"sourceCodeEnd":179,"githubUrl":"https://github.com/dotnetcore/CAP/blob/e52b8508e54cdb7a9ce7f9fec03d9ea8ad2710fb/src/DotNetCore.CAP.Dashboard/CircularBuffer.cs#L146-L179","documentation":"CircularBuffer<T>.CopyTo throws ArgumentException(\"arrayIndex\") when Count > array.Length - arrayIndex, i.e. the destination array is too small to hold all buffered items starting at the given offset. This is the capacity guard for the copy loop array[arrayIndex++] = item.","triggerScenarios":"Calling CopyTo with an array whose available space (Length - arrayIndex) is less than the buffer's Count, e.g. copying into new T[n] with n < Count or using a nonzero offset in an array sized exactly to Count.","commonSituations":"Reusing a stale array allocated for an older smaller Count while the ring buffer wrapped and grew, or applying a non-zero arrayIndex without enlarging the array.","solutions":["Allocate the destination with room for the offset: new T[buffer.Count + arrayIndex].","Use array.Length - arrayIndex check at the call site before copying, or call ToArray()/ToArray(segment) style helpers.","Re-derive Count from the buffer at copy time rather than caching a stale size."],"exampleFix":"// before\nvar dest = new T[buffer.Count];\nbuffer.CopyTo(dest, 5); // needs Count+5 slots\n// after\nvar dest = new T[buffer.Count + 5];\nbuffer.CopyTo(dest, 5);","handlingStrategy":"validation","validationCode":"if (array.Length - offset < buffer.Count)\n    array = new T[buffer.Count + offset];\nbuffer.CopyTo(array, offset);","typeGuard":null,"tryCatchPattern":"try { buffer.CopyTo(array, offset); }\ncatch (ArgumentException) { array = new T[buffer.Count + offset]; buffer.CopyTo(array, offset); }","preventionTips":["Size destinations as Count + arrayIndex, not just Count","Re-read Count at copy time instead of caching it","Prefer allocating a fresh array over reusing stale buffers"],"tags":["argument-exception","capacity","collection","dotnet"],"backgroundTag":"invalid-argument-value","analyzedSha":"e52b8508e54cdb7a9ce7f9fec03d9ea8ad2710fb","analyzedAt":"2026-09-14T14:46:34.677Z","contentChangedAt":"2026-09-14T14:46:34.677Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}