summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDenis Dzyubenko <denis.dzyubenko@nokia.com>2012-01-06 17:18:55 +0100
committerLars Knoll <lars.knoll@nokia.com>2012-01-06 23:06:22 +0100
commit7ef5aa21ace014cccc94420ba7624f2f0477bf1c (patch)
treef5042d956513fcafa110687cb37c5310c3a87452
parentb80bc3f04d650cf8a38429dfb5473daf38a0a4b8 (diff)
Fixed broken binary output after compaction
The tag was not present in the header after compaction was triggered Change-Id: I329519698961e615a378b51711fbb4d7978af665 Sanity-Review: Qt Sanity Bot <qt_sanity_bot@ovi.com> Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
-rw-r--r--src/qjson.cpp2
-rw-r--r--tests/auto/tst_qtjson.cpp20
2 files changed, 22 insertions, 0 deletions
diff --git a/src/qjson.cpp b/src/qjson.cpp
index 6d1aa70..fb31187 100644
--- a/src/qjson.cpp
+++ b/src/qjson.cpp
@@ -65,6 +65,8 @@ void Data::compact()
int size = sizeof(Base) + reserve + base->length*sizeof(offset);
int alloc = sizeof(Header) + size;
Header *h = (Header *) malloc(alloc);
+ h->tag = QBJS_Tag;
+ h->version = 1;
Base *b = h->root();
b->size = size;
b->is_object = header->root()->is_object;
diff --git a/tests/auto/tst_qtjson.cpp b/tests/auto/tst_qtjson.cpp
index 79e5ee1..6023950 100644
--- a/tests/auto/tst_qtjson.cpp
+++ b/tests/auto/tst_qtjson.cpp
@@ -92,6 +92,7 @@ private Q_SLOTS:
void validation();
void testDuplicateKeys();
+ void testCompaction();
};
TestQtJson::TestQtJson(QObject *parent) : QObject(parent)
@@ -1087,6 +1088,25 @@ void TestQtJson::testDuplicateKeys()
QCOMPARE(obj.value(QLatin1String("foo")).toString(), QLatin1String("zap"));
}
+void TestQtJson::testCompaction()
+{
+ // modify object enough times to trigger compactionCounter
+ // and make sure the data is still valid
+ JsonObject obj;
+ for (int i = 0; i < 33; ++i) {
+ obj.remove(QLatin1String("foo"));
+ obj.insert(QLatin1String("foo"), QLatin1String("bar"));
+ }
+ QCOMPARE(obj.numKeys(), 1);
+ QCOMPARE(obj.value(QLatin1String("foo")).toString(), QLatin1String("bar"));
+
+ JsonDocument doc = JsonDocument::fromBinaryData(JsonDocument(obj).toBinaryData());
+ QVERIFY(doc.isValid());
+ QVERIFY(!doc.isEmpty());
+ QVERIFY(doc.type() == ObjectValue);
+ QVERIFY(doc.object() == obj);
+}
+
QTEST_MAIN(TestQtJson)
#include "tst_qtjson.moc"