stride3d/stride · error · ArgumentException

Invalid offset ( ) or count + ( ) for source length

Error message

Invalid offset ({offset}) or count + ({count}) for source length {sourceLength}

What it means

Thrown by CheckRangeArguments when the requested range [offset, offset+count) extends past the source: sourceLength - offset < count. This is an ArgumentException (not ArgumentOutOfRange) because neither parameter alone is wrong — the combination is invalid for the given source length.

Solutions

  1. Clamp count to Math.Min(count, deque.Count - offset) before calling
  2. Check deque.Count immediately before the range operation
  3. Ensure offset and count both derive from the same deque's current Count

Example fix

// before
deque.RemoveRange(offset, sliceLength); // sliceLength from another collection
// after
count = Math.Min(sliceLength, deque.Count - offset);
deque.RemoveRange(offset, count);
Defensive patterns

Strategy: validation

Validate before calling

count = Math.Min(count, deque.Count - offset);
if (count < 0) count = 0;
deque.RemoveRange(offset, count);

Prevention

When it happens

Trigger: RemoveRange(offset, count) or a CopyTo path where offset + count > Count, e.g. RemoveRange(3, 10) on a 5-element deque.

Common situations: Using indices/lengths from a different (larger) collection; not refreshing Count after prior mutations; passing the source's capacity instead of its Count as the available length.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

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

    /// <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>
    private bool IsEmpty => Count == 0;

    /// <summary>
    /// Gets a value indicating whether this instance is at full capacity.
    /// </summary>
    private bool IsFull => Count == Capacity;

    /// <summary>
    /// Gets a value indicating whether the buffer is "split" (meaning the beginning of the view is at a later index in <see cref="buffer"/> than the end).
    /// </summary>

View on GitHub (pinned to 96fad776d2)