dotnet/wpf · error · ArgumentException
SR.Format(SR.MaxLengthExceedsBufferSize, count…
Error message
SR.Format(SR.MaxLengthExceedsBufferSize, count, textBuffer.Length, startIndex)
What it means
GetTextInRun throws ArgumentException when count exceeds the remaining space in textBuffer (count > textBuffer.Length - startIndex). The destination region starting at startIndex must be large enough for the requested character count; the resource reports count, buffer length and start index.
Solutions
- Pass count = textBuffer.Length - startIndex (the remaining space) instead of buffer.Length.
- Compute the request size against the current startIndex each iteration.
- Grow the buffer or cap the request to the available space.
Example fix
// before pointer.GetTextInRun(dir, buffer, start, buffer.Length); // after pointer.GetTextInRun(dir, buffer, start, buffer.Length - start);
Defensive patterns
Strategy: validation
Validate before calling
count = Math.Min(count, buffer.Length - startIndex);
Try / catch
try { pointer.GetTextInRun(dir, buffer, start, count); } catch (ArgumentException) { count = buffer.Length - start; } Prevention
- Always request at most buffer.Length - startIndex characters.
- In loops, compute remaining space per iteration.
- Write a wrapper that does the clamping once and use it everywhere.
When it happens
Trigger: Requesting more characters than textBuffer can hold from startIndex onward — e.g. passing the whole-buffer size as count while startIndex > 0, or underestimating how much of the buffer is already used.
Common situations: Loops that pass buffer.Length as count each iteration while advancing startIndex; pre-allocated buffers reused across calls after their size assumptions changed.
Related errors
- SR.BadDistance
- SR.BadTextPositionOrder
- SR.Format(SR.NegativeValue, "count")
- SR.Format(SR.NegativeValue, "startIndex")
- SR.Format(SR.StartIndexExceedsBufferSize, startIndex…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/bd92f6044913123c.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/DocumentSequenceTextPointer.cs:670
{
ValidationHelper.VerifyDirection(direction, "direction");
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));
}
return thisTp.ChildPointer.GetTextInRun(direction, textBuffer, startIndex, count);
}
/// <summary>
/// <see cref="ITextPointer.GetAdjacentElement"/>
/// </summary>
/// <remarks>Return null if the embedded object does not exist</remarks>
public static object GetAdjacentElement(DocumentSequenceTextPointer thisTp, LogicalDirection direction)
{
ValidationHelper.VerifyDirection(direction, "direction");
return xGapAwareGetEmbeddedElement(thisTp, direction);
}
/// <summary>
/// <see cref="ITextPointer.GetElementType"/>View on GitHub (pinned to 81131a70a4)