stride3d/stride · error · ArgumentOutOfRangeException

The length must be positive.

Error message

The length must be positive.

What it means

StringLookAheadBuffer.Skip throws ArgumentOutOfRangeException with the message 'The length must be positive.' when called with a negative length. Skip advances the read cursor by `length` characters; a negative value would move the cursor backwards, which the buffer contract forbids.

Solutions

  1. Clamp or guard the value before calling Skip: `if (n > 0) buffer.Skip(n);`
  2. Fix the upstream length computation so it cannot go negative (e.g. Math.Max(0, len)).
  3. If negative skip is intentional, use the buffer's rewind/backup API instead of Skip.

Example fix

// before
buffer.Skip(indent - currentIndent); // may be negative
// after
var delta = indent - currentIndent;
if (delta > 0) buffer.Skip(delta);
Defensive patterns

Strategy: validation

Validate before calling

if (length < 0)
    throw new ArgumentException("Skip length must be >= 0", nameof(length));
buffer.Skip(length);

Type guard

static bool CanSkip(int length) => length >= 0;

Try / catch

try { buffer.Skip(length); }
catch (ArgumentOutOfRangeException ex) { throw new YamlScannerException("Negative skip length computed while scanning", ex); }

Prevention

When it happens

Trigger: Calling buffer.Skip(n) with n < 0 during YAML scanning — usually a scanner bug where a computed step (e.g. from an indent or length calculation) went negative.

Common situations: Custom or patched YAML scanner logic, miscalculated offsets when handling block scalars or indentation, or feeding malformed input through a modified parser.

Related errors


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

Appendix: source

Thrown at sources/core/Stride.Core.Yaml/StringLookAheadBuffer.cs:81

        public bool EndOfInput { get { return IsOutside(currentIndex); } }

        public StringLookAheadBuffer(string value)
        {
            this.value = value;
        }

        public char Peek(int offset)
        {
            int index = currentIndex + offset;
            return IsOutside(index) ? '\0' : value[index];
        }

        public void Skip(int length)
        {
            if (length < 0)
            {
                throw new ArgumentOutOfRangeException("length", "The length must be positive.");
            }
            currentIndex += length;
        }
    }
}

View on GitHub (pinned to 96fad776d2)