{"record":{"id":"4ccd19d15bb67f97","repo":"dotnet/wpf","slug":"sr-negativevalue-count","errorCode":null,"errorMessage":"SR.NegativeValue (count)","messagePattern":"SR\\.NegativeValue \\(count\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextPointer.cs","lineNumber":1760,"sourceCode":"        }\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\n            {\n                skipCount = (direction == LogicalDirection.Forward) ? nodeOffset : textNode.SymbolCount - nodeOffset;\n                symbolOffset += nodeOffset;\n            }","sourceCodeStart":1742,"sourceCodeEnd":1778,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextPointer.cs#L1742-L1778","documentation":"TextPointer.GetTextInRun validates the caller-supplied buffer parameters before copying text into the destination char array. It throws ArgumentException(SR.NegativeValue, \"count\") when the count parameter is negative, because a negative number of characters to copy is meaningless. This is an eager argument-validation guard before any tree access happens.","triggerScenarios":"Calling any overload of TextPointer.GetTextInRun (direction, textBuffer, startIndex, count) with a negative count value, e.g. a computed count like maxLength - consumed that underflows below zero.","commonSituations":"Looping code that extracts text in chunks and subtracts consumed characters from a remaining count that was miscomputed or already zero; passing an int parsed from config with a negative value; arithmetic overflow when the remaining budget goes negative.","solutions":["Validate count >= 0 before calling GetTextInRun and clamp with Math.Max(0, count).","Fix the loop arithmetic that computes the chunk size so the remaining count cannot go negative (break when remaining <= 0).","Catch ArgumentException around the call if the negative value can legitimately arrive from external input, and treat it as bad input."],"exampleFix":"// before\npointer.GetTextInRun(LogicalDirection.Forward, buffer, start, remaining - consumed);\n// after\nint count = Math.Max(0, remaining - consumed);\nif (count > 0) pointer.GetTextInRun(LogicalDirection.Forward, buffer, start, count);","handlingStrategy":"validation","validationCode":"if (count < 0) throw new ArgumentOutOfRangeException(nameof(count));\n// or clamp:\ncount = Math.Max(0, count);","typeGuard":"static bool IsValidRead(TextPointer p, char[] buf, int start, int count) => count >= 0 && start >= 0 && start + count <= buf.Length;","tryCatchPattern":"try { pointer.GetTextInRun(dir, buffer, start, count); }\ncatch (ArgumentException ex) { /* treat as bad input, clamp and retry */ }","preventionTips":["Clamp chunk counts with Math.Max(0, n) before buffer reads","Break text-extraction loops when remaining <= 0","Validate parsed config values before passing as counts"],"tags":["wpf","argument-validation","textbuffer"],"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-21T21:30:21.729Z"}