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

Pick-to: 6.3 6.4
Change-Id: I3859764fed084846bcb0fffd17035355f823dc8f
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Thiago Macieira 2022-07-19 12:58:36 -07:00
parent 48675dfa33
commit eb9ace1cee
2 changed files with 9 additions and 12 deletions

View File

@ -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 // the type of d has already been set, but other field are not set
static void customConstruct(QVariant::Private *d, const void *copy) static void customConstruct(QVariant::Private *d, const void *copy)
{ {
QtPrivate::QMetaTypeInterface *iface = d->typeInterface(); const QtPrivate::QMetaTypeInterface *iface = d->typeInterface();
if (!(iface && iface->size)) { if (!(iface && iface->size)) {
*d = QVariant::Private(); *d = QVariant::Private();
return; return;
@ -494,11 +494,8 @@ QVariant::QVariant(const QVariant &p)
{ {
if (d.is_shared) { if (d.is_shared) {
d.data.shared->ref.ref(); d.data.shared->ref.ref();
return; } else if (const QtPrivate::QMetaTypeInterface *iface = d.typeInterface()) {
} auto other = p.constData();
QtPrivate::QMetaTypeInterface *iface = d.typeInterface();
auto other = p.constData();
if (iface) {
if (other) if (other)
iface->copyCtr(iface, &d.data, other); iface->copyCtr(iface, &d.data, other);
else else
@ -986,7 +983,7 @@ QVariant &QVariant::operator=(const QVariant &variant)
d = variant.d; d = variant.d;
} else { } else {
d = variant.d; d = variant.d;
QtPrivate::QMetaTypeInterface *iface = d.typeInterface(); const QtPrivate::QMetaTypeInterface *iface = d.typeInterface();
const void *other = variant.constData(); const void *other = variant.constData();
if (iface) { if (iface) {
if (other) if (other)

View File

@ -428,7 +428,7 @@ public:
static constexpr size_t MaxInternalSize = 3*sizeof(void *); static constexpr size_t MaxInternalSize = 3*sizeof(void *);
template<typename T> template<typename T>
static constexpr bool CanUseInternalSpace = (QTypeInfo<T>::isRelocatable && sizeof(T) <= MaxInternalSize && alignof(T) <= alignof(double)); static constexpr bool CanUseInternalSpace = (QTypeInfo<T>::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); Q_ASSERT(type);
return QMetaType::TypeFlags(type->flags) & QMetaType::RelocatableType && return QMetaType::TypeFlags(type->flags) & QMetaType::RelocatableType &&
@ -468,14 +468,14 @@ public:
void set(const T &t) void set(const T &t)
{ *static_cast<T *>(CanUseInternalSpace<T> ? &data.data : data.shared->data()) = t; } { *static_cast<T *>(CanUseInternalSpace<T> ? &data.data : data.shared->data()) = t; }
inline QMetaType type() const inline const QtPrivate::QMetaTypeInterface* typeInterface() const
{ {
return QMetaType(reinterpret_cast<QtPrivate::QMetaTypeInterface *>(packedType << 2)); return reinterpret_cast<const QtPrivate::QMetaTypeInterface *>(packedType << 2);
} }
inline QtPrivate::QMetaTypeInterface * typeInterface() const inline QMetaType type() const
{ {
return reinterpret_cast<QtPrivate::QMetaTypeInterface *>(packedType << 2); return QMetaType(typeInterface());
} }
inline int typeId() const inline int typeId() const