dotnet/wpf · error · ArgumentOutOfRangeException

Cannot set seek pointer to negative position.

Error message

Cannot set seek pointer to negative position.

What it means

When seeking with SeekOrigin.Begin, ByteStream rejects negative offsets with ArgumentOutOfRangeException(SR.SeekNegative). A negative absolute position is meaningless, so the call is refused before touching the underlying IStream.

Solutions

  1. Clamp or validate the offset before seeking: if (offset < 0) offset = 0;
  2. Use SeekOrigin.Current with a negative offset if a backward relative seek was intended.
  3. Fix the offset arithmetic that produced the negative value (check units and header parsing).

Example fix

// before
stream.Seek(delta, SeekOrigin.Begin); // ArgumentOutOfRangeException when delta<0
// after
stream.Seek(delta, delta >= 0 ? SeekOrigin.Begin : SeekOrigin.Current);
Defensive patterns

Strategy: validation

Validate before calling

if (offset < 0) throw new ArgumentOutOfRangeException(nameof(offset));

Try / catch

try { stream.Seek(offset, SeekOrigin.Begin); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "offset") { /* fix/clamp offset */ }

Prevention

When it happens

Trigger: Calling Seek(offset, SeekOrigin.Begin) with a negative offset, usually from arithmetic like Seek(relativeOffset, SeekOrigin.Begin) where relativeOffset was computed from subtraction or an uninitialized variable.

Common situations: Parsing container/part offsets where a header value is misread (endian mismatch), or code confusing SeekOrigin.Current semantics (where negatives are legal) with SeekOrigin.Begin semantics.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/ByteStream.cs:202

        public override long Seek(long offset, SeekOrigin origin)
        {
            CheckDisposedStatus();

            if (!CanSeek)
            {
                throw new NotSupportedException(SR.SeekNotSupported);
            }

            long seekPos = 0;
            int translatedSeekOrigin = 0;

            switch (origin)
            {
                case SeekOrigin.Begin:
                    translatedSeekOrigin = NativeMethods.STREAM_SEEK_SET;
                    if (0 > offset)
                    {
                        throw new ArgumentOutOfRangeException(nameof(offset),
                                                              SR.SeekNegative);
                    }
                    break;

                case SeekOrigin.Current:
                    translatedSeekOrigin = NativeMethods.STREAM_SEEK_CUR;
                    break;

                case SeekOrigin.End:
                    translatedSeekOrigin = NativeMethods.STREAM_SEEK_END;
                    break;
                default:
                    throw new System.ComponentModel.InvalidEnumArgumentException("origin",
                                                                                 (int)origin,
                                                                                 typeof(SeekOrigin));
            }

            _securitySuppressedIStream.Seek(offset, translatedSeekOrigin, out seekPos);

View on GitHub (pinned to 81131a70a4)