summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThiago Macieira <[email protected]>2025-02-13 09:45:28 -0800
committerJani Heikkinen <[email protected]>2025-03-17 13:10:27 +0000
commitc07c2d5a527a644d36e7853d55132ae38921682f (patch)
tree1a28ee71f7490a7b4c80841545438d740a5759c3
parente9fef44c65ca765f108726c7693c9100ec1b166d (diff)
CBOR/JSON: fix crash when comparing strings with different lengthv6.8.36.8.3
Amends 8e5ce9cd369230256045864d6fad38dbd8bee413, which introduced the QtPrivate::equalStrings() call. At that time, equalStrings() had already required equal lengths (see 1560e0161af70b5cf88a70e55c0b502612d433cd), so no excuse. [ChangeLog][QtCore][QCborMap and QJsonObject] Fixed bug that could result in a crash or failing to find a entry in the map/object with non- ASCII keys. Manual conflict resolution for 6.9: - Port from keyView() to key(), because the former is a 6.10+ feature. Fixes: QTBUG-133744 Change-Id: I6b0f8b0a2e47d3ef905afffda6c4c079814a0914 Reviewed-by: Marc Mutz <[email protected]> (cherry picked from commit 54daec43a041cb69cff31cbfd1dd0b7127e8ba87) (cherry picked from commit 8e94b67ba11fecfee134950eaae5c5b29812e82c) Reviewed-by: Qt Cherry-pick Bot <[email protected]> (cherry picked from commit d4a90a3f3013eae4e837fbd2df63d5803f19608a)
-rw-r--r--src/corelib/serialization/qcborvalue_p.h4
-rw-r--r--tests/auto/corelib/serialization/json/tst_qtjson.cpp17
-rw-r--r--tests/auto/corelib/serialization/qcborvalue/tst_qcborvalue.cpp42
3 files changed, 61 insertions, 2 deletions
diff --git a/src/corelib/serialization/qcborvalue_p.h b/src/corelib/serialization/qcborvalue_p.h
index 33eb912a6d9..8cb192eee0d 100644
--- a/src/corelib/serialization/qcborvalue_p.h
+++ b/src/corelib/serialization/qcborvalue_p.h
@@ -362,8 +362,8 @@ public:
if (e.flags & QtCbor::Element::StringIsUtf16) {
if (mode == QtCbor::Comparison::ForEquality)
- return QtPrivate::equalStrings(b->asStringView(), s) ? 0 : 1;
- return QtPrivate::compareStrings(b->asStringView(), s);
+ return b->asStringView() == s ? 0 : 1;
+ return b->asStringView().compare(s);
}
return compareUtf8(b, s);
}
diff --git a/tests/auto/corelib/serialization/json/tst_qtjson.cpp b/tests/auto/corelib/serialization/json/tst_qtjson.cpp
index e6c8f90e8f9..96060bbd132 100644
--- a/tests/auto/corelib/serialization/json/tst_qtjson.cpp
+++ b/tests/auto/corelib/serialization/json/tst_qtjson.cpp
@@ -41,6 +41,7 @@ private Q_SLOTS:
void testObjectTakeDetach();
void testObjectSmallKeys();
void testObjectInsertCopies();
+ void testObjectInsertNonAscii();
void testArraySimple();
void testArrayInsertCopies();
void testValueObject();
@@ -714,6 +715,22 @@ void tst_QtJson::testObjectInsertCopies()
}
}
+void tst_QtJson::testObjectInsertNonAscii()
+{
+ {
+ QJsonObject myObject;
+ myObject.insert("k♭", "First key");
+ myObject.insert("a", "Second key");
+ QCOMPARE(myObject.begin().key(), "a");
+ }
+ {
+ QJsonObject myObject;
+ myObject.insert("a", "Second key");
+ myObject.insert("k♭", "First key");
+ QCOMPARE(myObject.begin().key(), "a");
+ }
+}
+
void tst_QtJson::testArraySimple()
{
QJsonArray array;
diff --git a/tests/auto/corelib/serialization/qcborvalue/tst_qcborvalue.cpp b/tests/auto/corelib/serialization/qcborvalue/tst_qcborvalue.cpp
index 55ef4700950..1bd786ca04d 100644
--- a/tests/auto/corelib/serialization/qcborvalue/tst_qcborvalue.cpp
+++ b/tests/auto/corelib/serialization/qcborvalue/tst_qcborvalue.cpp
@@ -74,6 +74,7 @@ private slots:
void mapMutateWithCopies();
void mapStringValues();
void mapStringKeys();
+ void mapStringKeysNonAscii();
void mapValueRef_data() { basics_data(); }
void mapValueRef();
void mapInsertRemove_data() { basics_data(); }
@@ -1529,6 +1530,47 @@ void tst_QCborValue::mapStringKeys()
QVERIFY(m2.value(QCborValue(QByteArray("foo"))).isUndefined());
QVERIFY(m.value(QCborValue(QLatin1String("foo"))).isUndefined());
QCOMPARE(m.value(QCborValue(QByteArray("foo"))).toString(), "bar");
+
+ m.insert(u"World"_s, QCborValue(3)); // replaces
+ QCOMPARE(m.size(), 3);
+ QCOMPARE(m.value(u"World"_s), QCborValue(3));
+ QCOMPARE(m.value("World"_L1), QCborValue(3));
+
+ m.insert(u"Hello"_s, QCborValue(4)); // replaces (must match Latin1)
+ QCOMPARE(m.size(), 3);
+ QCOMPARE(m.value(u"Hello"_s), QCborValue(4));
+ QCOMPARE(m.value("Hello"_L1), QCborValue(4));
+}
+
+void tst_QCborValue::mapStringKeysNonAscii()
+{
+ {
+ QCborMap m;
+ m["a"_L1] = 1; // US-ASCII: 1 UTF-8 & UTF-16 code unit
+ m["\xE9"_L1] = 2; // Latin-1: 2 UTF-8 code units, 1 UTF-16
+ m[u"ü"_s] = 3; // ditto, but inserted as UTF-16
+ m[u"♭"_s] = 4; // BMP over U+07FF: 3 UTF-8 code units, 1 UTF-16
+ m[u"\U00010000"_s] = 5; // non-BMP: 4 UTF-8 code units, 2 UTF-16
+
+ QCOMPARE(m.size(), 5);
+ QCOMPARE(m.value(u"a"_s), 1);
+ QCOMPARE(m.value(u"é"_s), 2);
+ QCOMPARE(m.value("\xFC"_L1), 3);
+
+ QCOMPARE(m.value(u"k♭"_s), QCborValue());
+ QCOMPARE(m.value("foo"_L1), QCborValue());
+ }
+ {
+ QCborMap m;
+ m[u"k♭"_s] = 1;
+ m[u"a"_s] = 2;
+ m[u"\U00010000"_s] = 3;
+
+ QCOMPARE(m.size(), 3);
+ QCOMPARE(m.value(u"b"_s), QCborValue());
+ QCOMPARE(m.value(u"♭"_s), QCborValue());
+ QCOMPARE(m.value("foo"_L1), QCborValue());
+ }
}
void tst_QCborValue::mapInsertRemove()