dotnet/machinelearning · error · ArgumentOutOfRangeException

nameof(index)

Error message

nameof(index)

What it means

SetValidityBit validates that the bit index lies within [0, Length] before writing into the null (validity) bitmap buffers. An index beyond Length (or negative) would map to a bitmap buffer/offset that does not exist, so it throws ArgumentOutOfRangeException with paramName `index`. It is internal and normally reached via Append, which grows the container as needed.

Source

Thrown at src/Microsoft.Data.Analysis/PrimitiveColumnContainer.cs:311

        {
            int bitMapBufferIndex = (int)((uint)index / 8);
            Debug.Assert(bitMapBufferSpan.Length >= bitMapBufferIndex);
            byte curBitMap = bitMapBufferSpan[bitMapBufferIndex];
            byte newBitMap = SetBit(curBitMap, index, value);
            bitMapBufferSpan[bitMapBufferIndex] = newBitMap;
        }

        /// <summary>
        /// A null value has an unset bit
        /// A NON-null value has a set bit
        /// </summary>
        /// <param name="index"></param>
        /// <param name="value"></param>
        internal void SetValidityBit(long index, bool value)
        {
            if ((ulong)index > (ulong)Length)
            {
                throw new ArgumentOutOfRangeException(nameof(index));
            }
            // First find the right bitMapBuffer
            int bitMapIndex = (int)(index / ReadOnlyDataFrameBuffer<T>.MaxCapacity);
            Debug.Assert(NullBitMapBuffers.Count > bitMapIndex);
            DataFrameBuffer<byte> bitMapBuffer = (DataFrameBuffer<byte>)NullBitMapBuffers[bitMapIndex];

            // Set the bit
            index -= bitMapIndex * ReadOnlyDataFrameBuffer<T>.MaxCapacity;
            int bitMapBufferIndex = (int)((uint)index / 8);
            Debug.Assert(bitMapBuffer.Length >= bitMapBufferIndex);
            if (bitMapBuffer.Length == bitMapBufferIndex)
                bitMapBuffer.Append(0);
            SetValidityBit(bitMapBuffer.Span, (int)index, value);
        }

        private bool GetValidityBit(long index)
        {
            if ((uint)index >= Length)

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Ensure the container Length covers the target index first: call Resize(index) or use Append, which grows buffers automatically.
  2. Verify loops append at most Length values and that indices are long, non-negative, and zero-based.
  3. If constructing from a raw bitmap, pass the true final row count as `length` so subsequent appends start at the right index.

Example fix

// before
container.SetValidityBit(container.Length + 5, true); // out of range
// after
container.Resize(container.Length + 6);
container.SetValidityBit(container.Length - 1, true);
Defensive patterns

Strategy: validation

Validate before calling

if (index < 0 || index > container.Length) throw new InvalidOperationException("index out of range for SetValidityBit");

Type guard

static bool CanSetValidityBit<T>(PrimitiveColumnContainer<T> c, long index) => (ulong)index <= (ulong)c.Length;

Try / catch

try { container.SetValidityBit(index, value); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "index")
{
    container.Resize(index + 1);
    container.SetValidityBit(index, value);
}

Prevention

When it happens

Trigger: Calling Append/AppendMany with an index-derived path where the container's Length bookkeeping is behind (e.g. appending into a container constructed with an explicit smaller length), or internal/direct calls to SetValidityBit(index, value) with index > Length or index < 0.

Common situations: Manually extending a container after constructing it from a prebuilt bitmap with a length smaller than the data being appended, off-by-one loops that call append one time too many, or custom column subclasses that bypass AppendMany's growth.

Related errors


AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11). Data as JSON: /api/errors/e580ab0eac83fc63. Report an issue: GitHub.