dotnet/wpf · error · ArgumentException

SR.ReadBufferTooSmall

Error message

SR.ReadBufferTooSmall

What it means

VerifyStreamReadArgs in PackagingUtilities validates arguments for Stream.Read calls in WPF packaging code. When offset + count exceeds buffer.Length, the read would overflow the buffer, so an ArgumentException is thrown. The checked block also guards against integer overflow when computing offset + count.

Solutions

  1. Ensure buffer.Length >= offset + count before calling Read, or allocate a larger buffer.
  2. If reading from a current position, pass offset=0 and use the full buffer length as count.
  3. Round-trip: compute the safe count as buffer.Length - offset and clamp the requested count to it.

Example fix

// before
stream.Read(buffer, offset, count);
// after
count = Math.Min(count, buffer.Length - offset);
if (count < 0) throw new ArgumentOutOfRangeException(nameof(count));
stream.Read(buffer, offset, count);
Defensive patterns

Strategy: validation

Validate before calling

if (buffer == null) throw new ArgumentNullException(nameof(buffer));
if (offset < 0 || count < 0 || offset + count > buffer.Length)
    throw new ArgumentOutOfRangeException(nameof(count), "offset+count exceeds buffer length");

Type guard

static bool CanReadInto(byte[] buffer, int offset, int count) =>
    buffer != null && offset >= 0 && count >= 0 && (long)offset + count <= buffer.Length;

Try / catch

try { stream.Read(buffer, offset, count); }
catch (ArgumentException ex) when (ex.ParamName == "buffer") { /* fix bounds or reallocate buffer */ }

Prevention

When it happens

Trigger: Calling a packaging Stream.Read (e.g. on a ZipPackage part stream) with a buffer that is smaller than offset + count, e.g. buffer.Length=10 with offset=5, count=10.

Common situations: Hand-rolled stream copy loops where the offset was set from a previous partial read but the buffer was not grown; copying code that assumes offset is relative to a sub-range of the buffer.

Related errors


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

Appendix: source

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

                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>
        /// <remarks>common argument verification for Stream.Write</remarks>
        internal static void VerifyStreamWriteArgs(Stream s, byte[] buffer, int offset, int count)
        {
            if (!s.CanWrite)
                throw new NotSupportedException(SR.WriteNotSupported);

            ArgumentNullException.ThrowIfNull(buffer);

View on GitHub (pinned to 81131a70a4)