{"record":{"id":"bd92f6044913123c","repo":"dotnet/wpf","slug":"sr-format-sr-maxlengthexceedsbuffersize-count-textbuffer","errorCode":null,"errorMessage":"SR.Format(SR.MaxLengthExceedsBufferSize, count, textBuffer.Length, startIndex)","messagePattern":"SR\\.Format\\(SR\\.MaxLengthExceedsBufferSize, count, textBuffer\\.Length, startIndex\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/DocumentSequenceTextPointer.cs","lineNumber":670,"sourceCode":"        {\n            ValidationHelper.VerifyDirection(direction, \"direction\");\n\n            ArgumentNullException.ThrowIfNull(textBuffer);\n            if (startIndex < 0)\n            {\n                throw new ArgumentException(SR.Format(SR.NegativeValue, \"startIndex\"));\n            }\n            if (startIndex > textBuffer.Length)\n            {\n                throw new ArgumentException(SR.Format(SR.StartIndexExceedsBufferSize, startIndex, textBuffer.Length));\n            }\n            if (count < 0)\n            {\n                throw new ArgumentException(SR.Format(SR.NegativeValue, \"count\"));\n            }\n            if (count > textBuffer.Length - startIndex)\n            {\n                throw new ArgumentException(SR.Format(SR.MaxLengthExceedsBufferSize, count, textBuffer.Length, startIndex));\n            }\n\n            return thisTp.ChildPointer.GetTextInRun(direction, textBuffer, startIndex, count);\n        }\n\n        /// <summary>\n        /// <see cref=\"ITextPointer.GetAdjacentElement\"/>\n        /// </summary>\n        /// <remarks>Return null if the embedded object does not exist</remarks>\n        public static object GetAdjacentElement(DocumentSequenceTextPointer thisTp, LogicalDirection direction)\n        {\n            ValidationHelper.VerifyDirection(direction, \"direction\");\n\n            return xGapAwareGetEmbeddedElement(thisTp, direction);\n        }\n\n        /// <summary>\n        /// <see cref=\"ITextPointer.GetElementType\"/>","sourceCodeStart":652,"sourceCodeEnd":688,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/DocumentSequenceTextPointer.cs#L652-L688","documentation":"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.","triggerScenarios":"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.","commonSituations":"Loops that pass buffer.Length as count each iteration while advancing startIndex; pre-allocated buffers reused across calls after their size assumptions changed.","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."],"exampleFix":"// before\npointer.GetTextInRun(dir, buffer, start, buffer.Length);\n// after\npointer.GetTextInRun(dir, buffer, start, buffer.Length - start);","handlingStrategy":"validation","validationCode":"count = Math.Min(count, buffer.Length - startIndex);","typeGuard":null,"tryCatchPattern":"try { pointer.GetTextInRun(dir, buffer, start, count); } catch (ArgumentException) { count = buffer.Length - start; }","preventionTips":["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."],"tags":["argument-validation","textpointer","wpf"],"backgroundTag":"argument-out-of-range","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}