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
- Ensure buffer.Length >= offset + count before calling Read, or allocate a larger buffer.
- If reading from a current position, pass offset=0 and use the full buffer length as count.
- 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
- Always compute count as buffer.Length - offset before reading.
- Use ArraySegment<byte> or Span<byte> so bounds are enforced by the type.
- Never reuse an offset from a previous read without re-validating against the buffer.
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
- SR.WriteBufferTooSmall
- 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/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)