diff --git a/src/types/UiaTextRangeBase.cpp b/src/types/UiaTextRangeBase.cpp index 6571d2ea13f..1658e6d2b16 100644 --- a/src/types/UiaTextRangeBase.cpp +++ b/src/types/UiaTextRangeBase.cpp @@ -42,9 +42,15 @@ HRESULT UiaTextRangeBase::RuntimeClassInitialize(_In_ IUiaData* pData, _In_ std::wstring_view wordDelimiters) noexcept try { + RETURN_HR_IF_NULL(E_INVALIDARG, pData); RETURN_IF_FAILED(RuntimeClassInitialize(pData, pProvider, wordDelimiters)); + // GH#8730: The cursor position may be in a delayed state, resulting in it being out of bounds. + // If that's the case, clamp it to be within bounds. + // TODO GH#12440: We should be able to just check some fields off of the Cursor object, + // but Windows Terminal isn't updating those flags properly. _start = cursor.GetPosition(); + pData->GetTextBuffer().GetSize().Clamp(_start); _end = _start; UiaTracing::TextRange::Constructor(*this);