Skip to content

Commit

Permalink
QJsonObject::take: add missing detach() call
Browse files Browse the repository at this point in the history
We were modifying shared objects.

Fixes: QTBUG-89625
Change-Id: Id6bc735b79cf4beb9454fffd165c56476a5dec04
Reviewed-by: Volker Hilsheimer <[email protected]>
Reviewed-by: Christian Ehrlicher <[email protected]>
(cherry picked from commit 00b759a)
Reviewed-by: Qt Cherry-pick Bot <[email protected]>
  • Loading branch information
thiagomacieira authored and Qt Cherry-pick Bot committed Feb 3, 2021
1 parent b9f9357 commit 41830b8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/corelib/serialization/qjsonobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,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;
Expand Down
19 changes: 19 additions & 0 deletions tests/auto/corelib/serialization/json/tst_qtjson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ private Q_SLOTS:
void testNumbers_4();

void testObjectSimple();
void testObjectTakeDetach();
void testObjectSmallKeys();
void testObjectInsertCopies();
void testArraySimple();
Expand Down Expand Up @@ -523,6 +524,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;
Expand Down

0 comments on commit 41830b8

Please sign in to comment.