Properly detach when the modified object is a sub object

The clone() method didn't detach if we had enough memory
allocated, but didn't consider that the object being
modified is not the root object of the binary blob.

Change-Id: I9a479ae1c873b7fe9cff7e13c539e7a41961bf68
Reviewed-by: Cristiano di Flora <cristiano.di-flora@nokia.com>
Reviewed-by: abcd <amos.choy@nokia.com>
Reviewed-by: Jamey Hicks <jamey.hicks@nokia.com>
This commit is contained in:
Lars Knoll 2012-03-29 12:25:09 +02:00 committed by Qt by Nokia
parent 19109a7697
commit 698b33fcce
2 changed files with 26 additions and 1 deletions

View File

@ -745,7 +745,7 @@ public:
Data *clone(Base *b, int reserve = 0) Data *clone(Base *b, int reserve = 0)
{ {
int size = sizeof(Header) + b->size; 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; return this;
if (reserve) { if (reserve) {

View File

@ -122,6 +122,7 @@ private Q_SLOTS:
void assignArrays(); void assignArrays();
void testTrailingComma(); void testTrailingComma();
void testDetachBug();
private: private:
QString testDataDir; 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) QTEST_MAIN(TestQtJson)
#include "tst_qtjson.moc" #include "tst_qtjson.moc"