From 3fc198cf3c837cd14bc74d2012c53a41318e67a7 Mon Sep 17 00:00:00 2001 From: Michael Weghorn Date: Fri, 4 Aug 2023 19:27:58 +0200 Subject: [PATCH] a11y uia: Don't return "S_OK" and null text range MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When QWindowsUiaTextProvider::RangeFromPoint was called with a point that is not over any character, it was previously returning S_OK and a nullptr for the text range. This is contrary to what the ITextProvider::RangeFromPoint documentation [1] says: > If this method succeeds, it returns S_OK. > Otherwise, it returns an HRESULT error code. and > The property never returns NULL. Therefore, setting pRetVal to NULL and returning S_OK at the same time is problematic. Return UIA_E_ELEMENTNOTAVAILABLE instead for that case, and only return S_OK when actually setting a valid text range provider as well. Ideally, this should return an empty range for the character that is closest to the given point. That one could be identified by iterating over all characters and calculating their distance to the given point, but that would be too expensive. [1] https://learn.microsoft.com/en-us/windows/win32/api/uiautomationcore/nf-uiautomationcore-itextprovider-rangefrompoint Fixes: QTBUG-115801 Change-Id: Ib08d02677935a45517c937613785f1e3f53ee032 Reviewed-by: Jan Arve Sæther (cherry picked from commit 9900a12df649229c9ff9e2115ac90fb7f634da1d) Reviewed-by: Qt Cherry-pick Bot --- .../windows/uiautomation/qwindowsuiatextprovider.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/plugins/platforms/windows/uiautomation/qwindowsuiatextprovider.cpp b/src/plugins/platforms/windows/uiautomation/qwindowsuiatextprovider.cpp index e3b7c69b601..ae108c604d1 100644 --- a/src/plugins/platforms/windows/uiautomation/qwindowsuiatextprovider.cpp +++ b/src/plugins/platforms/windows/uiautomation/qwindowsuiatextprovider.cpp @@ -146,9 +146,10 @@ HRESULT STDMETHODCALLTYPE QWindowsUiaTextProvider::RangeFromPoint(UiaPoint point nativeUiaPointToPoint(point, window, &pt); int offset = textInterface->offsetAtPoint(pt); - if ((offset >= 0) && (offset < textInterface->characterCount())) { - *pRetVal = new QWindowsUiaTextRangeProvider(id(), offset, offset); - } + if (offset < 0 || offset >= textInterface->characterCount()) + return UIA_E_ELEMENTNOTAVAILABLE; + + *pRetVal = new QWindowsUiaTextRangeProvider(id(), offset, offset); return S_OK; }