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

  1. Ensure offset is >= 0 before calling Read; clamp or validate the computed offset.
  2. Fix the offset calculation (e.g. guard against underflow when subtracting).
  3. 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

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


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>
        /// VerifyStreamWriteArgs

View on GitHub (pinned to 81131a70a4)