{"record":{"id":"cb44e60042473713","repo":"dotnet/wpf","slug":"sr-format-sr-negativevalue-startindex","errorCode":null,"errorMessage":"SR.Format(SR.NegativeValue, \"startIndex\")","messagePattern":"SR\\.Format\\(SR\\.NegativeValue, \"startIndex\"\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/DocumentSequenceTextPointer.cs","lineNumber":658,"sourceCode":"        public static int GetTextRunLength(DocumentSequenceTextPointer thisTp, LogicalDirection direction)\n        {\n            ValidationHelper.VerifyDirection(direction, \"direction\");\n\n            return thisTp.ChildPointer.GetTextRunLength(direction);\n        }\n\n        /// <summary>\n        /// <see cref=\"ITextPointer.GetTextInRun(LogicalDirection,char[],int,int)\"/>\n        /// </summary>\n        /// <remarks>Only reutrn uninterrupted runs of text</remarks>\n        public static int GetTextInRun(DocumentSequenceTextPointer thisTp, LogicalDirection direction, char[] textBuffer, int startIndex, int count)\n        {\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>","sourceCodeStart":640,"sourceCodeEnd":676,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/DocumentSequenceTextPointer.cs#L640-L676","documentation":"DocumentSequenceTextPointer.GetTextInRun validates its arguments before delegating to the underlying child pointer. A negative startIndex is meaningless for a buffer offset, so it throws ArgumentException with the NegativeValue resource. This is strict input validation on a public ITextPointer API.","triggerScenarios":"Calling GetTextInRun(direction, textBuffer, startIndex, count) with startIndex < 0, e.g. a variable initialized to -1 as a sentinel or an underflowing computed offset.","commonSituations":"Arithmetic that can go negative when walking backwards through a buffer without clamping; passing an index derived from an empty or failed extraction; sentinel -1 values leaking into the call.","solutions":["Clamp or assert startIndex >= 0 before the call.","Fix the upstream offset computation that produced the negative value.","Throw a descriptive error in the caller when the computed index is negative."],"exampleFix":"// before\npointer.GetTextInRun(dir, buffer, index, count);\n// after\nif (index < 0) index = 0;\npointer.GetTextInRun(dir, buffer, index, count);","handlingStrategy":"validation","validationCode":"if (startIndex < 0) throw new ArgumentOutOfRangeException(nameof(startIndex), startIndex, \"Must be non-negative.\");","typeGuard":null,"tryCatchPattern":"try { pointer.GetTextInRun(dir, buffer, start, count); } catch (ArgumentException) { /* log invalid offset; clamp to 0 */ }","preventionTips":["Clamp computed offsets with Math.Max(0, index).","Avoid -1 sentinels for buffer indices.","Assert non-negative indices in debug builds before calling text APIs."],"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"}