summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDenis Dzyubenko <denis.dzyubenko@nokia.com>2012-06-04 18:48:51 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2012-12-13 23:58:51 +0100
commit6b5b5d4e530eef3f5a7025f753d6f54e76ad008f (patch)
tree59d23254a80c9dbb12c3061c9172c6a4ca48380a
parent20d6a0bfbcd10c3954e675adbcbd22b524ba7f04 (diff)
Added missing detach() to QJsonObject::take()
We also need to detach() the taken value in case the compaction triggers and modifies the underlaying data. Change-Id: Idcdeba4236b8e208d107d41be2decbdfc5721300 Reviewed-by: Bjørn Erik Nilsen <post@bjoernen.com> Reviewed-by: Denis Dzyubenko <denis@ddenis.info>
-rw-r--r--src/corelib/json/qjsonobject.cpp5
-rw-r--r--tests/auto/corelib/json/tst_qtjson.cpp17
2 files changed, 19 insertions, 3 deletions
diff --git a/src/corelib/json/qjsonobject.cpp b/src/corelib/json/qjsonobject.cpp
index e595753fec..1bdd872e78 100644
--- a/src/corelib/json/qjsonobject.cpp
+++ b/src/corelib/json/qjsonobject.cpp
@@ -390,13 +390,14 @@ QJsonValue QJsonObject::take(const QString &key)
if (!keyExists)
return QJsonValue(QJsonValue::Undefined);
- QJsonPrivate::Entry *e = o->entryAt(index);
+ QJsonValue v(d, o, o->entryAt(index)->value);
+ detach();
o->removeItems(index, 1);
++d->compactionCounter;
if (d->compactionCounter > 32u && d->compactionCounter >= unsigned(o->length) / 2u)
compact();
- return QJsonValue(d, o, e->value);
+ return v;
}
/*!
diff --git a/tests/auto/corelib/json/tst_qtjson.cpp b/tests/auto/corelib/json/tst_qtjson.cpp
index 82656d6309..4a8ef293ff 100644
--- a/tests/auto/corelib/json/tst_qtjson.cpp
+++ b/tests/auto/corelib/json/tst_qtjson.cpp
@@ -303,12 +303,27 @@ void tst_QtJson::testObjectSimple()
QVERIFY2(!object.contains("boolean"), "key boolean should have been removed");
QJsonValue taken = object.take("value");
-// QCOMPARE(taken, value);
+ QCOMPARE(taken, value);
QVERIFY2(!object.contains("value"), "key value should have been removed");
QString before = object.value("string").toString();
object.insert("string", QString::fromLatin1("foo"));
QVERIFY2(object.value("string").toString() != before, "value should have been updated");
+
+ size = object.size();
+ QJsonObject subobject;
+ subobject.insert("number", 42);
+ subobject.insert(QLatin1String("string"), QLatin1String("foobar"));
+ object.insert("subobject", subobject);
+ QCOMPARE(object.size(), size+1);
+ QJsonValue subvalue = object.take(QLatin1String("subobject"));
+ QCOMPARE(object.size(), size);
+ QCOMPARE(subvalue.toObject(), subobject);
+ // make object detach by modifying it many times
+ for (int i = 0; i < 64; ++i)
+ object.insert(QLatin1String("string"), QLatin1String("bar"));
+ QCOMPARE(object.size(), size);
+ QCOMPARE(subvalue.toObject(), subobject);
}
void tst_QtJson::testObjectSmallKeys()