summaryrefslogtreecommitdiffstats
path: root/src/gui/kernel/qguiapplication.cpp
diff options
context:
space:
mode:
authorShawn Rutledge <shawn.rutledge@qt.io>2022-02-01 07:56:23 +0100
committerVolker Hilsheimer <volker.hilsheimer@qt.io>2022-02-22 14:48:38 +0100
commitc5792dcfd631abb4f9e2b92cd6e88d7e5c373406 (patch)
tree83d79ff6b099fc211ba9267bf2d585a5b2044564 /src/gui/kernel/qguiapplication.cpp
parent29a7489769bca59630782061789561fb07d77892 (diff)
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 <marc.mutz@qt.io> Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Diffstat (limited to 'src/gui/kernel/qguiapplication.cpp')
-rw-r--r--src/gui/kernel/qguiapplication.cpp13
1 files changed, 11 insertions, 2 deletions
diff --git a/src/gui/kernel/qguiapplication.cpp b/src/gui/kernel/qguiapplication.cpp
index 7996883d06..da71580a73 100644
--- a/src/gui/kernel/qguiapplication.cpp
+++ b/src/gui/kernel/qguiapplication.cpp
@@ -130,6 +130,7 @@
#include <qtgui_tracepoints_p.h>
#include <ctype.h>
+#include <limits>
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<int>::max(), std::numeric_limits<int>::max());
+ return thePoint.toPoint();
+}
+
void QGuiApplicationPrivate::notifyThemeChanged()
{
updatePalette();