dotnet/machinelearning · error · ArgumentOutOfRangeException

index

Error message

index

What it means

GetValidityBit, called from IsValid(index), throws ArgumentOutOfRangeException(nameof(index)) when index is negative or greater than Length. The validity (null) bitmap can only be queried for existing row positions. The bound check uses (ulong)index > (ulong)Length, so both negative and too-large indices are rejected.

Source

Thrown at src/Microsoft.Data.Analysis/DataFrameColumns/ArrowStringDataFrameColumn.cs:79

            _offsetsBuffers.Add(offsetBuffer);
            _nullBitMapBuffers.Add(nullBitMapBuffer);

            _nullCount = nullCount;
        }

        private long _nullCount;

        /// <inheritdoc/>
        public override long NullCount => _nullCount;

        /// <inheritdoc/>
        public override bool IsValid(long index) => NullCount == 0 || GetValidityBit(index);

        private bool GetValidityBit(long index)
        {
            if ((ulong)index > (ulong)Length)
            {
                throw new ArgumentOutOfRangeException(nameof(index));
            }
            // First find the right bitMapBuffer
            int bitMapIndex = GetBufferIndexContainingRowIndex(index, out int indexInBuffer);
            Debug.Assert(_nullBitMapBuffers.Count > bitMapIndex);
            ReadOnlyDataFrameBuffer<byte> bitMapBuffer = _nullBitMapBuffers[bitMapIndex];
            int bitMapBufferIndex = (int)((uint)index / 8);
            Debug.Assert(bitMapBuffer.Length > bitMapBufferIndex);
            byte curBitMap = bitMapBuffer[bitMapBufferIndex];
            return ((curBitMap >> (indexInBuffer & 7)) & 1) != 0;
        }

        private void SetValidityBit(long index, bool value)
        {
            if ((ulong)index > (ulong)Length)
            {
                throw new ArgumentOutOfRangeException(nameof(index));
            }
            // First find the right bitMapBuffer

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Clamp/verify the index: only call IsValid for 0 <= index < Length (or <= Length as the implementation permits).
  2. Fix loop bounds to use '< Length' instead of '<= Length'.
  3. Use the same column instance to derive iteration bounds rather than another column's length.
  4. Check NullCount first — IsValid short-circuits to true when NullCount == 0, so guard with if (col.NullCount == 0 || (uint)index < (ulong)col.Length).

Example fix

// before
for (long i = 0; i <= column.Length; i++) Use(column.IsValid(i)); // throws at i == Length boundary misuse

// after
for (long i = 0; i < column.Length; i++) Use(column.IsValid(i));
Defensive patterns

Strategy: validation

Validate before calling

bool canCheck = index >= 0 && index <= column.Length;
if (canCheck) bool valid = column.IsValid(index);

Type guard

static bool InRange(ArrowStringDataFrameColumn c, long i) => (ulong)i <= (ulong)c.Length;

Try / catch

try { ok = column.IsValid(i); }
catch (ArgumentOutOfRangeException) { ok = false; /* treat as out of bounds */ }

Prevention

When it happens

Trigger: Calling column.IsValid(index) (directly or via null-check logic) with index < 0 or index > Length — e.g. iterating to Length inclusive, using a row count from a different column, or checking validity before the column is fully appended.

Common situations: Off-by-one loops (for i = 0; i <= col.Length; i++); cross-referencing rows of two columns with different lengths; computed indices from a stale snapshot after the column was truncated.

Related errors


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