{"record":{"id":"326442829a5723b1","repo":"microsoft/FASTER","slug":"elementindex","errorCode":null,"errorMessage":"elementIndex","messagePattern":"elementIndex","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"cs/src/core/VarLen/UnmanagedMemoryManager.cs","lineNumber":68,"sourceCode":"        /// <param name=\"length\"></param>\n        public void SetDestination(T* pointer, int length)\n        {\n            _pointer = pointer;\n            _length = length;\n        }\n\n        /// <summary>\n        /// Obtains a span that represents the region\n        /// </summary>\n        public override Span<T> GetSpan() => new Span<T>(_pointer, _length);\n\n        /// <summary>\n        /// Provides access to a pointer that represents the data (note: no actual pin occurs)\n        /// </summary>\n        public override MemoryHandle Pin(int elementIndex = 0)\n        {\n            if (elementIndex < 0 || elementIndex >= _length)\n                throw new ArgumentOutOfRangeException(nameof(elementIndex));\n            return new MemoryHandle(_pointer + elementIndex);\n        }\n        /// <summary>\n        /// Has no effect\n        /// </summary>\n        public override void Unpin() { }\n\n        /// <summary>\n        /// Releases all resources associated with this object\n        /// </summary>\n        protected override void Dispose(bool disposing) { }\n    }\n}\n","sourceCodeStart":50,"sourceCodeEnd":82,"githubUrl":"https://github.com/microsoft/FASTER/blob/321d872eabda6a0345c8bd76419f89723ed864ae/cs/src/core/VarLen/UnmanagedMemoryManager.cs#L50-L82","documentation":"ArgumentOutOfRangeException with the message \"elementIndex\" thrown by UnmanagedMemoryManager<T>.Pin when the requested elementIndex is negative or >= _length. Pin creates a MemoryHandle over raw unmanaged memory and validates the offset against the manager's length before computing the pointer.","triggerScenarios":"Calling Pin(i) on the IMemoryOwner/Memory<T> obtained from UnmanagedMemoryManager with i < 0 or i >= the element count passed at construction (e.g. slicing the Memory incorrectly or passing a length in bytes instead of element count).","commonSituations":"Confusing byte length with element count (T may be larger than 1 byte); slicing a Memory<T> and pinning with an absolute offset instead of slice-relative; off-by-one loops over elements.","solutions":["Pin with an index within [0, length) as given at construction; use sliced Memory to pin subsections instead of absolute indices","Pass element count, not byte size, when constructing UnmanagedMemoryManager","Validate the index before calling Pin in perf-sensitive code paths"],"exampleFix":"// before\nmemory.Pin(byteOffset); // may exceed element count\n// after\nint elementIndex = (int)(byteOffset / sizeof(MyType));\nif (elementIndex < 0 || elementIndex >= length) throw new ArgumentException(...);\nmemory.Pin(elementIndex);","handlingStrategy":"validation","validationCode":"if (elementIndex >= 0 && elementIndex < length) manager.Memory.Pin(elementIndex); else throw new ArgumentOutOfRangeException(nameof(elementIndex));","typeGuard":"bool CanPin(UnmanagedMemoryManager<T> m, int i) => i >= 0 && i < m.Length;","tryCatchPattern":"try { return memory.Pin(i); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"elementIndex\")\n{ throw new ArgumentException($\"Index {i} outside manager length {length}\", ex); }","preventionTips":["Convert byte offsets to element indices before pinning","Slice Memory<T> instead of pinning with absolute offsets","Keep construction length (element count) and pin index units consistent"],"tags":["memory","argument-out-of-range","unmanaged","csharp"],"backgroundTag":"argument-out-of-range","analyzedSha":"321d872eabda6a0345c8bd76419f89723ed864ae","analyzedAt":"2026-09-15T22:18:00.693Z","contentChangedAt":"2026-09-15T22:18:00.693Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}