diff options
-rw-r--r-- | src/corelib/kernel/qvariant.cpp | 8 | ||||
-rw-r--r-- | tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp | 48 |
2 files changed, 53 insertions, 3 deletions
diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp index 5cf8636dcd..c2c39efea3 100644 --- a/src/corelib/kernel/qvariant.cpp +++ b/src/corelib/kernel/qvariant.cpp @@ -2341,9 +2341,11 @@ static bool numericEquals(const QVariant::Private *d1, const QVariant::Private * #ifndef QT_BOOTSTRAPPED static bool canConvertMetaObject(QMetaType fromType, QMetaType toType) { - if ((fromType.flags() & QMetaType::PointerToQObject) && (toType.flags() & QMetaType::PointerToQObject)) { - return fromType.metaObject()->inherits(toType.metaObject()) || - toType.metaObject()->inherits(fromType.metaObject()); + if ((fromType.flags() & QMetaType::PointerToQObject) + && (toType.flags() & QMetaType::PointerToQObject)) { + const QMetaObject *f = fromType.metaObject(); + const QMetaObject *t = toType.metaObject(); + return f && t && (f->inherits(t) || t->inherits(f)); } return false; } diff --git a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp index 600a99e72c..5ca08b4e62 100644 --- a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp +++ b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp @@ -63,6 +63,7 @@ #include "qnumeric.h" #include <private/qlocale_p.h> +#include <private/qmetatype_p.h> #include "tst_qvariant_common.h" #include <unordered_map> @@ -309,6 +310,7 @@ private slots: void mutableView(); void moveOperations(); + void equalsWithoutMetaObject(); private: void dataStream_data(QDataStream::Version version); @@ -5086,5 +5088,51 @@ void tst_QVariant::moveOperations() QVERIFY(v2.value<std::list<int>>() == list); } +class NoMetaObject : public QObject {}; +void tst_QVariant::equalsWithoutMetaObject() +{ + using T = NoMetaObject*; + QtPrivate::QMetaTypeInterface d = { + /*.revision=*/ 0, + /*.alignment=*/ alignof(T), + /*.size=*/ sizeof(T), + /*.flags=*/ QtPrivate::QMetaTypeTypeFlags<T>::Flags, + /*.typeId=*/ 0, + /*.metaObject=*/ nullptr, // on purpose. + /*.name=*/ "NoMetaObject*", + /*.defaultCtr=*/ [](const QtPrivate::QMetaTypeInterface *, void *addr) { + new (addr) T(); + }, + /*.copyCtr=*/ [](const QtPrivate::QMetaTypeInterface *, void *addr, const void *other) { + new (addr) T(*reinterpret_cast<const T *>(other)); + }, + /*.moveCtr=*/ [](const QtPrivate::QMetaTypeInterface *, void *addr, void *other) { + new (addr) T(std::move(*reinterpret_cast<T *>(other))); + }, + /*.dtor=*/ [](const QtPrivate::QMetaTypeInterface *, void *addr) { + reinterpret_cast<T *>(addr)->~T(); + }, + /*.equals*/ nullptr, + /*.lessThan*/ nullptr, + /*.debugStream=*/ nullptr, + /*.dataStreamOut=*/ nullptr, + /*.dataStreamIn=*/ nullptr, + /*.legacyRegisterOp=*/ nullptr + }; + + QMetaType noMetaObjectMetaType(&d); + QMetaType qobjectMetaType = QMetaType::fromType<tst_QVariant*>(); + + QVERIFY(noMetaObjectMetaType.flags() & QMetaType::PointerToQObject); + QVERIFY(qobjectMetaType.flags() & QMetaType::PointerToQObject); + + QVariant noMetaObjectVariant(noMetaObjectMetaType, nullptr); + QVariant qobjectVariant(qobjectMetaType, nullptr); + + // Shouldn't crash + QVERIFY(noMetaObjectVariant != qobjectVariant); + QVERIFY(qobjectVariant != noMetaObjectVariant); +} + QTEST_MAIN(tst_QVariant) #include "tst_qvariant.moc" |