dotnet/wpf · error · ArgumentOutOfRangeException
SR.OffsetNegative
Error message
SR.OffsetNegative
What it means
VerifyStreamReadArgs throws ArgumentOutOfRangeException(nameof(offset), SR.OffsetNegative) when a Stream.Read call is made with a negative offset into the destination buffer. The offset must be a valid non-negative index within the buffer.
Solutions
- Ensure offset is >= 0 before calling Read; clamp or validate the computed offset.
- Fix the offset calculation (e.g. guard against underflow when subtracting).
- Replace sentinel -1 values with an explicit error path instead of calling Read.
Example fix
// before stream.Read(buffer, offset, count); // offset could be -1 // after if (offset < 0) throw new ArgumentException(nameof(offset)); stream.Read(buffer, offset, count);
Defensive patterns
Strategy: validation
Validate before calling
if (offset < 0) throw new ArgumentOutOfRangeException(nameof(offset));
Type guard
bool ValidReadArgs(int offset, int count) => offset >= 0 && count >= 0 && offset + count <= buffer.Length;
Try / catch
try { stream.Read(buffer, offset, count); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "offset") { /* fix offset computation */ } Prevention
- Validate offset >= 0 before any Stream.Read.
- Guard offset arithmetic against underflow.
- Avoid using -1 as an offset sentinel.
When it happens
Trigger: Passing a negative offset parameter to a packaging Stream.Read call, often from an uninitialized int or a miscalculated position.
Common situations: Arithmetic underflow when computing the offset; copying a negative saved offset variable; defaulting offset to an error sentinel like -1 and passing it through.
Related errors
- SR.ReadCountNegative
- ' ' ID is not a valid XSD ID.
- A read or write operation references a location outside the…
- ArgumentOutOfRangeException: offset is outside the valid…
- Cannot access Stream object because it was closed or…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/26d29df634386062.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/IO/Packaging/PackagingUtilities.cs:111
/// <summary>
/// VerifyStreamReadArgs
/// </summary>
/// <param name="s">stream</param>
/// <param name="buffer">buffer</param>
/// <param name="offset">offset</param>
/// <param name="count">count</param>
/// <remarks>Common argument verification for Stream.Read()</remarks>
internal static void VerifyStreamReadArgs(Stream s, byte[] buffer, int offset, int count)
{
if (!s.CanRead)
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>
/// VerifyStreamWriteArgsView on GitHub (pinned to 81131a70a4)