dotnet/wpf · error · ArgumentOutOfRangeException

SR.WriteCountNegative

Error message

SR.WriteCountNegative

What it means

VerifyStreamWriteArgs rejects a negative count with ArgumentOutOfRangeException(SR.WriteCountNegative). Stream.Write requires count >= 0; the WPF packaging helpers enforce this explicitly before the checked bounds check.

Solutions

  1. Compute count as Math.Max(0, total - read) or validate count >= 0 before writing.
  2. Use (int)(stream.Length - stream.Position) with a non-negativity check when writing the remainder.
  3. Replace sentinel negative values with an explicit branch for 'write everything'.

Example fix

// before
int count = (int)(totalSize - written);
stream.Write(buffer, 0, count);
// after
int count = (int)Math.Max(0, totalSize - written);
stream.Write(buffer, 0, count);
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

static bool IsValidCount(int count) => count >= 0;

Try / catch

try { stream.Write(buffer, offset, count); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "count") { /* clamp count to >= 0 */ }

Prevention

When it happens

Trigger: Passing a byte count computed as a difference (e.g. length - position) that went negative, or forwarding an unvalidated length from another API to a packaging stream Write.

Common situations: Size arithmetic on truncated files where remaining = total - read underflows; passing -1 as a sentinel for 'all bytes'.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/IO/Packaging/PackagingUtilities.cs:150

        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        /// <remarks>common argument verification for Stream.Write</remarks>
        internal static void VerifyStreamWriteArgs(Stream s, byte[] buffer, int offset, int count)
        {
            if (!s.CanWrite)
                throw new NotSupportedException(SR.WriteNotSupported);

            ArgumentNullException.ThrowIfNull(buffer);

            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(offset), SR.OffsetNegative);
            }

            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(count), SR.WriteCountNegative);
            }

            checked
            {
                if (offset + count > buffer.Length)
                    throw new ArgumentException(SR.WriteBufferTooSmall, nameof(buffer));
            }
        }

        /// <summary>
        /// Read utility that is guaranteed to return the number of bytes requested
        /// if they are available.
        /// </summary>
        /// <param name="stream">stream to read from</param>
        /// <param name="buffer">buffer to read into</param>
        /// <param name="offset">offset in buffer to write to</param>
        /// <param name="count">bytes to read</param>
        /// <returns>bytes read</returns>

View on GitHub (pinned to 81131a70a4)