aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2022-08-02 11:34:53 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2022-08-23 05:12:41 +0000
commite2c637ab36c441eaa80e1845c0f9ab4a75542f0b (patch)
tree4a78c206f4a8d1778e3f00efceb43c567ab4e6cc /tests
parentab56ee12778bb4a96f809d4ccec8561c1bbfd8d8 (diff)
JSON: Properly handle bad objects in JSON.stringify()
For objects with circular structures we generate a proper error message and fail earlier. For objects with excessive recursion we throw a range error rather than crashing. This behavior is modeled after node's behavior in such circumstances. We use the existing stack overflow detection to determine when to throw the range error. Testing shows that on windows the limit was insufficient. Lower it. Fixes: QTBUG-92192 Change-Id: I25dd302f65f359111e42492df3c71549c4ed7157 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> (cherry picked from commit af1ef35fa00a466d3af04c17b59fcb4ea38f396a) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/qml/qjsonbinding/tst_qjsonbinding.cpp40
1 files changed, 40 insertions, 0 deletions
diff --git a/tests/auto/qml/qjsonbinding/tst_qjsonbinding.cpp b/tests/auto/qml/qjsonbinding/tst_qjsonbinding.cpp
index cc301081cf..6235ff376d 100644
--- a/tests/auto/qml/qjsonbinding/tst_qjsonbinding.cpp
+++ b/tests/auto/qml/qjsonbinding/tst_qjsonbinding.cpp
@@ -52,6 +52,9 @@ private slots:
void writeProperty_javascriptExpression_data();
void writeProperty_javascriptExpression();
+ void cyclicStringify();
+ void recursiveStringify();
+
private:
QByteArray readAsUtf8(const QString &fileName);
static QJsonValue valueFromJson(const QByteArray &json);
@@ -489,6 +492,43 @@ void tst_qjsonbinding::writeProperty_javascriptExpression()
QCOMPARE(ret.toString(), expectedJson);
}
+void tst_qjsonbinding::cyclicStringify()
+{
+ QJSEngine e;
+ const QString program = QStringLiteral(R"(
+ var a = {};
+ a.a = a;
+ JSON.stringify(a);
+ )");
+
+ QJSValue result = e.evaluate(program);
+ QVERIFY(result.isError());
+ QCOMPARE(result.errorType(), QJSValue::TypeError);
+ QVERIFY(result.toString().contains(QLatin1String("Cannot convert circular structure to JSON")));
+}
+
+void tst_qjsonbinding::recursiveStringify()
+{
+ QJSEngine e;
+ const QString program = QStringLiteral(R"(
+ function create() {
+ var a = {}
+ Object.defineProperty(a, "a", {
+ enumerable: true,
+ get: function() { return create(); }
+ });
+ return a;
+ }
+
+ JSON.stringify(create());
+ )");
+
+ QJSValue result = e.evaluate(program);
+ QVERIFY(result.isError());
+ QCOMPARE(result.errorType(), QJSValue::RangeError);
+ QVERIFY(result.toString().contains(QLatin1String("Maximum call stack size exceeded")));
+}
+
QTEST_MAIN(tst_qjsonbinding)
#include "tst_qjsonbinding.moc"