diff options
author | Marc Mutz <marc.mutz@qt.io> | 2022-01-04 19:43:49 +0100 |
---|---|---|
committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2022-01-14 18:57:41 +0100 |
commit | efe4863f837834c1664e37bf439bdec399f1dc88 (patch) | |
tree | 81fd4cc37ddb561ef40eb065e55874b36d635211 /src/gui/kernel/qwindow.cpp | |
parent | 7955dceec1fe093a1d29de58590e08be4e68ccfa (diff) |
QWindow: fix UB (invalid static_cast)
Do the cast to QMouseEvent only after we determined that it's actually
a QMouseEvent. Says ubsan:
src/gui/kernel/qwindow.cpp:2558:27: runtime error: downcast of address 0x7fffca0e5af0 which does not point to an object of type 'QMouseEvent'
0x7fffca0e5af0: note: object is of type 'QShowEvent'
ff 7f 00 00 b0 09 01 b8 61 7f 00 00 11 00 00 00 00 00 00 00 b3 8a b5 41 00 00 00 00 80 50 5a cc
^~~~~~~~~~~~~~~~~~~~~~~
vptr for 'QShowEvent'
Only cast _after_ determining that `ev` is-a mouse event.
Amends 3d71c4b740d23d5c3f380f495990f35ea17dc2a0.
Pick-to: 6.3 6.2
Change-Id: I8455c998e2f4390a1483c1a097eb095358963ace
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/gui/kernel/qwindow.cpp')
-rw-r--r-- | src/gui/kernel/qwindow.cpp | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/src/gui/kernel/qwindow.cpp b/src/gui/kernel/qwindow.cpp index 8070530033..dc55c3c787 100644 --- a/src/gui/kernel/qwindow.cpp +++ b/src/gui/kernel/qwindow.cpp @@ -2559,7 +2559,12 @@ bool QWindow::event(QEvent *ev) static const QEvent::Type contextMenuTrigger = QGuiApplicationPrivate::platformTheme()->themeHint(QPlatformTheme::ContextMenuOnMouseRelease).toBool() ? QEvent::MouseButtonRelease : QEvent::MouseButtonPress; - if (QMouseEvent *me = static_cast<QMouseEvent *>(ev); + auto asMouseEvent = [](QEvent *ev) { + const auto t = ev->type(); + return t == QEvent::MouseButtonPress || t == QEvent::MouseButtonRelease + ? static_cast<QMouseEvent *>(ev) : nullptr ; + }; + if (QMouseEvent *me = asMouseEvent(ev); me && ev->type() == contextMenuTrigger && me->button() == Qt::RightButton) { QSinglePointEvent *pev = static_cast<QSinglePointEvent*>(ev); QContextMenuEvent e(QContextMenuEvent::Mouse, me->position().toPoint(), |