{"record":{"id":"cb182cae22199796","repo":"dotnet/wpf","slug":"sr-negativevalue-startindex","errorCode":null,"errorMessage":"SR.NegativeValue (startIndex)","messagePattern":"SR\\.NegativeValue \\(startIndex\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextPointer.cs","lineNumber":1752,"sourceCode":"            TextElement ancestor = this.Parent as TextElement;\n\n            while (ancestor != null && !(ancestor is ListItem))\n            {\n                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)","sourceCodeStart":1734,"sourceCodeEnd":1770,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextPointer.cs#L1734-L1770","documentation":"TextPointer.GetTextInRun validates the caller-supplied destination char[] (textBuffer) before copying run text. A negative startIndex throws ArgumentException(SR.NegativeValue, \"startIndex\"). This internal overload copies at most count characters into textBuffer starting at startIndex, so all slice parameters must be valid.","triggerScenarios":"Calling GetTextInRun with a negative startIndex — typically a computed offset (e.g. a subtraction like position - length) that underflowed, or an uninitialized/default int passed to the buffer-copy overload.","commonSituations":"Manual run-walking loops where the start index arithmetic can go negative near the start of the buffer; passing -1 as a sentinel value; off-by-one loops wrapping around to negative indices.","solutions":["Clamp startIndex to Math.Max(0, startIndex) before the call.","Fix the index arithmetic that produces the negative value (inspect any subtraction feeding startIndex).","Prefer the simpler GetTextInRun(TextPointerContext, int) overload returning a string if you do not need the char[] destination form."],"exampleFix":"// before\ntp.GetTextInRunLogicalDirection(direction, buffer, startIndex, count);\n// after\nstartIndex = Math.Max(0, startIndex);\ntp.GetTextInRunLogicalDirection(direction, buffer, startIndex, count);","handlingStrategy":"validation","validationCode":"if (startIndex < 0) startIndex = 0;","typeGuard":null,"tryCatchPattern":"try { tp.GetTextInRunLogicalDirection(direction, buffer, startIndex, count); }\ncatch (ArgumentException ex) when (ex.ParamName == null && ex.Message.Contains(\"startIndex\")) { startIndex = 0; }","preventionTips":["Clamp computed offsets with Math.Max(0, x) before buffer operations","Avoid sentinel values like -1 for start indices","Verify index arithmetic in run-walking loops at buffer boundaries"],"tags":["wpf","textpointer","argument-range"],"backgroundTag":"invalid-argument-value","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"}