summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2022-01-14 13:41:20 +0100
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2022-01-18 03:37:24 +0000
commit2ed7c64dd92eedeccaae6ff9d8531d9b4b0ff22a (patch)
tree337c612f8026d7ed2728d136f0247a9815231828 /tests
parent77895514d5419b77535de093b544aee30686cd22 (diff)
JSON: When clearing duplicate object entries, also clear containers
Previously, if you had multiple entries with the same name in an object, and some of them were again objects or arrays, parsing the JSON document would leak memory. Also, we use std::stable_sort instead of std::sort now, so that we don't accidentally randomize the order of elements with equal keys. [ChangeLog][QtCore][JSON] A memory leak in the JSON parser when reading objects with duplicate keys was fixed. Fixes: QTBUG-99799 Change-Id: Ic2065f2e490c2d3506a356745542148ad9c24262 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> (cherry picked from commit 1b4a5ecc91c87ffe7869d233c00a37d1b5b9cb13) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/corelib/serialization/json/tst_qtjson.cpp19
1 files changed, 19 insertions, 0 deletions
diff --git a/tests/auto/corelib/serialization/json/tst_qtjson.cpp b/tests/auto/corelib/serialization/json/tst_qtjson.cpp
index 57a679dbc9..3a57d3b8a5 100644
--- a/tests/auto/corelib/serialization/json/tst_qtjson.cpp
+++ b/tests/auto/corelib/serialization/json/tst_qtjson.cpp
@@ -175,6 +175,7 @@ private Q_SLOTS:
void fromToVariantConversions();
void testIteratorComparison();
+ void noLeakOnNameClash();
private:
QString testDataDir;
@@ -3649,5 +3650,23 @@ void tst_QtJson::testIteratorComparison()
QVERIFY(t.end() > t.begin());
}
+void tst_QtJson::noLeakOnNameClash()
+{
+ QJsonDocument doc = QJsonDocument::fromJson("{\"\":{\"\":0},\"\":0}");
+ QVERIFY(!doc.isNull());
+ const QJsonObject obj = doc.object();
+
+ // Removed the duplicate key.
+ QCOMPARE(obj.length(), 1);
+
+ // Retained the last of the duplicates.
+ const QJsonValue val = obj.begin().value();
+ QVERIFY(val.isDouble());
+ QCOMPARE(val.toDouble(), 0.0);
+
+ // It should not leak.
+ // In particular it should not forget to deref the container for the inner object.
+}
+
QTEST_MAIN(tst_QtJson)
#include "tst_qtjson.moc"