From d8fd2425fb37b5f745e385552b11dc4cdcffb5ff Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Thu, 16 Sep 2021 23:18:10 +0200 Subject: [PATCH] QCoreApplication: enforce non-null arguments when sending/posting events Passing nullptr as receiver and/or as an event parameter to sendEvent, postEvent, etc. is meaningless. It's also something that users can check for. Therefore, it should not be allowed. Note that the current code already relies on the arguments not to be null, albeit "indirectly" (e.g. they get dereferenced without any null checks). Hence: add asserts that check for non-null in all the relevant codepaths, except for the ones in which there's currently just a warning; for those, add a Qt 7 note. Change-Id: Ia4c58551de88a5d1003f09efa448c1330b6cb122 Reviewed-by: Thiago Macieira Reviewed-by: Qt CI Bot Reviewed-by: Volker Hilsheimer Reviewed-by: Edward Welbourne --- src/corelib/kernel/qcoreapplication.cpp | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp index 3bce669ba10..9a40e987fa7 100644 --- a/src/corelib/kernel/qcoreapplication.cpp +++ b/src/corelib/kernel/qcoreapplication.cpp @@ -1133,6 +1133,9 @@ bool QCoreApplication::forwardEvent(QObject *receiver, QEvent *event, QEvent *or bool QCoreApplication::notify(QObject *receiver, QEvent *event) { + Q_ASSERT(receiver); + Q_ASSERT(event); + // no events are delivered after ~QCoreApplication() has started if (QCoreApplicationPrivate::is_app_closing) return true; @@ -1141,6 +1144,9 @@ bool QCoreApplication::notify(QObject *receiver, QEvent *event) static bool doNotify(QObject *receiver, QEvent *event) { + Q_ASSERT(event); + + // ### Qt 7: turn into an assert if (receiver == nullptr) { // serious error qWarning("QCoreApplication::notify: Unexpected null receiver"); return true; @@ -1464,10 +1470,12 @@ void QCoreApplication::exit(int returnCode) */ bool QCoreApplication::sendEvent(QObject *receiver, QEvent *event) { + Q_ASSERT_X(receiver, "QCoreApplication::sendEvent", "Unexpected null receiver"); + Q_ASSERT_X(event, "QCoreApplication::sendEvent", "Unexpected null event"); + Q_TRACE(QCoreApplication_sendEvent, receiver, event, event->type()); - if (event) - event->m_spont = false; + event->m_spont = false; return notifyInternal2(receiver, event); } @@ -1476,10 +1484,12 @@ bool QCoreApplication::sendEvent(QObject *receiver, QEvent *event) */ bool QCoreApplication::sendSpontaneousEvent(QObject *receiver, QEvent *event) { + Q_ASSERT_X(receiver, "QCoreApplication::sendSpontaneousEvent", "Unexpected null receiver"); + Q_ASSERT_X(event, "QCoreApplication::sendSpontaneousEvent", "Unexpected null event"); + Q_TRACE(QCoreApplication_sendSpontaneousEvent, receiver, event, event->type()); - if (event) - event->m_spont = true; + event->m_spont = true; return notifyInternal2(receiver, event); } @@ -1544,8 +1554,11 @@ QCoreApplicationPrivate::QPostEventListLocker QCoreApplicationPrivate::lockThrea */ void QCoreApplication::postEvent(QObject *receiver, QEvent *event, int priority) { + Q_ASSERT_X(event, "QCoreApplication::postEvent", "Unexpected null event"); + Q_TRACE_SCOPE(QCoreApplication_postEvent, receiver, event, event->type()); + // ### Qt 7: turn into an assert if (receiver == nullptr) { qWarning("QCoreApplication::postEvent: Unexpected null receiver"); delete event; @@ -1615,11 +1628,11 @@ void QCoreApplication::postEvent(QObject *receiver, QEvent *event, int priority) */ bool QCoreApplication::compressEvent(QEvent *event, QObject *receiver, QPostEventList *postedEvents) { -#ifdef Q_OS_WIN Q_ASSERT(event); Q_ASSERT(receiver); Q_ASSERT(postedEvents); +#ifdef Q_OS_WIN // compress posted timers to this object. if (event->type() == QEvent::Timer && receiver->d_func()->postedEvents > 0) { int timerId = ((QTimerEvent *) event)->timerId();