Fix crashes when invoking toVariant() on empty QJsonValue objects.

Change-Id: I51cd114e862c6fad564484e990348f324ad56ab9
Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@digia.com>
This commit is contained in:
Friedemann Kleint 2013-07-23 15:42:55 +02:00 committed by The Qt Project
parent 0edf1390ca
commit 77833b90c8
2 changed files with 10 additions and 2 deletions

View File

@ -384,9 +384,13 @@ QVariant QJsonValue::toVariant() const
case String:
return toString();
case Array:
return QJsonArray(d, static_cast<QJsonPrivate::Array *>(base)).toVariantList();
return d ?
QJsonArray(d, static_cast<QJsonPrivate::Array *>(base)).toVariantList() :
QVariantList();
case Object:
return QJsonObject(d, static_cast<QJsonPrivate::Object *>(base)).toVariantMap();
return d ?
QJsonObject(d, static_cast<QJsonPrivate::Object *>(base)).toVariantMap() :
QVariantMap();
case Null:
case Undefined:
break;

View File

@ -1062,6 +1062,8 @@ void tst_QtJson::fromVariantMap()
void tst_QtJson::toVariantMap()
{
QCOMPARE(QMetaType::Type(QJsonValue(QJsonObject()).toVariant().type()), QMetaType::QVariantMap); // QTBUG-32524
QJsonObject object;
QVariantMap map = object.toVariantMap();
QVERIFY(map.isEmpty());
@ -1091,6 +1093,8 @@ void tst_QtJson::toVariantMap()
void tst_QtJson::toVariantList()
{
QCOMPARE(QMetaType::Type(QJsonValue(QJsonArray()).toVariant().type()), QMetaType::QVariantList); // QTBUG-32524
QJsonArray array;
QVariantList list = array.toVariantList();
QVERIFY(list.isEmpty());