From e1d3687d64a19d27448b3f8247505daa99261ea1 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Fri, 15 Feb 2013 10:44:54 +0100 Subject: Fix crashes when creating large documents Compact an object in regular intervals when inserting data into it, to avoid the object becoming huge. Compact an object/array before inserting into another array or object. Check that the document doesn't get so big it's overflowing the internal data structures. Task-number: QTBUG-29288 Change-Id: Id39d80dac1e7d5a11f40819f41b4b336bce16947 Reviewed-by: Thiago Macieira --- src/corelib/json/qjson.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'src/corelib/json/qjson.cpp') diff --git a/src/corelib/json/qjson.cpp b/src/corelib/json/qjson.cpp index ed6ef74e3c..8215aeefc0 100644 --- a/src/corelib/json/qjson.cpp +++ b/src/corelib/json/qjson.cpp @@ -149,6 +149,10 @@ bool Data::valid() const int Base::reserveSpace(uint dataSize, int posInTable, uint numItems, bool replace) { Q_ASSERT(posInTable >= 0 && posInTable <= (int)length); + if (size + dataSize >= Value::MaxSize) { + qWarning("QJson: Document too large to store in data structure %d %d %d", (uint)size, dataSize, Value::MaxSize); + return 0; + } offset off = tableOffset; // move table to new position @@ -334,7 +338,7 @@ bool Value::isValid(const Base *b) const /*! \internal */ -int Value::requiredStorage(const QJsonValue &v, bool *compressed) +int Value::requiredStorage(QJsonValue &v, bool *compressed) { *compressed = false; switch (v.t) { @@ -351,6 +355,11 @@ int Value::requiredStorage(const QJsonValue &v, bool *compressed) } case QJsonValue::Array: case QJsonValue::Object: + if (v.d && v.d->compactionCounter) { + v.detach(); + v.d->compact(); + v.base = static_cast(v.d->header->root()); + } return v.base ? v.base->size : sizeof(QJsonPrivate::Base); case QJsonValue::Undefined: case QJsonValue::Null: -- cgit v1.2.3