From 6b5b5d4e530eef3f5a7025f753d6f54e76ad008f Mon Sep 17 00:00:00 2001 From: Denis Dzyubenko Date: Mon, 4 Jun 2012 18:48:51 +0200 Subject: [PATCH] Added missing detach() to QJsonObject::take() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Denis Dzyubenko --- src/corelib/json/qjsonobject.cpp | 5 +++-- tests/auto/corelib/json/tst_qtjson.cpp | 17 ++++++++++++++++- 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()