From fcb982e0c45c21e5a934a9ab7629b971dab4549c Mon Sep 17 00:00:00 2001 From: Carlos Zamora Date: Tue, 8 Feb 2022 11:11:52 -0800 Subject: [PATCH 1/4] Validate cursor position in UIA UTR ctor --- src/types/UiaTextRangeBase.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/types/UiaTextRangeBase.cpp b/src/types/UiaTextRangeBase.cpp index 6571d2ea13f..6e668cf062b 100644 --- a/src/types/UiaTextRangeBase.cpp +++ b/src/types/UiaTextRangeBase.cpp @@ -44,7 +44,15 @@ try { 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#XXXX: 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(); + if (const auto bufferSize{ pData->GetTextBuffer().GetSize() }; !bufferSize.IsInBounds(_start)) + { + bufferSize.Clamp(_start); + } _end = _start; UiaTracing::TextRange::Constructor(*this); From d01b25dc1418cba49d7e942c0c790f7efebb1d0d Mon Sep 17 00:00:00 2001 From: Carlos Zamora Date: Tue, 8 Feb 2022 12:22:40 -0800 Subject: [PATCH 2/4] always clamp --- src/types/UiaTextRangeBase.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/types/UiaTextRangeBase.cpp b/src/types/UiaTextRangeBase.cpp index 6e668cf062b..8121ed2c1d4 100644 --- a/src/types/UiaTextRangeBase.cpp +++ b/src/types/UiaTextRangeBase.cpp @@ -49,10 +49,7 @@ try // TODO GH#XXXX: 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(); - if (const auto bufferSize{ pData->GetTextBuffer().GetSize() }; !bufferSize.IsInBounds(_start)) - { - bufferSize.Clamp(_start); - } + pData->GetTextBuffer().GetSize().Clamp(_start); _end = _start; UiaTracing::TextRange::Constructor(*this); From 74ff0b26a8f111c178bbeb16b295913a4357a906 Mon Sep 17 00:00:00 2001 From: Carlos Zamora Date: Tue, 8 Feb 2022 13:35:47 -0800 Subject: [PATCH 3/4] link follow-up issue --- src/types/UiaTextRangeBase.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types/UiaTextRangeBase.cpp b/src/types/UiaTextRangeBase.cpp index 8121ed2c1d4..2fb7d2ec1fe 100644 --- a/src/types/UiaTextRangeBase.cpp +++ b/src/types/UiaTextRangeBase.cpp @@ -46,7 +46,7 @@ try // 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#XXXX: We should be able to just check some fields off of the Cursor object, + // 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); From b8ceb3f4a6aa39142b6d9485bf0dd5be36bcf2fa Mon Sep 17 00:00:00 2001 From: Carlos Zamora Date: Tue, 8 Feb 2022 15:15:09 -0800 Subject: [PATCH 4/4] appease the static analyzer --- src/types/UiaTextRangeBase.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/types/UiaTextRangeBase.cpp b/src/types/UiaTextRangeBase.cpp index 2fb7d2ec1fe..1658e6d2b16 100644 --- a/src/types/UiaTextRangeBase.cpp +++ b/src/types/UiaTextRangeBase.cpp @@ -42,6 +42,7 @@ 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.