dotnet/wpf · error · ArgumentException
Buffer size is too small to accommodate the specified…
Error message
Buffer size is too small to accommodate the specified parameters.
What it means
ByteStream.Read requires the supplied buffer to be non-empty and large enough to hold count bytes starting at offset: (buffer.Length > 0) && ((buffer.Length - offset) >= count). Otherwise it throws ArgumentException(SR.BufferTooSmall) naming the buffer parameter, before any IStream read occurs.
Solutions
- Clamp count: count = Math.Min(count, buffer.Length - offset) before calling Read.
- Ensure offset + count <= buffer.Length by construction in copy loops.
- Allocate a buffer at least count bytes long when a full read is intended.
Example fix
// before stream.Read(buf, offset, requestedCount); // ArgumentException if too small // after int count = Math.Min(requestedCount, buf.Length - offset); if (count > 0) stream.Read(buf, offset, count);
Defensive patterns
Strategy: validation
Validate before calling
if (buf.Length == 0 || count > buf.Length - offset) count = Math.Max(0, buf.Length - offset);
Try / catch
try { stream.Read(buf, offset, count); }
catch (ArgumentException ex) when (ex.ParamName == "buffer") { count = buf.Length - offset; stream.Read(buf, offset, count); } Prevention
- Always clamp count to buffer.Length - offset
- Verify offset + count <= buffer.Length before reading
- Prefer ArraySegment or Span-based helpers to keep bounds consistent
When it happens
Trigger: Calling Read with buffer.Length < offset + count, e.g. buffer.Length == 0 with count > 0, or reading exactly the remaining chunk after the last chunk partially filled the buffer.
Common situations: Chunked-read loops that forget to shrink count for the final partial chunk; reusing small scratch buffers with oversized counts.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- ' ' is not a valid value for this control.
- Buffer offset cannot be negative.
- Cannot have leading path delimiter.
- CompoundFile path must be non-empty.
- Count of bytes to read cannot be negative.
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/ef784698f2b44b7d.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/ByteStream.cs:279
// 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
if (0 == offset)
{
_securitySuppressedIStream.Read(buffer, count, out read);
}
// offset involved. Must be positive
else if (0 < offset)
{
// Read into local array and then copy it into the given buffer at
// the specified offset.
byte[] localBuffer = new byte[count];
_securitySuppressedIStream.Read(localBuffer, count, out read);
if (read > 0)
{View on GitHub (pinned to 81131a70a4)