{"record":{"id":"416455c4d4416766","repo":"dotnet/wpf","slug":"sr-baddistance-textpointer","errorCode":null,"errorMessage":"SR.BadDistance","messagePattern":"SR\\.BadDistance","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextPointer.cs","lineNumber":289,"sourceCode":"        {\n            InitializeOffset(position, 0, direction);\n        }\n\n        // Creates a new TextPointer instance.\n        internal TextPointer(TextPointer position, int offset, LogicalDirection direction)\n        {\n            InitializeOffset(position, offset, direction);\n        }\n\n        // Creates a new TextPointer instance.\n        internal TextPointer(TextContainer textContainer, int offset, LogicalDirection direction)\n        {\n            SplayTreeNode node;\n            ElementEdge edge;\n\n            if (offset < 1 || offset > textContainer.InternalSymbolCount - 1)\n            {\n                throw new ArgumentException(SR.BadDistance);\n            }\n\n            textContainer.GetNodeAndEdgeAtOffset(offset, out node, out edge);\n\n            Initialize(textContainer, (TextTreeNode)node, edge, direction, textContainer.PositionGeneration, false, false, textContainer.LayoutGeneration);\n        }\n\n        // Creates a new TextPointer instance.\n        internal TextPointer(TextContainer tree, TextTreeNode node, ElementEdge edge)\n        {\n            Initialize(tree, node, edge, LogicalDirection.Forward, tree.PositionGeneration, false, false, tree.LayoutGeneration);\n        }\n\n        // Creates a new TextPointer instance.\n        internal TextPointer(TextContainer tree, TextTreeNode node, ElementEdge edge, LogicalDirection direction)\n        {\n            Initialize(tree, node, edge, direction, tree.PositionGeneration, false, false, tree.LayoutGeneration);\n        }","sourceCodeStart":271,"sourceCodeEnd":307,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextPointer.cs#L271-L307","documentation":"This is the TextPointer(TextContainer, int) constructor validating the symbol offset. The offset must lie strictly between 1 and InternalSymbolCount - 1 (i.e. within the document's actual content, excluding boundary symbols). Offsets outside that range throw ArgumentException(SR.BadDistance).","triggerScenarios":"Constructing a TextPointer with offset < 1, or an offset greater than or equal to the container's internal symbol count — typically an offset captured before text was deleted, or offset computed from a different/older tree.","commonSituations":"Storing a character offset and later creating a TextPointer after the document changed (text removed shrinks symbol count); using raw offsets from serialization against a freshly loaded document; off-by-one using count instead of count-1.","solutions":["Validate the offset against textContainer.SymbolCount (1 <= offset <= SymbolCount-1) before constructing the TextPointer.","Prefer persistent TextPointer instances or manual movers over stored raw offsets so positions track edits.","Re-clamp the offset to the current document length before use."],"exampleFix":"// before\nvar tp = new TextPointer(container, savedOffset);\n// after\nint offset = Math.Max(1, Math.Min(savedOffset, container.SymbolCount - 1));\nvar tp = new TextPointer(container, offset);","handlingStrategy":"validation","validationCode":"bool IsValidOffset(TextContainer c, int offset) => offset >= 1 && offset <= c.SymbolCount - 1;\nif (!IsValidOffset(container, offset)) offset = Math.Max(1, Math.Min(offset, container.SymbolCount - 1));","typeGuard":null,"tryCatchPattern":"try { tp = new TextPointer(container, offset); }\ncatch (ArgumentException) { tp = new TextPointer(container, Math.Max(1, container.SymbolCount - 1)); }","preventionTips":["Re-clamp stored offsets against the current symbol count before use","Prefer TextPointer instances (which track edits) over raw offsets","Remember valid offsets exclude boundary symbols: 1..SymbolCount-1"],"tags":["wpf","textpointer","argument-range"],"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"}