{"record":{"id":"87e244f142f122ff","repo":"dotnet/wpf","slug":"sr-format-sr-negativevalue-count","errorCode":null,"errorMessage":"SR.Format(SR.NegativeValue, \"count\")","messagePattern":"SR\\.Format\\(SR\\.NegativeValue, \"count\"\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/DocumentSequenceTextPointer.cs","lineNumber":666,"sourceCode":"        /// <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>\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);","sourceCodeStart":648,"sourceCodeEnd":684,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/DocumentSequenceTextPointer.cs#L648-L684","documentation":"GetTextInRun rejects a negative count with ArgumentException (NegativeValue resource). The count is the maximum number of characters to copy; a negative value has no meaning, so the method fails fast rather than treating it as zero.","triggerScenarios":"Calling GetTextInRun(direction, textBuffer, startIndex, count) with count < 0, e.g. remaining-length arithmetic that went negative (buffer.Length - startIndex underflow handled separately, but expressions like end - start can yield negatives).","commonSituations":"Computing count as (someEnd - startIndex) where someEnd < startIndex; passing -1 as an 'unlimited' sentinel, which this API does not support.","solutions":["Pass 0 (or a non-negative value) instead of negative counts; there is no -1 sentinel here.","Fix the subtraction producing the negative remaining length.","Clamp: count = Math.Max(0, count) before calling."],"exampleFix":"// before\npointer.GetTextInRun(dir, buffer, start, end - start);\n// after\nvar count = Math.Max(0, end - start);\npointer.GetTextInRun(dir, buffer, start, count);","handlingStrategy":"validation","validationCode":"if (count < 0) count = 0; // or throw ArgumentOutOfRangeException(nameof(count))","typeGuard":null,"tryCatchPattern":"try { pointer.GetTextInRun(dir, buffer, start, count); } catch (ArgumentException) { count = Math.Max(0, count); }","preventionTips":["Clamp count = Math.Max(0, requested).","Guard subtraction-based counts (end - start) against end < start.","Remember GetTextInRun has no -1 'unlimited' sentinel; pass int.MaxValue-style positives if needed."],"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-22T01:17:13.364Z"}