From e133f0cca44181005f19d006d6c896abe59c1b33 Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Fri, 11 Nov 2016 17:06:19 +0100 Subject: Improve error offset in JSON parsing Do not consume white-space after a token before the token has been parsed, otherwise we end up with misleading offsets. This also fixes a wrong error of illegal number in several cases. Change-Id: I492ca4de0346a1d0ab73b1c23d7a72dba812664c Reviewed-by: Lars Knoll Reviewed-by: Oswald Buddenhagen --- tests/auto/corelib/json/tst_qtjson.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'tests') diff --git a/tests/auto/corelib/json/tst_qtjson.cpp b/tests/auto/corelib/json/tst_qtjson.cpp index 17892b44a2..4a6584a0f6 100644 --- a/tests/auto/corelib/json/tst_qtjson.cpp +++ b/tests/auto/corelib/json/tst_qtjson.cpp @@ -139,6 +139,9 @@ private Q_SLOTS: void removeNonLatinKey(); void documentFromVariant(); + void parseErrorOffset_data(); + void parseErrorOffset(); + private: QString testDataDir; }; @@ -2830,5 +2833,35 @@ void tst_QtJson::documentFromVariant() QCOMPARE(do1.object(), do2.object()); } +void tst_QtJson::parseErrorOffset_data() +{ + QTest::addColumn("json"); + QTest::addColumn("errorOffset"); + + QTest::newRow("Trailing comma in object") << QByteArray("{ \"value\": false, }") << 19; + QTest::newRow("Trailing comma in object plus whitespace") << QByteArray("{ \"value\": false, } ") << 19; + QTest::newRow("Trailing comma in array") << QByteArray("[ false, ]") << 10; + QTest::newRow("Trailing comma in array plus whitespace") << QByteArray("[ false, ] ") << 10; + QTest::newRow("Missing value in object") << QByteArray("{ \"value\": , } ") << 12; + QTest::newRow("Missing value in array") << QByteArray("[ \"value\" , , ] ") << 13; + QTest::newRow("Leading comma in object") << QByteArray("{ , \"value\": false}") << 3; + QTest::newRow("Leading comma in array") << QByteArray("[ , false]") << 3; + QTest::newRow("Stray ,") << QByteArray(" , ") << 3; + QTest::newRow("Stray [") << QByteArray(" [ ") << 5; + QTest::newRow("Stray }") << QByteArray(" } ") << 3; +} + +void tst_QtJson::parseErrorOffset() +{ + QFETCH(QByteArray, json); + QFETCH(int, errorOffset); + + QJsonParseError error; + QJsonDocument::fromJson(json, &error); + + QVERIFY(error.error != QJsonParseError::NoError); + QCOMPARE(error.offset, errorOffset); +} + QTEST_MAIN(tst_QtJson) #include "tst_qtjson.moc" -- cgit v1.2.3