summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel/qvariant.cpp
diff options
context:
space:
mode:
authorFabian Kosmale <fabian.kosmale@qt.io>2020-10-09 17:53:53 +0200
committerFabian Kosmale <fabian.kosmale@qt.io>2020-10-13 08:06:18 +0200
commitb83225fcc3c6aa5655884cbf9e7391afe38b4392 (patch)
treec509ef650db260053d4d599a775e0ea267fbacf4 /src/corelib/kernel/qvariant.cpp
parent9aa2be236f432dcf4e95a114aeb7254d78ca9dad (diff)
qDebug: Avoid implicit QVariant conversion
This commit restricts operator<<(QDebug lhs, QVariant rhs) to only work if rhs is actually of type QVariant (instead of any type convertible to QVariant). This is especially important as a) we check in QMetaType whether (slightly simplified) QDebug{} << std::declval<T>() is valid, and if so, register a function which simply uses the operator. b) In QVariant, we ask the metatype system for the contained types registered debug function and then use it. If a type now does not have its own operator<< for QDebug, but is implicitly convertible to QVariant containing itself, this would lead to an infinite recursion, when trying to use qDebug with that type. The registered function in a) would just convert the type to QVariant, and then ask the QVariant to print itself. Disallowing implicit conversions in qDebug in general was considered (i.e. adding template<typename T> operator<<(T) = delete in QDebug ), but discarded as it breaks too much code relying on conversions. Fixes: QTBUG-87122 Change-Id: Ib709297670cbc6cc307efd0dfd8e5b0279df9414 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/kernel/qvariant.cpp')
-rw-r--r--src/corelib/kernel/qvariant.cpp12
1 files changed, 6 insertions, 6 deletions
diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp
index ad52921cb7..05293afccc 100644
--- a/src/corelib/kernel/qvariant.cpp
+++ b/src/corelib/kernel/qvariant.cpp
@@ -2396,16 +2396,16 @@ bool QVariant::isNull() const
}
#ifndef QT_NO_DEBUG_STREAM
-QDebug operator<<(QDebug dbg, const QVariant &v)
+QDebug QVariant::qdebugHelper(QDebug dbg) const
{
QDebugStateSaver saver(dbg);
- const uint typeId = v.d.typeId();
+ const uint typeId = d.typeId();
dbg.nospace() << "QVariant(";
if (typeId != QMetaType::UnknownType) {
- dbg << v.d.type().name() << ", ";
- bool streamed = v.d.type().debugStream(dbg, v.d.storage());
- if (!streamed && v.canConvert<QString>())
- dbg << v.toString();
+ dbg << d.type().name() << ", ";
+ bool streamed = d.type().debugStream(dbg, d.storage());
+ if (!streamed && canConvert<QString>())
+ dbg << toString();
} else {
dbg << "Invalid";
}