From bb35230991453e65d1932a4aa47cf96f14970dfc Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 2 Feb 2023 09:05:09 +0100 Subject: [PATCH] Q_APPLICATION_STATIC: add missing std::launder() Unlike Q_GLOBAL_STATIC, a Q_APPLICATION_STATIC can be destroyed and re-created multiple times (if QCoreApplication is, too). This means we're basically in the std::optional case and require std::launder() to avoid UB when the payload type has either of the following: - a const member - a reference member - a vtable and different derived classes might be occupying the same space at different times While we can probably exclude the latter, because we always construct the same type, PlainType, in the storage, the first two can easily happen. Fix by adding the missing std::launder(). As a drive-by, remove the uneeded casting (and now launder()ing) of &storage when passing to innerFunction(), which anyway takes its argument as void*. Amends 81a31beeb25eaf14d5c5f42fe26aa49d6ef29bf8, which changed Q_APPLICATION_STATIC to use inline aligned_union storage instead of the original implementation's unique_ptr. Change-Id: I065bd33812a40195109bf11d5bc79975f2f87cd5 Reviewed-by: Fabian Kosmale Reviewed-by: Thiago Macieira (cherry picked from commit c53fdcb5ee15c309ba68717fa6564dc1ff4b8618) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/kernel/qapplicationstatic.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/corelib/kernel/qapplicationstatic.h b/src/corelib/kernel/qapplicationstatic.h index f311da5d77e..2f2cab9174e 100644 --- a/src/corelib/kernel/qapplicationstatic.h +++ b/src/corelib/kernel/qapplicationstatic.h @@ -9,6 +9,8 @@ #include #include +#include + QT_BEGIN_NAMESPACE namespace QtGlobalStatic { @@ -36,7 +38,7 @@ template struct ApplicationHolder static PlainType *realPointer() { - return reinterpret_cast(&storage); + return std::launder(reinterpret_cast(&storage)); } // called from QGlobalStatic::instance() @@ -46,7 +48,7 @@ template struct ApplicationHolder return realPointer(); QMutexLocker locker(&mutex); if (guard.loadRelaxed() == QtGlobalStatic::Uninitialized) { - QAS::innerFunction(realPointer()); + QAS::innerFunction(&storage); QObject::connect(QCoreApplication::instance(), &QObject::destroyed, reset); guard.storeRelaxed(QtGlobalStatic::Initialized); }