dotnet/wpf · error · ArgumentException

SR.WriteBufferTooSmall

Error message

SR.WriteBufferTooSmall

What it means

VerifyStreamWriteArgs throws ArgumentException(SR.WriteBufferTooSmall) when offset + count exceeds buffer.Length, i.e. the write would read past the end of the source buffer. Evaluated inside checked to also trap integer overflow of offset + count.

Solutions

  1. Ensure buffer.Length >= offset + count; shrink count to buffer.Length - offset.
  2. Pass the actual number of valid bytes in the buffer as count, not the buffer's capacity.
  3. Add a pre-write assertion/bounds check in the copy helper that computes these values.

Example fix

// before
stream.Write(buffer, offset, buffer.Length);
// after
int count = buffer.Length - offset;
if (count <= 0) return;
stream.Write(buffer, offset, count);
Defensive patterns

Strategy: validation

Validate before calling

if ((long)offset + count > buffer.Length)
    throw new ArgumentException("offset+count exceeds buffer length", nameof(buffer));

Type guard

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

Try / catch

try { stream.Write(buffer, offset, count); }
catch (ArgumentException ex) when (ex.ParamName == "buffer") { /* clamp count = buffer.Length - offset */ }

Prevention

When it happens

Trigger: Calling Write on a packaging stream with buffer.Length=8, offset=4, count=8 (needs 12 bytes); or offset/count values so large their sum overflows int.

Common situations: Mixing up count as an end-index instead of a length; using a destination-buffer length as count while the source buffer is smaller.

Related errors


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

Appendix: source

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

            if (!s.CanWrite)
                throw new NotSupportedException(SR.WriteNotSupported);

            ArgumentNullException.ThrowIfNull(buffer);

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

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

            checked
            {
                if (offset + count > buffer.Length)
                    throw new ArgumentException(SR.WriteBufferTooSmall, nameof(buffer));
            }
        }

        /// <summary>
        /// Read utility that is guaranteed to return the number of bytes requested
        /// if they are available.
        /// </summary>
        /// <param name="stream">stream to read from</param>
        /// <param name="buffer">buffer to read into</param>
        /// <param name="offset">offset in buffer to write to</param>
        /// <param name="count">bytes to read</param>
        /// <returns>bytes read</returns>
        /// <remarks>Normal Stream.Read does not guarantee how many bytes it will
        /// return.  This one does.</remarks>
        internal static int ReliableRead(Stream stream, byte[] buffer, int offset, int count)
        {
            return ReliableRead(stream, buffer, offset, count, count);
        }

View on GitHub (pinned to 81131a70a4)