From 23aa51991dee2864d7f1d1acee953fd5c7b409fe Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 4 Jan 2022 16:55:03 +0100 Subject: [PATCH] QTzTimeZonePrivate: fix permanently-detaching m_icu The m_icu member is a mutable QSharedDataPointer, which means that only the non-const API subset is accessible, and so any access to it will always detach, in 5.15 even a check like if (m_icu), which the code luckily doesn't use (Qt 6 added a operator bool() const). We don't need detaching behavior here, though, since, once set, m_icu is never changed. So just use a QExplicitlySharedDataPointer instead, and never call detach() (which would do the wrong thing). Just in case someone does add a detach() later, instantiate QExplicitlySharedDataPointer over QIcuTimeZonePrivate directly. This requires making displayName() overloads from QTimeZonePrivate visible in QIcuTimeZonePrivate. Add an assertion that QIcuTimeZonePrivate is final, with instructions on what to do if it fails. Finally, hold a pointer-to-const to avoid race conditions in the pointee. The code still contains a data race, due to the lazy initialization of m_icu, but now we have at least a fighting change to fix it. Pick-to: 6.3 6.2 5.15 Change-Id: I32c343822dac43f96d9fbc4c759fa44138861eae Reviewed-by: Thiago Macieira --- src/corelib/time/qtimezoneprivate_p.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/corelib/time/qtimezoneprivate_p.h b/src/corelib/time/qtimezoneprivate_p.h index 129f0343b3..b37d9995e3 100644 --- a/src/corelib/time/qtimezoneprivate_p.h +++ b/src/corelib/time/qtimezoneprivate_p.h @@ -243,6 +243,7 @@ public: QIcuTimeZonePrivate *clone() const override; + using QTimeZonePrivate::displayName; QString displayName(QTimeZone::TimeType timeType, QTimeZone::NameType nameType, const QLocale &locale) const override; QString abbreviation(qint64 atMSecsSinceEpoch) const override; @@ -353,7 +354,12 @@ private: Data dataForTzTransition(QTzTransitionTime tran) const; Data dataFromRule(QTzTransitionRule rule, qint64 msecsSinceEpoch) const; #if QT_CONFIG(icu) - mutable QSharedDataPointer m_icu; +# ifdef __cpp_lib_is_final + static_assert(std::is_final::value, + "if QIcuTimeZonePrivate isn't final, we may need to specialize " + "QExplicitlySharedDataPointer::clone() to call QTimeZonePrivate::clone()"); +# endif + mutable QExplicitlySharedDataPointer m_icu; #endif QTzTimeZoneCacheEntry cached_data; QList tranCache() const { return cached_data.m_tranTimes; }