diff options
author | Fabian Kosmale <fabian.kosmale@qt.io> | 2022-07-18 09:17:35 +0200 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2022-07-21 08:37:34 -0700 |
commit | 6a0e59ed506ecb7d6a1bc17beedd938d36a1d26f (patch) | |
tree | d65cb75892227f468d474b666a34f5a36ca7e162 /src/corelib/kernel/qvariant.cpp | |
parent | 030923c0def8b4c08fef2e1b865f30dd7287a907 (diff) |
QVariant: Avoid crash when constructed from unsuitable metatype
If the metatype does not support copy and default construction, then it
it unsuitunsuitable for use in QMetaType. We cannot prevent users from
passing in such metatypes (as we have e.g. a ctor taking QMetaType), so
verify this in customConstruct, and make the variant invalid in that
case.
Pick-to: 6.2 6.3 6.4
Change-Id: Ib1f0149c8fb9a1cce0049fd0311980754cc85d1b
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Diffstat (limited to 'src/corelib/kernel/qvariant.cpp')
-rw-r--r-- | src/corelib/kernel/qvariant.cpp | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp index 472c510358..a2711a48f8 100644 --- a/src/corelib/kernel/qvariant.cpp +++ b/src/corelib/kernel/qvariant.cpp @@ -220,11 +220,15 @@ static void customConstruct(QVariant::Private *d, const void *copy) *d = QVariant::Private(); return; } + if (!(iface->copyCtr && iface->defaultCtr)) { + // QVariant requires type to be copy and default constructible + *d = QVariant::Private(); + qWarning("QVariant: Provided metatype does not support " + "destruction, copy and default construction"); + return; + } if (QVariant::Private::canUseInternalSpace(iface)) { - // QVariant requires type to be copy and default constructible - Q_ASSERT(iface->copyCtr); - Q_ASSERT(iface->defaultCtr); if (copy) iface->copyCtr(iface, &d->data, copy); else @@ -778,6 +782,9 @@ QVariant::QVariant(const QVariant &p) instead to construct variants from the pointer types represented by \c QMetaType::VoidStar, and \c QMetaType::QObjectStar. + If \a type does not support copy and default construction, the variant will + be invalid. + \sa QVariant::fromValue(), QMetaType::Type */ QVariant::QVariant(QMetaType type, const void *copy) : d(type) |