dotnet/wpf · error · ArgumentOutOfRangeException

SR.ReadCountNegative

Error message

SR.ReadCountNegative

What it means

This is a standard argument guard in PackagingUtilities.VerifyStreamReadArgs, a shared validation helper for Stream.Read implementations: it throws ArgumentOutOfRangeException when the count parameter is negative. It validates the buffer, offset and count supplied by a stream caller before the read is attempted.

Solutions

  1. Validate count >= 0 before calling Read; clamp to Math.Max(0, remaining).
  2. Fix the remaining-bytes calculation that produced the negative count.
  3. Use checked arithmetic to catch overflow earlier and handle it explicitly.

Example fix

// before
int count = total - read; // can go negative
stream.Read(buffer, offset, count);
// after
int count = Math.Max(0, total - read);
if (count > 0) stream.Read(buffer, offset, count);
Defensive patterns

Strategy: validation

Validate before calling

int count = Math.Max(0, Math.Min(total - read, buffer.Length - offset));
if (count > 0) stream.Read(buffer, offset, count);

Type guard

bool ValidCount(int count) => count >= 0;

Try / catch

try { stream.Read(buffer, offset, count); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "count") { /* clamp count */ }

Prevention

When it happens

Trigger: Passing a negative count parameter to a packaging Stream.Read call, e.g. from a length computation that produced a negative value.

Common situations: Subtracting a larger value from a length (remaining-bytes math gone wrong); uninitialized size variables; arithmetic on long-to-int casts that overflow negative.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/IO/Packaging/PackagingUtilities.cs:116

        /// <param name="buffer">buffer</param>
        /// <param name="offset">offset</param>
        /// <param name="count">count</param>
        /// <remarks>Common argument verification for Stream.Read()</remarks>
        internal static void VerifyStreamReadArgs(Stream s, byte[] buffer, int offset, int count)
        {
            if (!s.CanRead)
                throw new NotSupportedException(SR.ReadNotSupported);

            ArgumentNullException.ThrowIfNull(buffer);

            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(offset), SR.OffsetNegative);
            }

            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(count), SR.ReadCountNegative);
            }

            checked     // catch any integer overflows
            {
                if (offset + count > buffer.Length)
                {
                    throw new ArgumentException(SR.ReadBufferTooSmall, nameof(buffer));
                }
            }
        }

        /// <summary>
        /// VerifyStreamWriteArgs
        /// </summary>
        /// <param name="s"></param>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>

View on GitHub (pinned to 81131a70a4)