QJsonObject::take: add missing detach() call

We were modifying shared objects.

Fixes: QTBUG-89625
Change-Id: Id6bc735b79cf4beb9454fffd165c56476a5dec04
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Christian Ehrlicher <ch.ehrlicher@gmx.de>
(cherry picked from commit 00b759a8d06dbec42232b1b8748c0725da7ced00)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Thiago Macieira 2021-01-21 11:33:51 -08:00 committed by Qt Cherry-pick Bot
parent 191e1bbf37
commit 9b43d8a98a
2 changed files with 20 additions and 0 deletions

View File

@ -617,6 +617,7 @@ QJsonValue QJsonObject::takeImpl(T key)
if (!keyExists)
return QJsonValue(QJsonValue::Undefined);
detach();
const QJsonValue v = QJsonPrivate::Value::fromTrustedCbor(o->extractAt(index + 1));
removeAt(index / 2);
return v;

View File

@ -58,6 +58,7 @@ private Q_SLOTS:
void testNumberComparisons();
void testObjectSimple();
void testObjectTakeDetach();
void testObjectSmallKeys();
void testObjectInsertCopies();
void testArraySimple();
@ -575,6 +576,24 @@ void tst_QtJson::testObjectSimple()
QCOMPARE(subvalue.toObject(), subobject);
}
void tst_QtJson::testObjectTakeDetach()
{
QJsonObject object1, object2;
object1["key1"] = 1;
object1["key2"] = 2;
object2 = object1;
object1.take("key2");
object1.remove("key1");
QVERIFY(!object1.contains("key1"));
QVERIFY(object2.contains("key1"));
QVERIFY(object2.value("key1").isDouble());
QVERIFY(!object1.contains("key2"));
QVERIFY(object2.contains("key2"));
QVERIFY(object2.value("key2").isDouble());
}
void tst_QtJson::testObjectSmallKeys()
{
QJsonObject data1;