{"record":{"id":"85f5c374466dc14b","repo":"dotnet/wpf","slug":"sr-incorrectanchorlength","errorCode":null,"errorMessage":"SR.IncorrectAnchorLength","messagePattern":"SR\\.IncorrectAnchorLength","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/TextSelectionProcessor.cs","lineNumber":242,"sourceCode":"                    return null;\n\n                ITextPointer start = elementStart.CreatePointer(startOffset);// new TextPointer((TextPointer)elementStart, startOffset);\n\n                ITextPointer end = (textRangeLength <= endOffset) ?\n                    elementEnd.CreatePointer() : //new TextPointer((TextPointer)elementEnd) :\n                    elementStart.CreatePointer(endOffset);// new TextPointer((TextPointer)elementStart, endOffset);\n\n                //we do not process 0 length selection\n                if (start.CompareTo(end) >= 0)\n                    return null;\n\n                anchor.AddTextSegment(start, end);\n            }\n\n            //we do not support 0 or negative length selection\n            if (anchor.IsEmpty)\n            {\n                throw new ArgumentException(SR.IncorrectAnchorLength, nameof(locatorPart));\n            }\n\n            attachmentLevel = AttachmentLevel.Full;\n\n            if (_clamping)\n            {\n                ITextPointer currentStart = anchor.Start;\n                ITextPointer currentEnd = anchor.End;\n                IServiceProvider serviceProvider = null;\n                ITextView textView = null;\n\n                if (_targetPage != null)\n                {\n                    serviceProvider = _targetPage as IServiceProvider;\n                }\n                else\n                {\n                    FlowDocument content = currentStart.TextContainer.Parent as FlowDocument;","sourceCodeStart":224,"sourceCodeEnd":260,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/TextSelectionProcessor.cs#L224-L260","documentation":"TextSelectionProcessor.ResolveLocatorPart throws ArgumentException(SR.IncorrectAnchorLength) when the text anchor built from the supplied TextAnchorLocatorPart resolves to an empty (zero or negative length) selection. WPF Annotations anchoring requires a non-empty text segment range to attach an annotation to; an empty selection has no span to anchor against.","triggerScenarios":"Calling ResolveLocatorPart with a ContentLocatorPart (TextSelectionProcessor) whose parsed segment start/end offsets produce an empty anchor — e.g. start==end or end<start, or a segment string encoding a zero-length range.","commonSituations":"Persisted annotation locators from an older document whose text shrank or was deleted; programmatically generated locator parts with equal start/end offsets; off-by-one in offset computation when creating anchors.","solutions":["Verify the segment's start and end offsets differ and end>start before creating/serializing the locator part","Re-resolve the annotation after re-anchoring against refreshed text so offsets are recomputed","Validate persisted locator parts when loading and drop/repair ones with empty ranges"],"exampleFix":"// before\npart.NameValuePairs[\"Segment0\"] = \"10,10\"; // empty range\n// after\npart.NameValuePairs[\"Segment0\"] = \"10,25\"; // start < end","handlingStrategy":"validation","validationCode":"// parse segment offsets before resolving\nvar kv = part.NameValuePairs[\"Segment0\"].Split(',');\nint s = int.Parse(kv[0], System.Globalization.CultureInfo.InvariantCulture);\nint e = int.Parse(kv[1], System.Globalization.CultureInfo.InvariantCulture);\nif (e <= s) throw new InvalidOperationException(\"Anchor range must be non-empty (end > start).\");","typeGuard":null,"tryCatchPattern":"try { processor.ResolveLocatorPart(part, out level); }\ncatch (ArgumentException ex) { /* log ex.ParamName; treat annotation as unresolvable */ }","preventionTips":["Always create locator parts via TextSelectionProcessor.CreateLocatorPart","Assert start<end when encoding segment strings","Re-anchor persisted annotations when the underlying text changes"],"tags":["wpf","annotations","anchoring","argument-exception"],"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"}