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
- Ensure buffer.Length >= offset + count; shrink count to buffer.Length - offset.
- Pass the actual number of valid bytes in the buffer as count, not the buffer's capacity.
- 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
- Cast to long when checking offset + count to avoid int overflow.
- Pass the number of valid bytes, not the buffer capacity, as count.
- Centralize buffer bounds checking in one copy helper.
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
- SR.ReadBufferTooSmall
- SR.WriteCountNegative
- ' ' is not a valid value for ' '.
- A read or write operation references a location outside the…
- Specified argument was out of the range of valid values.
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)