{"record":{"id":"7f3c562d6cf58909","repo":"dotnet/wpf","slug":"start-or-end-specified-is-past-the-end-of-the-text-range","errorCode":null,"errorMessage":"Start or end specified is past the end of the text range.","messagePattern":"Start or end specified is past the end of the text range\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsEditBoxRange.cs","lineNumber":1077,"sourceCode":"\n        // method to set both endpoints simultaneously\n        private void MoveTo(int start, int end)\n        {\n            if (start < 0 || end < start)\n            {\n                throw new InvalidOperationException(SR.Format(SR.InvalidTextRangeOffset, GetType().FullName));\n            }\n\n            _start = start;\n            _end = end;\n        }\n\n        private void ValidateEndpoints()\n        {\n            int limit = _provider.GetTextLength();\n            if (Start > limit || End > limit)\n            {\n                throw new InvalidOperationException(SR.Format(SR.InvalidRangeEndpoint, GetType().FullName));\n            }\n        }\n\n        #endregion Private Methods\n\n        //------------------------------------------------------\n        //\n        //  Private Properties\n        //\n        //------------------------------------------------------\n\n        #region Private Properties\n\n        private bool IsDegenerate\n        {\n            get\n            {\n                // strictly only needs to be == since never should _start>_end.","sourceCodeStart":1059,"sourceCodeEnd":1095,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsEditBoxRange.cs#L1059-L1095","documentation":"ValidateEndpoints verifies that both endpoints of the WindowsEditBoxRange still lie within the provider's text; if Start or End exceeds GetTextLength(), it throws InvalidOperationException with SR.InvalidRangeEndpoint ('Start or end specified is past the end of the text range').","triggerScenarios":"Using a range after the underlying edit-box text shrank (text cleared, shortened, or control repurposed) so that cached offsets now exceed the text length.","commonSituations":"Long-lived automation ranges held across document edits, automated tests typing/clearing text between range operations, or text set asynchronously while a range from the old content is still in use.","solutions":["Re-acquire the range from the current document state after any text modification.","Before using a cached range, check range endpoints against provider.GetTextLength() and rebuild if out of bounds.","Catch InvalidOperationException and recreate the range via TextPattern.DocumentRange/RangeFromChild.","Synchronize range usage with text-changed events (AutomationPropertyChanged/TextChanged) instead of caching indefinitely."],"exampleFix":"// before\n// range captured earlier\nrange.Select();\n// after\nif (range == null) range = textPattern.DocumentRange;\ntry { range.Select(); }\ncatch (InvalidOperationException) { range = textPattern.DocumentRange; range.Select(); }","handlingStrategy":"validation","validationCode":"int len = provider.GetTextLength();\nbool endpointsValid = range != null; // UIA ranges expose no direct endpoints; re-validate by use\nif (textWasModified) { range = textPattern.DocumentRange; }","typeGuard":null,"tryCatchPattern":"try { range.Select(); }\ncatch (InvalidOperationException) { range = textPattern.DocumentRange; range.Select(); }","preventionTips":["Do not cache TextPattern ranges across text edits","Subscribe to TextChanged/ValuePattern changes and refresh ranges","Re-acquire DocumentRange after clear/replace operations","In tests, recreate ranges after each text mutation step"],"tags":["uiautomation","textrange","stale-state","out-of-bounds"],"backgroundTag":"value-out-of-range","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"}