From e57d7dbfa6d036f5f5b3014936900b93a60d2cfc Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 19 Jul 2022 12:58:36 -0700 Subject: [PATCH] QVariant: add missing const to QMetaTypeInterface pointers They're ALWAYS const objects, though they also chock full of relocations so they are never in read-only sections of memory (except maybe in final executables that are position-dependent). These are methods in a private sub-class of QVariant. No one outside of QtCore (at least qtbase) should be using them directly. QVariant doesn't have many friends (a bit anti-social); the one that matters is qvariant_cast and that one does access QVariant::Private. This is not a BC problem because QVariant::Private::type()'s signature is not changing. In any case, QVariant's Q_CORE_EXPORT does not apply to QVariant::Private anyway (see [1]). [1] https://msvc.godbolt.org/z/r9cer8eWh Change-Id: I3859764fed084846bcb0fffd17035355f823dc8f Reviewed-by: Fabian Kosmale (cherry picked from commit eb9ace1cee4bb48005797b0b2d2d3d576a4cb4ce) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/kernel/qvariant.cpp | 11 ++++------- src/corelib/kernel/qvariant.h | 10 +++++----- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp index ca724dbfac0..a2711a48f85 100644 --- a/src/corelib/kernel/qvariant.cpp +++ b/src/corelib/kernel/qvariant.cpp @@ -215,7 +215,7 @@ static qreal qConvertToRealNumber(const QVariant::Private *d, bool *ok) // the type of d has already been set, but other field are not set static void customConstruct(QVariant::Private *d, const void *copy) { - QtPrivate::QMetaTypeInterface *iface = d->typeInterface(); + const QtPrivate::QMetaTypeInterface *iface = d->typeInterface(); if (!(iface && iface->size)) { *d = QVariant::Private(); return; @@ -498,11 +498,8 @@ QVariant::QVariant(const QVariant &p) { if (d.is_shared) { d.data.shared->ref.ref(); - return; - } - QtPrivate::QMetaTypeInterface *iface = d.typeInterface(); - auto other = p.constData(); - if (iface) { + } else if (const QtPrivate::QMetaTypeInterface *iface = d.typeInterface()) { + auto other = p.constData(); if (other) iface->copyCtr(iface, &d.data, other); else @@ -993,7 +990,7 @@ QVariant &QVariant::operator=(const QVariant &variant) d = variant.d; } else { d = variant.d; - QtPrivate::QMetaTypeInterface *iface = d.typeInterface(); + const QtPrivate::QMetaTypeInterface *iface = d.typeInterface(); const void *other = variant.constData(); if (iface) { if (other) diff --git a/src/corelib/kernel/qvariant.h b/src/corelib/kernel/qvariant.h index 2a9b92f0c8c..dc73f46af51 100644 --- a/src/corelib/kernel/qvariant.h +++ b/src/corelib/kernel/qvariant.h @@ -428,7 +428,7 @@ public: static constexpr size_t MaxInternalSize = 3*sizeof(void *); template static constexpr bool CanUseInternalSpace = (QTypeInfo::isRelocatable && sizeof(T) <= MaxInternalSize && alignof(T) <= alignof(double)); - static constexpr bool canUseInternalSpace(QtPrivate::QMetaTypeInterface *type) + static constexpr bool canUseInternalSpace(const QtPrivate::QMetaTypeInterface *type) { Q_ASSERT(type); return QMetaType::TypeFlags(type->flags) & QMetaType::RelocatableType && @@ -468,14 +468,14 @@ public: void set(const T &t) { *static_cast(CanUseInternalSpace ? &data.data : data.shared->data()) = t; } - inline QMetaType type() const + inline const QtPrivate::QMetaTypeInterface* typeInterface() const { - return QMetaType(reinterpret_cast(packedType << 2)); + return reinterpret_cast(packedType << 2); } - inline QtPrivate::QMetaTypeInterface * typeInterface() const + inline QMetaType type() const { - return reinterpret_cast(packedType << 2); + return QMetaType(typeInterface()); } inline int typeId() const