From c5792dcfd631abb4f9e2b92cd6e88d7e5c373406 Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Tue, 1 Feb 2022 07:56:23 +0100 Subject: [PATCH] Make QGuiApplicationPrivate::lastCursorPosition.toPoint() safe to use QGuiApplicationPrivate::lastCursorPosition is initialized with qInf(); so before Qt has seen a mouse move event, attempting to convert to QPoint is an error. It's best to have one place where we do the qIsInf() check rather than several (and otherwise prefer using the QPointF as-is rather than converting to QPoint at all). Introduce a helper class that contains a QPointF, and provides a safe conversion to QPoint, as well as simple accessors for clients using QPointF. Fixes: QTBUG-52472 Task-number: QTBUG-45045 Change-Id: I83fad1bfb658e03fa876344552f1d5bb751d9f81 Pick-to: 6.2 6.3 Reviewed-by: Marc Mutz Reviewed-by: Shawn Rutledge --- src/gui/kernel/qguiapplication.cpp | 13 +++++++++++-- src/gui/kernel/qguiapplication_p.h | 15 ++++++++++++++- src/widgets/kernel/qwidgetwindow.cpp | 2 +- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/gui/kernel/qguiapplication.cpp b/src/gui/kernel/qguiapplication.cpp index 7996883d061..da71580a739 100644 --- a/src/gui/kernel/qguiapplication.cpp +++ b/src/gui/kernel/qguiapplication.cpp @@ -130,6 +130,7 @@ #include #include +#include QT_BEGIN_NAMESPACE @@ -147,7 +148,7 @@ Q_GUI_EXPORT bool qt_is_tty_app = false; Qt::MouseButtons QGuiApplicationPrivate::mouse_buttons = Qt::NoButton; Qt::KeyboardModifiers QGuiApplicationPrivate::modifier_buttons = Qt::NoModifier; -QPointF QGuiApplicationPrivate::lastCursorPosition(qt_inf(), qt_inf()); +QGuiApplicationPrivate::QLastCursorPosition QGuiApplicationPrivate::lastCursorPosition; QWindow *QGuiApplicationPrivate::currentMouseWindow = nullptr; @@ -711,7 +712,7 @@ QGuiApplication::~QGuiApplication() QGuiApplicationPrivate::desktopFileName = nullptr; QGuiApplicationPrivate::mouse_buttons = Qt::NoButton; QGuiApplicationPrivate::modifier_buttons = Qt::NoModifier; - QGuiApplicationPrivate::lastCursorPosition = {qreal(qInf()), qreal(qInf())}; + QGuiApplicationPrivate::lastCursorPosition.reset(); QGuiApplicationPrivate::currentMousePressWindow = QGuiApplicationPrivate::currentMouseWindow = nullptr; QGuiApplicationPrivate::applicationState = Qt::ApplicationInactive; QGuiApplicationPrivate::currentDragWindow = nullptr; @@ -4144,6 +4145,14 @@ QPixmap QGuiApplicationPrivate::getPixmapCursor(Qt::CursorShape cshape) return QPixmap(); } +QPoint QGuiApplicationPrivate::QLastCursorPosition::toPoint() const noexcept +{ + // Guard against the default initialization of qInf() (avoid UB or SIGFPE in conversion). + if (Q_UNLIKELY(qIsInf(thePoint.x()))) + return QPoint(std::numeric_limits::max(), std::numeric_limits::max()); + return thePoint.toPoint(); +} + void QGuiApplicationPrivate::notifyThemeChanged() { updatePalette(); diff --git a/src/gui/kernel/qguiapplication_p.h b/src/gui/kernel/qguiapplication_p.h index 6bb973ee753..3100b408efd 100644 --- a/src/gui/kernel/qguiapplication_p.h +++ b/src/gui/kernel/qguiapplication_p.h @@ -60,6 +60,7 @@ #include #include +#include #include #include @@ -227,7 +228,19 @@ public: virtual bool closeAllPopups() { return false; } static Qt::MouseButton mousePressButton; - static QPointF lastCursorPosition; + static struct QLastCursorPosition { + constexpr inline QLastCursorPosition() noexcept : thePoint(qt_inf(), qt_inf()) {} + constexpr inline Q_IMPLICIT QLastCursorPosition(QPointF p) noexcept : thePoint(p) {} + constexpr inline Q_IMPLICIT operator QPointF() const noexcept { return thePoint; } + constexpr inline qreal x() const noexcept{ return thePoint.x(); } + constexpr inline qreal y() const noexcept{ return thePoint.y(); } + Q_GUI_EXPORT QPoint toPoint() const noexcept; + + constexpr void reset() noexcept { *this = QLastCursorPosition{}; } + + private: + QPointF thePoint; + } lastCursorPosition; static QWindow *currentMouseWindow; static QWindow *currentMousePressWindow; static Qt::ApplicationState applicationState; diff --git a/src/widgets/kernel/qwidgetwindow.cpp b/src/widgets/kernel/qwidgetwindow.cpp index 14a2655e35b..88818ce9c52 100644 --- a/src/widgets/kernel/qwidgetwindow.cpp +++ b/src/widgets/kernel/qwidgetwindow.cpp @@ -402,7 +402,7 @@ void QWidgetWindow::handleEnterLeaveEvent(QEvent *event) QWindowSystemInterfacePrivate::EnterEvent *systemEvent = static_cast (QWindowSystemInterfacePrivate::peekWindowSystemEvent(QWindowSystemInterfacePrivate::Enter)); - const QPointF globalPosF = systemEvent ? systemEvent->globalPos : QGuiApplicationPrivate::lastCursorPosition; + const QPointF globalPosF = systemEvent ? systemEvent->globalPos : QPointF(QGuiApplicationPrivate::lastCursorPosition); if (systemEvent) { if (QWidgetWindow *enterWindow = qobject_cast(systemEvent->enter)) {