Fix ubsan warning of illegal cast and illegal method call

Avoid casting an event to a type it does not have. Instead use a static accessor class.

Task-number: QTBUG-99563
Change-Id: Ideb11779b1510cd10a27fb8bc40bcc8e4849bf15
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
(cherry picked from commit acc3ef6653c710b509e9321663986910f88ac3b4)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Allan Sandfeld Jensen 2024-11-12 12:34:15 +01:00 committed by Qt Cherry-pick Bot
parent 3b541daff7
commit b52a0f8e10
8 changed files with 37 additions and 15 deletions

View File

@ -4804,19 +4804,27 @@ QMutableTouchEvent::~QMutableTouchEvent()
/*! \internal
Add the given \a point.
*/
void QMutableTouchEvent::addPoint(const QEventPoint &point)
void QMutableTouchEvent::addPoint(QTouchEvent *e, const QEventPoint &point)
{
m_points.append(point);
auto &added = m_points.last();
e->m_points.append(point);
auto &added = e->m_points.last();
if (!added.device())
QMutableEventPoint::setDevice(added, pointingDevice());
m_touchPointStates |= point.state();
QMutableEventPoint::setDevice(added, e->pointingDevice());
e->m_touchPointStates |= point.state();
}
QMutableSinglePointEvent::~QMutableSinglePointEvent()
= default;
/*! \internal
Add the given \a point.
*/
void QMutableTouchEvent::addPoint(const QEventPoint &point)
{
addPoint(this, point);
}
QT_END_NAMESPACE
#include "moc_qevent.cpp"

View File

@ -134,6 +134,7 @@ public:
protected:
friend class ::tst_QEvent;
friend class QMutableSinglePointEvent;
QSinglePointEvent(Type type, const QPointingDevice *dev, const QEventPoint &point,
Qt::MouseButton button, Qt::MouseButtons buttons,
Qt::KeyboardModifiers modifiers, Qt::MouseEventSource source);
@ -943,6 +944,7 @@ public:
bool isEndEvent() const override;
protected:
friend class QMutableTouchEvent;
QObject *m_target = nullptr;
QEventPoint::States m_touchPointStates = QEventPoint::State::Unknown;
quint32 m_reserved : 24;

View File

@ -39,8 +39,10 @@ public:
static QMutableTouchEvent &from(QTouchEvent &e) { return static_cast<QMutableTouchEvent &>(e); }
void setTarget(QObject *target) { m_target = target; }
void addPoint(const QEventPoint &point);
static void setTarget(QTouchEvent *e, QObject *target) { e->m_target = target; }
static void addPoint(QTouchEvent *e, const QEventPoint &point);
};
class Q_GUI_EXPORT QMutableSinglePointEvent : public QSinglePointEvent
@ -63,6 +65,16 @@ public:
bool isDoubleClick() { return m_doubleClick; }
void setDoubleClick(bool d = true) { m_doubleClick = d; }
static bool isDoubleClick(const QSinglePointEvent *ev)
{
return ev->m_doubleClick;
}
static void setDoubleClick(QSinglePointEvent *ev, bool d)
{
ev->m_doubleClick = d;
}
};
QT_END_NAMESPACE

View File

@ -2455,7 +2455,7 @@ void QGuiApplicationPrivate::processMouseEvent(QWindowSystemInterfacePrivate::Mo
if (doubleClick && (ev.type() == QEvent::MouseButtonPress)) {
// QtBUG-25831, used to suppress delivery in qwidgetwindow.cpp
QMutableSinglePointEvent::from(ev).setDoubleClick();
QMutableSinglePointEvent::setDoubleClick(&ev, true);
}
QGuiApplication::sendSpontaneousEvent(window, &ev);

View File

@ -407,7 +407,7 @@ void QPointingDevicePrivate::sendTouchCancelEvent(QTouchEvent *cancelEvent)
if (cancelEvent->points().isEmpty()) {
for (auto &epd : activePoints.values()) {
if (epd.exclusiveGrabber)
QMutableTouchEvent::from(cancelEvent)->addPoint(epd.eventPoint);
QMutableTouchEvent::addPoint(cancelEvent, epd.eventPoint);
}
}
for (auto &epd : activePoints.values()) {

View File

@ -2902,7 +2902,7 @@ bool QGraphicsView::viewportEvent(QEvent *event)
if (d->scene && d->sceneInteractionAllowed) {
// Convert and deliver the touch event to the scene.
QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event);
QMutableTouchEvent::from(touchEvent)->setTarget(viewport());
QMutableTouchEvent::setTarget(touchEvent, viewport());
QGraphicsViewPrivate::translateTouchEvent(d, touchEvent);
QCoreApplication::sendEvent(d->scene, touchEvent);
} else {

View File

@ -2762,7 +2762,7 @@ bool QApplication::notify(QObject *receiver, QEvent *e)
mouse->pointingDevice());
me.m_spont = mouse->spontaneous();
me.setTimestamp(mouse->timestamp());
QMutableSinglePointEvent::from(me).setDoubleClick(QMutableSinglePointEvent::from(mouse)->isDoubleClick());
QMutableSinglePointEvent::setDoubleClick(&me, QMutableSinglePointEvent::isDoubleClick(mouse));
// throw away any mouse-tracking-only mouse events
if (!w->hasMouseTracking()
&& mouse->type() == QEvent::MouseMove && mouse->buttons() == 0) {
@ -3067,7 +3067,7 @@ bool QApplication::notify(QObject *receiver, QEvent *e)
#endif // QT_CONFIG(draganddrop)
case QEvent::TouchBegin: {
// Note: TouchUpdate and TouchEnd events are never propagated
QMutableTouchEvent *touchEvent = QMutableTouchEvent::from(static_cast<QTouchEvent *>(e));
QTouchEvent *touchEvent = static_cast<QTouchEvent *>(e);
bool eventAccepted = touchEvent->isAccepted();
bool acceptTouchEvents = w->testAttribute(Qt::WA_AcceptTouchEvents);
@ -3084,7 +3084,7 @@ bool QApplication::notify(QObject *receiver, QEvent *e)
while (w) {
// first, try to deliver the touch event
acceptTouchEvents = w->testAttribute(Qt::WA_AcceptTouchEvents);
touchEvent->setTarget(w);
QMutableTouchEvent::setTarget(touchEvent, w);
touchEvent->setAccepted(acceptTouchEvents);
QPointer<QWidget> p = w;
res = acceptTouchEvents && d->notify_helper(w, touchEvent);
@ -3110,7 +3110,7 @@ bool QApplication::notify(QObject *receiver, QEvent *e)
const QPoint offset = w->pos();
w = w->parentWidget();
touchEvent->setTarget(w);
QMutableTouchEvent::setTarget(touchEvent, w);
for (int i = 0; i < touchEvent->pointCount(); ++i) {
auto &pt = touchEvent->point(i);
QMutableEventPoint::setPosition(pt, pt.position() + offset);

View File

@ -564,7 +564,7 @@ void QWidgetWindow::handleMouseEvent(QMouseEvent *event)
}
}
if ((event->type() != QEvent::MouseButtonPress) || !(QMutableSinglePointEvent::from(event)->isDoubleClick())) {
if ((event->type() != QEvent::MouseButtonPress) || !(QMutableSinglePointEvent::isDoubleClick(event))) {
// if the widget that was pressed is gone, then deliver move events without buttons
const auto buttons = event->type() == QEvent::MouseMove && qt_popup_down_closed
? Qt::NoButton : event->buttons();
@ -656,7 +656,7 @@ void QWidgetWindow::handleMouseEvent(QMouseEvent *event)
mapped = event->position().toPoint();
}
if ((event->type() != QEvent::MouseButtonPress) || !QMutableSinglePointEvent::from(event)->isDoubleClick()) {
if ((event->type() != QEvent::MouseButtonPress) || !QMutableSinglePointEvent::isDoubleClick(event)) {
// The preceding statement excludes MouseButtonPress events which caused
// creation of a MouseButtonDblClick event. QTBUG-25831