{"record":{"id":"858c33b3383dc21c","repo":"dotnet/reactive","slug":"specified-argument-was-out-of-the-range-of-valid-values-858c33","errorCode":null,"errorMessage":"Specified argument was out of the range of valid values. (Parameter 'arrayIndex')","messagePattern":"Specified argument was out of the range of valid values\\. \\(Parameter 'arrayIndex'\\)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Disposables/CompositeDisposable.cs","lineNumber":432,"sourceCode":"        }\n\n        /// <summary>\n        /// Copies the disposables contained in the <see cref=\"CompositeDisposable\"/> to an array, starting at a particular array index.\n        /// </summary>\n        /// <param name=\"array\">Array to copy the contained disposables to.</param>\n        /// <param name=\"arrayIndex\">Target index at which to copy the first disposable of the group.</param>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"array\"/> is <c>null</c>.</exception>\n        /// <exception cref=\"ArgumentOutOfRangeException\"><paramref name=\"arrayIndex\"/> is less than zero. -or - <paramref name=\"arrayIndex\"/> is larger than or equal to the array length.</exception>\n        public void CopyTo(IDisposable[] array, int arrayIndex)\n        {\n            if (array == null)\n            {\n                throw new ArgumentNullException(nameof(array));\n            }\n\n            if (arrayIndex < 0 || arrayIndex >= array.Length)\n            {\n                throw new ArgumentOutOfRangeException(nameof(arrayIndex));\n            }\n\n            lock (_gate)\n            {\n                // disposed composites are always empty\n                if (_disposed)\n                {\n                    return;\n                }\n\n                if (arrayIndex + _count > array.Length)\n                {\n                    // there is not enough space beyond arrayIndex \n                    // to accommodate all _count disposables in this composite\n                    throw new ArgumentOutOfRangeException(nameof(arrayIndex));\n                }\n                \n                var i = arrayIndex;","sourceCodeStart":414,"sourceCodeEnd":450,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Disposables/CompositeDisposable.cs#L414-L450","documentation":"CompositeDisposable.CopyTo throws ArgumentOutOfRangeException when arrayIndex is negative or greater than or equal to the array length. The starting index must point inside the destination array; this first bounds check happens before the lock and before accounting for the number of elements to copy.","triggerScenarios":"Calling composite.CopyTo(arr, -1) or composite.CopyTo(new IDisposable[3], 3); also when arrayIndex is computed from a variable that drifted (e.g. an offset from a previous partial copy).","commonSituations":"Manual paging/copy loops that advance an offset without revalidating it against the array length; off-by-one errors where arrayIndex == array.Length was assumed valid as an exclusive end.","solutions":["Validate the index first: if (arrayIndex < 0 || arrayIndex >= array.Length) throw/skip.","For an empty composite, use CopyTo(arr, 0) with a non-empty array or use LINQ ToArray().","Clamp arrayIndex to Math.Min(index, array.Length - 1) when the copy is optional."],"exampleFix":"// before\ncomposite.CopyTo(buffer, offset); // offset can equal buffer.Length\n// after\nif (offset >= 0 && offset < buffer.Length)\n{\n    composite.CopyTo(buffer, offset);\n}","handlingStrategy":"validation","validationCode":"if (arrayIndex < 0 || arrayIndex >= array.Length) throw new ArgumentException(\"arrayIndex outside destination array\");\ncomposite.CopyTo(array, arrayIndex);","typeGuard":"bool IsValidIndex<T>(T[] a, int i) => a != null && i >= 0 && i < a.Length;","tryCatchPattern":"try { composite.CopyTo(array, index); } catch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"arrayIndex\") { /* clamp or log */ }","preventionTips":["Validate offsets computed from loops against array bounds","Remember arrayIndex must be a valid in-range index, not an exclusive end","Use index 0 with an exactly-sized array when unsure"],"tags":["argument-out-of-range","copyto","csharp","rx"],"backgroundTag":"argument-out-of-range","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}