{"record":{"id":"59360a9bcb3eac06","repo":"dotnet/wpf","slug":"nameof-segmentnumber","errorCode":null,"errorMessage":"nameof(segmentNumber)","messagePattern":"nameof\\(segmentNumber\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/TextSelectionProcessor.cs","lineNumber":434,"sourceCode":"        //------------------------------------------------------\n        //\n        //  Private Methods\n        //\n        //------------------------------------------------------\n\n        #region Private Methods\n\n        /// <summary>\n        ///     Extracts the values of attributes from a locator part.\n        /// </summary>\n        /// <param name=\"locatorPart\">the locator part to extract values from</param>\n        /// <param name=\"segmentNumber\">the number of the segment to extract values for</param>\n        /// <param name=\"startOffset\">value of offset attribute</param>\n        /// <param name=\"endOffset\">value of length attribute</param>\n        private static void GetLocatorPartSegmentValues(ContentLocatorPart locatorPart, int segmentNumber, out int startOffset, out int endOffset)\n        {\n            if (segmentNumber < 0)\n                throw new ArgumentException(nameof(segmentNumber));\n\n            ReadOnlySpan<char> segmentString = locatorPart.NameValuePairs[SegmentAttribute + segmentNumber.ToString(NumberFormatInfo.InvariantInfo)].AsSpan();\n\n            Span<Range> splitRegions = stackalloc Range[3];\n            if (segmentString.Split(splitRegions, Separator) != 2)\n            {\n                throw new ArgumentException(SR.Format(SR.InvalidLocatorPart, SegmentAttribute + segmentNumber.ToString(NumberFormatInfo.InvariantInfo)));\n            }\n\n            startOffset = int.Parse(segmentString[splitRegions[0]], NumberFormatInfo.InvariantInfo);\n            endOffset = int.Parse(segmentString[splitRegions[1]], NumberFormatInfo.InvariantInfo);\n        }\n\n        /// <summary>\n        ///     Returns the TextContainer for a node that contains text.  If the node\n        ///     doesn't have a TextContainer this method returns null.\n        /// </summary>\n        private ITextContainer GetTextContainer(DependencyObject startNode)","sourceCodeStart":416,"sourceCodeEnd":452,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/TextSelectionProcessor.cs#L416-L452","documentation":"TextSelectionProcessor.GetLocatorPartSegmentValues throws ArgumentException(nameof(segmentNumber)) when segmentNumber is negative. Segment attributes are keyed 'Segment0','Segment1',... so a negative index is invalid by contract; the exception's parameter name is reported as 'segmentNumber'.","triggerScenarios":"Calling GetLocatorPartSegmentValues with a negative segmentNumber; a loop iterating to a bad Count value or computing an index from user/persisted data that underflows below zero.","commonSituations":"Off-by-one or underflow when enumerating segments of a locator part; corrupted Count value leading to bad index arithmetic.","solutions":["Validate segmentNumber >= 0 before calling","Enumerate segments from 0 to Count-1 using the locator part's Count attribute","Clamp or reject negative indices when reading persisted data"],"exampleFix":"// before\nGetLocatorPartSegmentValues(part, i - 1, out s, out e);\n// after\nif (i - 1 >= 0) GetLocatorPartSegmentValues(part, i - 1, out s, out e);","handlingStrategy":"validation","validationCode":"if (segmentNumber < 0)\n    throw new ArgumentOutOfRangeException(nameof(segmentNumber));","typeGuard":null,"tryCatchPattern":"try { GetLocatorPartSegmentValues(part, i, out s, out e); }\ncatch (ArgumentException ex) when (ex.ParamName == \"segmentNumber\") { /* skip/clamp index */ }","preventionTips":["Enumerate segments with for(int i=0;i<count;i++) using the Count attribute","Clamp indices derived from external data","Guard subtraction that can underflow to -1"],"tags":["wpf","annotations","argument-out-of-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"}