dotnet/wpf · error · ArgumentException

Cannot set position of the stream to a negative value.

Error message

Cannot set position of the stream to a negative value.

What it means

ArgumentException thrown by the Position setter on UnsafeIndexingFilterStream when the caller assigns a negative value: the seek-based logical position cannot be moved before the beginning of the stream.

Solutions

  1. Clamp the requested position to >= 0 before assigning
  2. Validate user-supplied offsets (e.g. from a parser or progress calculation) for negative values before seeking
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/UnsafeIndexingFilterStream.cs:240 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/UnsafeIndexingFilterStream.cs:240

            }
        }

        /// <summary>
        /// Logical byte position in this stream
        /// </summary>
        public override long Position
        {
            get
            {
                ThrowIfStreamDisposed();
                return Seek(0, SeekOrigin.Current);
            }
            set
            {
                ThrowIfStreamDisposed();

                if (value < 0)
                    throw new ArgumentException(SR.CannotSetNegativePosition);

                Seek(value, SeekOrigin.Begin);
            }
        }

        /// <summary>
        /// Length.
        /// </summary>
        public override long Length
        {
            get
            {
                ThrowIfStreamDisposed();

                // Retrieve stream stats. STATFLAG_NONAME means don't return the stream name.
                System.Runtime.InteropServices.ComTypes.STATSTG statstg;
                _oleStream.Stat(out statstg, NativeMethods.STATFLAG_NONAME);
                return statstg.cbSize;

View on GitHub (pinned to 81131a70a4)