{"record":{"id":"fd2813699639fcfe","repo":"dotnet/wpf","slug":"sr-startindexexceedsbuffersize-startindex-textbuffer-length","errorCode":null,"errorMessage":"SR.StartIndexExceedsBufferSize (startIndex, textBuffer.Length)","messagePattern":"SR\\.StartIndexExceedsBufferSize \\(startIndex, textBuffer\\.Length\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextPointer.cs","lineNumber":1756,"sourceCode":"                ancestor = ancestor.Parent as TextElement;\n            }\n\n            return ancestor as ListItem;\n        }\n\n        internal static int GetTextInRun(TextContainer textContainer, int symbolOffset, TextTreeTextNode textNode, int nodeOffset, LogicalDirection direction, char[] textBuffer, int startIndex, int count)\n        {\n            int skipCount;\n            int finalCount;\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            Invariant.Assert(textNode != null, \"textNode is expected to be non-null\");\n\n            textContainer.EmptyDeadPositionList();\n\n            if (nodeOffset < 0)\n            {\n                skipCount = 0;\n            }\n            else","sourceCodeStart":1738,"sourceCodeEnd":1774,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextPointer.cs#L1738-L1774","documentation":"The same GetTextInRun validation checks that startIndex does not exceed the textBuffer's length; startIndex > textBuffer.Length throws ArgumentException(SR.StartIndexExceedsBufferSize) with startIndex and buffer length. The write position must fall within the destination array for the run text to be copied.","triggerScenarios":"Calling GetTextInRun with startIndex greater than textBuffer.Length — e.g. a startIndex equal to a larger buffer's length after the buffer was reallocated smaller, or passing document offsets as buffer indices by mistake.","commonSituations":"Reusing a buffer sized for an earlier, longer run; confusing document position offsets with char[] indices; computing startIndex from a previous call's return count without checking remaining capacity.","solutions":["Validate startIndex <= textBuffer.Length before calling, and size the buffer to at least startIndex + count.","Reallocate or enlarge the destination buffer when the requested range exceeds its length.","Track remaining capacity (textBuffer.Length - startIndex) and clamp count to it."],"exampleFix":"// before\ntp.GetTextInRunLogicalDirection(direction, buffer, startIndex, count);\n// after\ncount = Math.Min(count, buffer.Length - startIndex);\nif (count > 0)\n    tp.GetTextInRunLogicalDirection(direction, buffer, startIndex, count);","handlingStrategy":"validation","validationCode":"if (startIndex > buffer.Length) throw new InvalidOperationException(\"buffer too small\");\ncount = Math.Min(count, buffer.Length - startIndex);","typeGuard":null,"tryCatchPattern":"try { tp.GetTextInRunLogicalDirection(direction, buffer, startIndex, count); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"buffer\")) { buffer = new char[startIndex + count]; }","preventionTips":["Size destination buffers as startIndex + count before copying","Track remaining capacity (Length - startIndex) and clamp count","Reallocate buffers when document/run sizes change between calls"],"tags":["wpf","textpointer","buffer-size"],"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-22T01:17:13.364Z"}