stride3d/stride · error · ArgumentOutOfRangeException

Invalid offset

Error message

Invalid offset {offset}

What it means

Thrown by CheckRangeArguments when the offset of a range operation is negative. The deque requires offset >= 0 for CopyTo and RemoveRange; a negative offset cannot address any part of the source. Raised as ArgumentOutOfRangeException naming 'offset'.

Solutions

  1. Validate offset >= 0 before the call, throwing or clamping as appropriate
  2. Re-check the arithmetic that produces the offset (subtraction may underflow)
  3. If clamping, decide explicitly whether a negative offset should mean 0 or an error rather than letting it propagate

Example fix

// before
deque.RemoveRange(start - count, count); // start < count => negative offset
// after
int offset = Math.Max(0, start - count);
deque.RemoveRange(offset, count);
Defensive patterns

Strategy: validation

Validate before calling

if (offset < 0) throw new ArgumentOutOfRangeException(nameof(offset));
deque.RemoveRange(offset, count);

Prevention

When it happens

Trigger: Calling range-taking members (CopyTo / RemoveRange overloads that go through CheckRangeArguments) with a negative offset, e.g. RemoveRange(-1, 5).

Common situations: Computing offset as (startIndex - count) which underflows below zero; integer arithmetic on sizes that went negative; passing an unchecked user-supplied offset.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/14aa17e24fd1db6c. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core/Collections/Dequeue.cs:421

        if (index < 0 || index >= sourceLength)
        {
            throw new ArgumentOutOfRangeException(nameof(index), "Invalid existing index " + index + " for source length " + sourceLength);
        }
    }

    /// <summary>
    /// Checks the <paramref name="offset"/> and <paramref name="count"/> arguments for validity when applied to a source of a given length. Allows 0-element ranges, including a 0-element range at the end of the source.
    /// </summary>
    /// <param name="sourceLength">The length of the source. This parameter is not checked for validity.</param>
    /// <param name="offset">The index into source at which the range begins.</param>
    /// <param name="count">The number of elements in the range.</param>
    /// <exception cref="ArgumentOutOfRangeException">Either <paramref name="offset"/> or <paramref name="count"/> is less than 0.</exception>
    /// <exception cref="ArgumentException">The range [offset, offset + count) is not within the range [0, sourceLength).</exception>
    private static void CheckRangeArguments(int sourceLength, int offset, int count)
    {
        if (offset < 0)
        {
            throw new ArgumentOutOfRangeException(nameof(offset), "Invalid offset " + offset);
        }

        if (count < 0)
        {
            throw new ArgumentOutOfRangeException(nameof(count), "Invalid count " + count);
        }

        if (sourceLength - offset < count)
        {
            throw new ArgumentException("Invalid offset (" + offset + ") or count + (" + count + ") for source length " + sourceLength);
        }
    }

    #endregion

    /// <summary>
    /// Gets a value indicating whether this instance is empty.
    /// </summary>

View on GitHub (pinned to 96fad776d2)