dotnet/wpf · error · ArgumentOutOfRangeException

Buffer offset cannot be negative.

Error message

Buffer offset cannot be negative.

What it means

ByteStream.Read validates that the buffer offset is non-negative, throwing ArgumentOutOfRangeException(SR.BufferOffsetNegative) when offset < 0. This mirrors the standard Stream contract but is checked explicitly before the IStream call.

Solutions

  1. Validate offset >= 0 before calling Read.
  2. Fix the position/cursor arithmetic producing the negative offset.
  3. Replace sentinel -1 offsets with explicit flags.

Example fix

// before
stream.Read(buf, pos, len); // pos may underflow to negative
// after
if (pos < 0) throw new InvalidOperationException("Parser position underflow");
stream.Read(buf, pos, len);
Defensive patterns

Strategy: validation

Validate before calling

if (offset < 0) throw new ArgumentOutOfRangeException(nameof(offset));

Try / catch

try { stream.Read(buf, offset, count); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "offset") { /* fix cursor arithmetic */ }

Prevention

When it happens

Trigger: Calling Read(buffer, offset, count) with a negative offset, typically from cursor arithmetic like offset += consumed where consumed was negative, or passing -1 as a sentinel.

Common situations: Manual buffer management in parsers with off-by-one or underflowing positions; sentinel values leaking into real calls.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/f8854e028692f17f. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/ByteStream.cs:271

            int read = 0;

            // optimization: if we are being asked to read zero bytes, be done.
            if (count == 0)
            {
                return read;
            }
            
            // count has to be positive number
            if (0 > count)
            {
                throw new ArgumentOutOfRangeException(nameof(count),
                                                      SR.ReadCountNegative);
            }

            // offset has to be a positive number
            if (0 > offset)
            {
                throw new ArgumentOutOfRangeException(nameof(offset),
                                                      SR.BufferOffsetNegative);
            }

            // make sure that we have a buffer that matches number of bytes we need to read 
            // since all values are > 0, there is no chance of overflow
            if (!((buffer.Length > 0) && ((buffer.Length - offset) >= count)))
            {
                throw new ArgumentException(SR.BufferTooSmall, nameof(buffer));
            }
            
            // offset == 0 is the normal case
            if (0 == offset)
            {
                _securitySuppressedIStream.Read(buffer, count, out read);
            }
            // offset involved.  Must be positive
            else if (0 < offset)
            {

View on GitHub (pinned to 81131a70a4)