diff options
author | Lars Knoll <lars.knoll@nokia.com> | 2012-05-12 12:09:35 +0200 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-05-16 04:25:11 +0200 |
commit | 1756a84756807a9849aa507e77845dfdf31b8020 (patch) | |
tree | 0ed4ff43ba687440a6a2c2a52da183f752465f51 /tests/auto/corelib | |
parent | d5000e82e3d17532b76d5a69fd35ac4e8f3b9d18 (diff) |
Limit the nesting depth of the Json parser
The parser is recursive and too deeply nested json would
cause it to exhaust the available stack space leading to
crashes.
We now abort parsing with a DeepNesting parse error if the
document is too deeply nested. The current nesting limit
is set to 1024, which should be more then enough for any
real JSON data set.
Change-Id: I4adea3fd727149f7342536d73cf4530361a0a3a1
Reviewed-by: Jamey Hicks <jamey.hicks@nokia.com>
Reviewed-by: Denis Dzyubenko <denis.dzyubenko@nokia.com>
Diffstat (limited to 'tests/auto/corelib')
-rw-r--r-- | tests/auto/corelib/json/tst_qtjson.cpp | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/tests/auto/corelib/json/tst_qtjson.cpp b/tests/auto/corelib/json/tst_qtjson.cpp index 84913f9289..00810889bb 100644 --- a/tests/auto/corelib/json/tst_qtjson.cpp +++ b/tests/auto/corelib/json/tst_qtjson.cpp @@ -127,6 +127,7 @@ private Q_SLOTS: void valueEquals(); void bom(); + void nesting(); private: QString testDataDir; }; @@ -1924,5 +1925,64 @@ void TestQtJson::bom() QVERIFY(error.error == QJsonParseError::NoError); } +void TestQtJson::nesting() +{ + // check that we abort parsing too deeply nested json documents. + // this is to make sure we don't crash because the parser exhausts the + // stack. + + const char *array_data = + "[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[" + "[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[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]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]" + "]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]" + "]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]" + "]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]" + "]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]" + "]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]" + "]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]"; + + QByteArray json(array_data); + QJsonParseError error; + QJsonDocument doc = QJsonDocument::fromJson(json, &error); + + QVERIFY(!doc.isNull()); + QVERIFY(error.error == QJsonParseError::NoError); + + json.prepend("["); + json.append("]"); + doc = QJsonDocument::fromJson(json, &error); + + QVERIFY(doc.isNull()); + QVERIFY(error.error == QJsonParseError::DeepNesting); + + json = QByteArray("true "); + + for (int i = 0; i < 1024; ++i) { + json.prepend("{ \"Key\": "); + json.append(" }"); + } + + doc = QJsonDocument::fromJson(json, &error); + + QVERIFY(!doc.isNull()); + QVERIFY(error.error == QJsonParseError::NoError); + + json.prepend("["); + json.append("]"); + doc = QJsonDocument::fromJson(json, &error); + + QVERIFY(doc.isNull()); + QVERIFY(error.error == QJsonParseError::DeepNesting); + +} + QTEST_MAIN(TestQtJson) #include "tst_qtjson.moc" |