dotnet/wpf · error · ArgumentException
SR.MaxLengthExceedsBufferSize (count, textBuffer.Length…
Error message
SR.MaxLengthExceedsBufferSize (count, textBuffer.Length, startIndex)
What it means
TextPointer.GetTextInRun also checks that count does not exceed the space available in the destination buffer: count must be <= textBuffer.Length - startIndex. When it is larger, the copy would overflow the buffer, so it throws ArgumentException(SR.MaxLengthExceedsBufferSize) reporting count, buffer length, and start index.
Solutions
- Clamp the count: count = Math.Min(count, textBuffer.Length - startIndex) before the call.
- Ensure startIndex + count <= textBuffer.Length whenever sizing buffers.
- If the run may be longer than the buffer, loop: call GetTextInRun repeatedly with the remaining buffer space and advance the pointer.
Example fix
// before pointer.GetTextInRun(dir, buffer, start, runLength); // after int count = Math.Min(runLength, buffer.Length - start); pointer.GetTextInRun(dir, buffer, start, count);
Defensive patterns
Strategy: validation
Validate before calling
if (count > buffer.Length - startIndex)
count = buffer.Length - startIndex; Type guard
static bool FitsBuffer(char[] buf, int start, int count) => start >= 0 && count >= 0 && start + count <= buf.Length;
Try / catch
try { pointer.GetTextInRun(dir, buffer, start, count); }
catch (ArgumentException) { count = buffer.Length - start; pointer.GetTextInRun(dir, buffer, start, count); } Prevention
- Always derive count from buffer.Length - startIndex
- Re-size buffers when document content changes
- Loop for long runs instead of sizing one big buffer
When it happens
Trigger: Calling TextPointer.GetTextInRun(direction, textBuffer, startIndex, count) where count > textBuffer.Length - startIndex, e.g. buffer length 10, startIndex 5, count 10 (only 5 slots remain).
Common situations: Allocating a buffer for the full run length but passing a startIndex that shrinks the usable space; reusing a stale buffer size after the document text grew; off-by-one in start/end index arithmetic.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- ' ' is not a valid value for ' '.
- A read or write operation references a location outside the…
- ArgumentException(message, name)
- ArgumentNullException(name)
- ArgumentOutOfRangeException(authentication)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/42c0a68874128235.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextPointer.cs:1764
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;
}
finalCount = 0;
// Loop and combine adjacent text nodes into a single run.
// This isn't just a perf optimization. Because text positionsView on GitHub (pinned to 81131a70a4)