dotnet/wpf · error · ArgumentException

SR.NegativeValue (count)

Error message

SR.NegativeValue (count)

What it means

TextPointer.GetTextInRun validates the caller-supplied buffer parameters before copying text into the destination char array. It throws ArgumentException(SR.NegativeValue, "count") when the count parameter is negative, because a negative number of characters to copy is meaningless. This is an eager argument-validation guard before any tree access happens.

Solutions

  1. Validate count >= 0 before calling GetTextInRun and clamp with Math.Max(0, count).
  2. Fix the loop arithmetic that computes the chunk size so the remaining count cannot go negative (break when remaining <= 0).
  3. Catch ArgumentException around the call if the negative value can legitimately arrive from external input, and treat it as bad input.

Example fix

// before
pointer.GetTextInRun(LogicalDirection.Forward, buffer, start, remaining - consumed);
// after
int count = Math.Max(0, remaining - consumed);
if (count > 0) pointer.GetTextInRun(LogicalDirection.Forward, buffer, start, count);
Defensive patterns

Strategy: validation

Validate before calling

if (count < 0) throw new ArgumentOutOfRangeException(nameof(count));
// or clamp:
count = Math.Max(0, count);

Type guard

static bool IsValidRead(TextPointer p, char[] buf, int start, int count) => count >= 0 && start >= 0 && start + count <= buf.Length;

Try / catch

try { pointer.GetTextInRun(dir, buffer, start, count); }
catch (ArgumentException ex) { /* treat as bad input, clamp and retry */ }

Prevention

When it happens

Trigger: Calling any overload of TextPointer.GetTextInRun (direction, textBuffer, startIndex, count) with a negative count value, e.g. a computed count like maxLength - consumed that underflows below zero.

Common situations: Looping code that extracts text in chunks and subtracts consumed characters from a remaining count that was miscomputed or already zero; passing an int parsed from config with a negative value; arithmetic overflow when the remaining budget goes negative.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextPointer.cs:1760

        }

        internal static int GetTextInRun(TextContainer textContainer, int symbolOffset, TextTreeTextNode textNode, int nodeOffset, LogicalDirection direction, char[] textBuffer, int startIndex, int count)
        {
            int skipCount;
            int finalCount;

            ArgumentNullException.ThrowIfNull(textBuffer);
            if (startIndex < 0)
            {
                throw new ArgumentException(SR.Format(SR.NegativeValue, "startIndex"));
            }
            if (startIndex > textBuffer.Length)
            {
                throw new ArgumentException(SR.Format(SR.StartIndexExceedsBufferSize, startIndex, textBuffer.Length));
            }
            if (count < 0)
            {
                throw new ArgumentException(SR.Format(SR.NegativeValue, "count"));
            }
            if (count > textBuffer.Length - startIndex)
            {
                throw new ArgumentException(SR.Format(SR.MaxLengthExceedsBufferSize, count, textBuffer.Length, startIndex));
            }
            Invariant.Assert(textNode != null, "textNode is expected to be non-null");

            textContainer.EmptyDeadPositionList();

            if (nodeOffset < 0)
            {
                skipCount = 0;
            }
            else
            {
                skipCount = (direction == LogicalDirection.Forward) ? nodeOffset : textNode.SymbolCount - nodeOffset;
                symbolOffset += nodeOffset;
            }

View on GitHub (pinned to 81131a70a4)