summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel/qvariant.cpp
diff options
context:
space:
mode:
authorAllan Sandfeld Jensen <allan.jensen@qt.io>2017-01-23 10:31:16 +0100
committerSimon Hausmann <simon.hausmann@qt.io>2017-08-04 11:19:25 +0000
commitf2c6e10ad59c0660b772bff7715fb232cdbe394c (patch)
tree59646dbc12f2ea11c89bcb6392b6bb78ba510dcc /src/corelib/kernel/qvariant.cpp
parent341bfcd1eaa9116c143e3b7d3219ef04c7b8a0cb (diff)
QVariants of null pointers should be null
Changes the QVariant::isNull() implementation for pointer types so they return true if null. [ChangeLog][QVariant] QVariants containing pointers will now return true on isNull() if the contained pointer is null. Change-Id: I8aa0dab482403837073fb2f376a46126cc3bc6b2 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/kernel/qvariant.cpp')
-rw-r--r--src/corelib/kernel/qvariant.cpp19
1 files changed, 15 insertions, 4 deletions
diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp
index b73c83bc49..a6b92f9f8f 100644
--- a/src/corelib/kernel/qvariant.cpp
+++ b/src/corelib/kernel/qvariant.cpp
@@ -1177,7 +1177,17 @@ static void customClear(QVariant::Private *d)
static bool customIsNull(const QVariant::Private *d)
{
- return d->is_null;
+ if (d->is_null)
+ return true;
+ const char *const typeName = QMetaType::typeName(d->type);
+ if (Q_UNLIKELY(!typeName) && Q_LIKELY(!QMetaType::isRegistered(d->type)))
+ qFatal("QVariant::isNull: type %d unknown to QVariant.", d->type);
+ uint typeNameLen = qstrlen(typeName);
+ if (typeNameLen > 0 && typeName[typeNameLen - 1] == '*') {
+ const void *d_ptr = d->is_shared ? d->data.shared->ptr : &(d->data.ptr);
+ return *static_cast<void *const *>(d_ptr) == nullptr;
+ }
+ return false;
}
static bool customCompare(const QVariant::Private *a, const QVariant::Private *b)
@@ -3740,9 +3750,10 @@ void* QVariant::data()
/*!
Returns \c true if this is a null variant, false otherwise. A variant is
- considered null if it contains no initialized value or it contains an instance
- of built-in type that has an isNull method, in which case the result would be
- the same as calling isNull on the wrapped object.
+ considered null if it contains no initialized value, or the contained value
+ is a null pointer or is an instance of a built-in type that has an isNull
+ method, in which case the result would be the same as calling isNull on the
+ wrapped object.
\warning Null variants is not a single state and two null variants may easily
return \c false on the == operator if they do not contain similar null values.