diff options
-rw-r--r-- | src/corelib/json/qjson_p.h | 2 | ||||
-rw-r--r-- | tests/auto/corelib/json/tst_qtjson.cpp | 25 |
2 files changed, 26 insertions, 1 deletions
diff --git a/src/corelib/json/qjson_p.h b/src/corelib/json/qjson_p.h index f8f41c2e25..831774e456 100644 --- a/src/corelib/json/qjson_p.h +++ b/src/corelib/json/qjson_p.h @@ -745,7 +745,7 @@ public: Data *clone(Base *b, int reserve = 0) { int size = sizeof(Header) + b->size; - if (ref.load() == 1 && alloc >= size + reserve) + if (b == header->root() && ref.load() == 1 && alloc >= size + reserve) return this; if (reserve) { diff --git a/tests/auto/corelib/json/tst_qtjson.cpp b/tests/auto/corelib/json/tst_qtjson.cpp index 8a206dd6f9..4ab4b78da1 100644 --- a/tests/auto/corelib/json/tst_qtjson.cpp +++ b/tests/auto/corelib/json/tst_qtjson.cpp @@ -122,6 +122,7 @@ private Q_SLOTS: void assignArrays(); void testTrailingComma(); + void testDetachBug(); private: QString testDataDir; }; @@ -1820,5 +1821,29 @@ void TestQtJson::testTrailingComma() } } +void TestQtJson::testDetachBug() +{ + QJsonObject dynamic; + QJsonObject embedded; + + QJsonObject local; + + embedded.insert("Key1", QString("Value1")); + embedded.insert("Key2", QString("Value2")); + dynamic.insert(QStringLiteral("Bogus"), QString("bogusValue")); + dynamic.insert("embedded", embedded); + local = dynamic.value("embedded").toObject(); + + dynamic.remove("embedded"); + + QCOMPARE(local.keys().size(),2); + local.remove("Key1"); + local.remove("Key2"); + QCOMPARE(local.keys().size(), 0); + + local.insert("Key1", QString("anotherValue")); + QCOMPARE(local.keys().size(), 1); +} + QTEST_MAIN(TestQtJson) #include "tst_qtjson.moc" |