Skip to content

Commit

Permalink
Make sure remove() doesn't corrupt the json object
Browse files Browse the repository at this point in the history
When using non latin keys, remove() could cause corruption
of the json object.

Task-number: QTBUG-42270
Change-Id: I7305e57ebb78630a9bf68bc4f831a6d1646abb79
Reviewed-by: Thiago Macieira <[email protected]>
  • Loading branch information
laknoll authored and Lars Knoll committed Mar 18, 2015
1 parent 124da60 commit 0411240
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/corelib/json/qjson_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ class Entry {
if (value.latinKey)
s += sizeof(ushort) + qFromLittleEndian(*(ushort *) ((const char *)this + sizeof(Entry)));
else
s += sizeof(uint) + qFromLittleEndian(*(int *) ((const char *)this + sizeof(Entry)));
s += sizeof(uint) + sizeof(ushort)*qFromLittleEndian(*(int *) ((const char *)this + sizeof(Entry)));
return alignedSize(s);
}

Expand Down
21 changes: 21 additions & 0 deletions tests/auto/corelib/json/tst_qtjson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ private Q_SLOTS:

void unicodeKeys();
void garbageAtEnd();

void removeNonLatinKey();
private:
QString testDataDir;
};
Expand Down Expand Up @@ -2776,5 +2778,24 @@ void tst_QtJson::garbageAtEnd()
QVERIFY(!doc.isEmpty());
}

void tst_QtJson::removeNonLatinKey()
{
const QString nonLatinKeyName = QString::fromUtf8("Атрибут100500");

QJsonObject sourceObject;

sourceObject.insert("code", 1);
sourceObject.remove("code");

sourceObject.insert(nonLatinKeyName, 1);

const QByteArray json = QJsonDocument(sourceObject).toJson();
const QJsonObject restoredObject = QJsonDocument::fromJson(json).object();

QCOMPARE(sourceObject.keys(), restoredObject.keys());
QVERIFY(sourceObject.contains(nonLatinKeyName));
QVERIFY(restoredObject.contains(nonLatinKeyName));
}

QTEST_MAIN(tst_QtJson)
#include "tst_qtjson.moc"

0 comments on commit 0411240

Please sign in to comment.