dotnet/wpf · error · ArgumentOutOfRangeException

Count of bytes to read cannot be negative.

Error message

Count of bytes to read cannot be negative.

What it means

ByteStream.Read validates that count (bytes to read) is non-negative and throws ArgumentOutOfRangeException(SR.ReadCountNegative) when count < 0. Zero is explicitly allowed and returns immediately (optimization path).

Solutions

  1. Validate count >= 0 before calling Read, clamping negative remainders to 0.
  2. Fix the arithmetic computing count (check start/end ordering).
  3. Use larger types (long) for intermediate size math to avoid overflow before the cast to int.

Example fix

// before
stream.Read(buf, 0, (int)(end - start)); // throws when end < start
// after
int count = (int)Math.Max(0, end - start);
stream.Read(buf, 0, count);
Defensive patterns

Strategy: validation

Validate before calling

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

Try / catch

try { stream.Read(buf, 0, count); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "count") { /* recompute count */ }

Prevention

When it happens

Trigger: Calling Read(buffer, offset, count) with a negative count, usually from buffer-size arithmetic like (int)(end - start) where end < start, or an int overflow/underflow.

Common situations: Chunked copy loops with miscomputed remaining bytes; interop code passing sizes as unsigned that wrap to negative ints.

Related errors


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

Appendix: source

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

            CheckDisposedStatus();

            if (!CanRead)
            {
                throw new NotSupportedException(SR.ReadNotSupported);
            }
            
            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

View on GitHub (pinned to 81131a70a4)