{"record":{"id":"6d1e3bbe3874158c","repo":"dotnet/wpf","slug":"sr-negativevalue","errorCode":null,"errorMessage":"SR.NegativeValue","messagePattern":"SR\\.NegativeValue","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FixedTextPointer.cs","lineNumber":128,"sourceCode":"        }\n\n        // <see cref=\"System.Windows.Documents.ITextPointer.GetTextInRun\"/>\n        string ITextPointer.GetTextInRun(LogicalDirection direction)\n        {\n            return TextPointerBase.GetTextInRun(this, 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        int ITextPointer.GetTextInRun(LogicalDirection direction, char[] textBuffer, int startIndex, int count)\n        {\n            ValidationHelper.VerifyDirection(direction, \"direction\");\n            ArgumentNullException.ThrowIfNull(textBuffer);\n            if (count < 0)\n            {\n                throw new ArgumentException(SR.Format(SR.NegativeValue, \"count\"));\n            }\n\n            if (_flowPosition.GetPointerContext(direction) != TextPointerContext.Text)\n            {\n                return 0;\n            }\n            return _flowPosition.GetTextInRun(direction, count, textBuffer, startIndex);\n        }\n\n        /// <summary>\n        /// <see cref=\"ITextPointer.GetAdjacentElement\"/>\n        /// </summary>\n        /// <remarks>Return null if the embedded object does not exist</remarks>\n        object ITextPointer.GetAdjacentElement(LogicalDirection direction)\n        {\n            ValidationHelper.VerifyDirection(direction, \"direction\");\n            TextPointerContext tpc = _flowPosition.GetPointerContext(direction);\n            if (!(tpc == TextPointerContext.EmbeddedElement || tpc == TextPointerContext.ElementStart || tpc == TextPointerContext.ElementEnd))","sourceCodeStart":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FixedTextPointer.cs#L110-L146","documentation":"ITextPointer.GetTextInRun on FixedTextPointer throws ArgumentException(SR.NegativeValue) when the count parameter is negative. The count must be a non-negative number of characters to copy into the textBuffer starting at startIndex.","triggerScenarios":"Calling GetTextInRun(direction, char[] textBuffer, startIndex, count) with count < 0 — typically an off-by-one or subtraction yielding -1.","commonSituations":"Computing count as (endIndex - startIndex) where indices are inverted or empty ranges produce negatives; passing an uninitialized variable; loop boundary errors.","solutions":["Clamp count to zero before calling: count = Math.Max(0, count).","Fix the index computation so end >= start before subtracting.","Validate arguments before the call and skip the call for empty ranges.","Catch ArgumentException and log/repair the range computation."],"exampleFix":"// before\nint count = endIndex - startIndex; // may be negative\npointer.GetTextInRun(dir, buffer, 0, count);\n// after\nint count = Math.Max(0, endIndex - startIndex);\nif (count > 0) pointer.GetTextInRun(dir, buffer, 0, count);","handlingStrategy":"validation","validationCode":"int safeCount = Math.Max(0, requestedCount);\nif (safeCount > 0) pointer.GetTextInRun(direction, buffer, startIndex, safeCount);","typeGuard":null,"tryCatchPattern":"try { n = pointer.GetTextInRun(direction, buffer, startIndex, count); }\ncatch (ArgumentException) { n = 0; /* clamp and recompute */ }","preventionTips":["Always clamp character counts to non-negative before text APIs.","Compute ranges with Math.Max(0, end - start).","Unit-test range calculations for empty and inverted ranges."],"tags":["wpf","documents","argument-exception","text"],"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"}