dotnet/wpf · error · ArgumentOutOfRangeException

ArgumentOutOfRangeException(nameof(offset))

Error message

ArgumentOutOfRangeException(nameof(offset))

What it means

After WriterSeek computes the new write position, it validates that the position lies within the valid window (<= WriteLength and >= ReadLength). If not, it throws ArgumentOutOfRangeException named 'offset', meaning the requested seek offset would move the write position outside the readable/writable span of the internal buffer.

Solutions

  1. Clamp or compute the offset so the resulting position stays within [ReadLength, WriteLength].
  2. Seek from SeekOrigin.Begin with a known-valid absolute position instead of relative math.
  3. Inspect WriteLength/ReadLength before seeking to confirm the target position is in range.

Example fix

// before
stream.Seek(-100, SeekOrigin.Current); // lands before ReadLength
// after
long target = Math.Max(stream.Length, 0);
stream.Seek(target, SeekOrigin.Begin);
Defensive patterns

Strategy: validation

Validate before calling

long target = origin == SeekOrigin.Begin ? offset : currentPos + offset;
if (target < 0 || target > maxValidPosition) throw new ArgumentOutOfRangeException(nameof(offset));

Try / catch

try { stream.Seek(offset, origin); }
catch (ArgumentOutOfRangeException) { /* clamp offset and retry */ }

Prevention

When it happens

Trigger: Calling Seek(offset, SeekOrigin.Begin) with a negative offset or one beyond WriteLength, or Seek(offset, SeekOrigin.Current) where the result lands before ReadLength or past WriteLength.

Common situations: Bookkeeping errors in stream positions when interleaving reads and writes on the internal XAML parser stream; assumptions about stream length that don't hold for this pipe-like stream.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/751ad44539c44960. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlStream.cs:162

            switch(loc)
            {
                case SeekOrigin.Begin:
                    WritePosition = (int) offset;
                    break;
                case SeekOrigin.Current:
                    WritePosition = (int) (WritePosition + offset);
                    break;
                case SeekOrigin.End:
                    throw new NotSupportedException(SR.ParserWriterNoSeekEnd);
                default:
                    throw new ArgumentException(SR.ParserWriterUnknownOrigin);
            }

            if( (!( WritePosition <= WriteLength ))
                ||
                (!( WritePosition >= ReadLength  )) )
            {
                throw new ArgumentOutOfRangeException( nameof(offset));
            }

            return WritePosition;
        }


        /// <summary>
        /// Called by the Writer to indicate its okay for the reader to see
        /// the file up to and including the position. Once the Writer calls
        /// this it cannot go back and change the content.
        /// </summary>
        /// <param name="position">Absolute position in the stream</param>
        internal void UpdateReaderLength(long position)
        {
            ArgumentOutOfRangeException.ThrowIfLessThan(position, ReadLength);
#if DEBUG
            Debug.Assert(!WriteComplete,"UpdateReaderLength called after close");
#endif

View on GitHub (pinned to 81131a70a4)