{"record":{"id":"017959bd606f71d4","repo":"dotnet/machinelearning","slug":"specified-argument-was-out-of-the-range-of-valid-v","errorCode":null,"errorMessage":"Specified argument was out of the range of valid values. (Parameter 'index')","messagePattern":"Specified argument was out of the range of valid values\\. \\(Parameter 'index'\\)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.Data.Analysis/DataFrameBuffer.cs","lineNumber":97,"sourceCode":"\n            if (newLength > Capacity)\n            {\n                //Double buffer size, but not higher than MaxByteCapacity\n                var doubledSize = (int)Math.Min((long)ReadOnlyBuffer.Length * 2, ArrayUtility.ArrayMaxSize);\n                var newCapacity = Math.Max(newLength * Size, doubledSize);\n\n                var memory = new Memory<byte>(new byte[newCapacity]);\n                _memory.CopyTo(memory);\n                _memory = memory;\n            }\n        }\n\n        internal override T this[int index]\n        {\n            set\n            {\n                if (index >= Length)\n                    throw new ArgumentOutOfRangeException(nameof(index));\n\n                RawSpan[index] = value;\n            }\n        }\n\n        internal static DataFrameBuffer<T> GetMutableBuffer(ReadOnlyDataFrameBuffer<T> buffer)\n        {\n            DataFrameBuffer<T> mutableBuffer = buffer as DataFrameBuffer<T>;\n            if (mutableBuffer == null)\n            {\n                mutableBuffer = new DataFrameBuffer<T>(buffer.ReadOnlyBuffer, buffer.Length);\n            }\n            return mutableBuffer;\n        }\n    }\n}\n","sourceCodeStart":79,"sourceCodeEnd":114,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.Data.Analysis/DataFrameBuffer.cs#L79-L114","documentation":"The internal indexer setter validates that index is within the currently written Length of the buffer and throws ArgumentOutOfRangeException otherwise. You may only overwrite values already appended; setting beyond Length is undefined for the buffer layout.","triggerScenarios":"Setting DataFrameBuffer<T>[index] with index >= Length (src/Microsoft.Data.Analysis/DataFrameBuffer.cs:97), e.g. writing at Capacity indices or a stale index after the buffer was reset/shrunk.","commonSituations":"Pre-allocating a large buffer and trying to fill by index before appending; off-by-one loops using Capacity instead of Length; concurrent writers using divergent index counters.","solutions":["Append values instead of writing by index until Length covers the target","Ensure index < Length before assignment (or extend via Append/EnsureCapacity first)","Fix loop bounds to iterate up to Length, not Capacity"],"exampleFix":"// before\nfor (int i = 0; i < buffer.Capacity; i++) buffer[i] = data[i];\n// after\nfor (int i = 0; i < buffer.Length; i++) buffer[i] = data[i];","handlingStrategy":"type-guard","validationCode":"bool CanWriteAt(DataFrameBuffer<T> b, int i) => i >= 0 && i < b.Length;","typeGuard":"bool IsValidIndex<T>(DataFrameBuffer<T> buffer, int index) => index >= 0 && index < buffer.Length;","tryCatchPattern":"try { buffer[index] = value; }\ncatch (ArgumentOutOfRangeException) { /* ensure Length covers index via Append/EnsureCapacity first */ }","preventionTips":["Only index into positions already appended (index < Length)","Iterate to Length, not Capacity","Append to grow instead of assigning beyond Length"],"tags":["dotnet","buffer","index","argument-out-of-range"],"backgroundTag":"argument-out-of-range","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}