fix: UB by erroneous event downcasting

When an event type is `QEvent::Wheel`, the event type is `QWheelEvent`,
which is not a `QMouseEvent`. This caused a undefined behavior that can
be cached by ubsan.
This commit is contained in:
Edoardo Morandi
2023-04-16 00:16:52 +02:00
parent 89ebbfebcd
commit e2e1a51790

View File

@@ -936,8 +936,7 @@ bool QuarterWidget::viewportEvent(QEvent* event)
// additionally handle it by this viewer. This is e.g. needed when
// resizing a widget item because the cursor may already be outside
// this widget.
if (event->type() == QEvent::Wheel ||
event->type() == QEvent::MouseButtonDblClick ||
if (event->type() == QEvent::MouseButtonDblClick ||
event->type() == QEvent::MouseButtonPress) {
QMouseEvent* mouse = static_cast<QMouseEvent*>(event);
QGraphicsItem *item = itemAt(mouse->pos());
@@ -954,6 +953,19 @@ bool QuarterWidget::viewportEvent(QEvent* event)
return false;
}
}
else if (event->type() == QEvent::Wheel) {
auto wheel = static_cast<QWheelEvent*>(event);
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
QPoint pos = wheel->pos();
#else
QPoint pos = wheel->position().toPoint();
#endif
QGraphicsItem* item = itemAt(pos);
if (!item) {
QGraphicsView::viewportEvent(event);
return false;
}
}
return QGraphicsView::viewportEvent(event);
}