1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

Fix ODR violation in SettingsSerializer

Fixes #2173

Also fixes some compilation errors in some situations
This commit is contained in:
tux3 2016-01-19 16:28:42 +01:00
parent 96582c4161
commit f79bb24024
No known key found for this signature in database
GPG Key ID: 7E086DD661263264
2 changed files with 36 additions and 53 deletions

View File

@ -31,17 +31,12 @@ using namespace std;
const char SettingsSerializer::magic[] = {0x51,0x54,0x4F,0x58}; const char SettingsSerializer::magic[] = {0x51,0x54,0x4F,0x58};
inline QDataStream& operator<<(QDataStream& dataStream, const SettingsSerializer::RecordTag& tag) QDataStream& writeStream(QDataStream& dataStream, const SettingsSerializer::RecordTag& tag)
{ {
return dataStream << static_cast<uint8_t>(tag); return dataStream << static_cast<uint8_t>(tag);
} }
inline QDataStream& operator<<(QDataStream& dataStream, const QString& str) QDataStream& writeStream(QDataStream& dataStream, const QByteArray& data)
{
return dataStream << str.toUtf8();
}
inline QDataStream& operator<<(QDataStream& dataStream, const QByteArray& data)
{ {
QByteArray size = vuintToData(data.size()); QByteArray size = vuintToData(data.size());
dataStream.writeRawData(size.data(), size.size()); dataStream.writeRawData(size.data(), size.size());
@ -49,12 +44,18 @@ inline QDataStream& operator<<(QDataStream& dataStream, const QByteArray& data)
return dataStream; return dataStream;
} }
QDataStream& operator>>(QDataStream& dataStream, SettingsSerializer::RecordTag& tag) QDataStream& writeStream(QDataStream& dataStream, const QString& str)
{
return writeStream(dataStream, str.toUtf8());
}
QDataStream& readStream(QDataStream& dataStream, SettingsSerializer::RecordTag& tag)
{ {
return dataStream.operator >>((uint8_t&)tag); return dataStream.operator >>((uint8_t&)tag);
} }
inline QDataStream& operator>>(QDataStream& dataStream, QByteArray& data)
QDataStream& readStream(QDataStream& dataStream, QByteArray& data)
{ {
unsigned char num3; unsigned char num3;
size_t num = 0; size_t num = 0;
@ -214,16 +215,6 @@ void SettingsSerializer::load()
readSerialized(); readSerialized();
else else
readIni(); readIni();
/* Dump state for debugging
qDebug() << "SettingsSerializer data:";
for (int i=0; i<groups.size(); i++)
qDebug()<<"Group"<<i<<"is"<<groups[i];
for (int i=0; i<arrays.size(); i++)
qDebug()<<"Array"<<i<<"size"<<arrays[i].size<<arrays[i].values.size()<<"of group"<<arrays[i].group<<"is"<<arrays[i].name;
for (int i=0; i<values.size(); i++)
qDebug()<<"Value"<<i<<"of group"<<values[i].group<<"array"<<values[i].array<<values[i].arrayIndex<<"key"<<values[i].key;
*/
} }
void SettingsSerializer::save() void SettingsSerializer::save()
@ -244,9 +235,8 @@ void SettingsSerializer::save()
// Save the group name, if any // Save the group name, if any
if (g!=-1) if (g!=-1)
{ {
stream << RecordTag::GroupStart; writeStream(stream, RecordTag::GroupStart);
stream << groups[g].toUtf8(); writeStream(stream, groups[g].toUtf8());
//qDebug()<<"#Group"<<groups[g];
} }
// Save all the arrays of this group // Save all the arrays of this group
@ -256,19 +246,18 @@ void SettingsSerializer::save()
continue; continue;
if (a.size <= 0) if (a.size <= 0)
continue; continue;
stream << RecordTag::ArrayStart; writeStream(stream, RecordTag::ArrayStart);
stream << a.name.toUtf8(); writeStream(stream, a.name.toUtf8());
stream << vuintToData(a.size); writeStream(stream, vuintToData(a.size));
//qDebug()<<"#array start"<<a.name<<a.size;
for (uint64_t vi : a.values) for (uint64_t vi : a.values)
{ {
stream << RecordTag::ArrayValue; writeStream(stream, RecordTag::ArrayValue);
stream << vuintToData(values[vi].arrayIndex); writeStream(stream, vuintToData(values[vi].arrayIndex));
stream << values[vi].key.toUtf8(); writeStream(stream, values[vi].key.toUtf8());
writePackedVariant(stream, values[vi].value); writePackedVariant(stream, values[vi].value);
//qDebug()<<"#key (in array)"<<values[vi].key;
} }
stream << RecordTag::ArrayEnd; writeStream(stream, RecordTag::ArrayEnd);
} }
// Save all the values of this group that aren't in an array // Save all the values of this group that aren't in an array
@ -276,10 +265,9 @@ void SettingsSerializer::save()
{ {
if (v.group != g || v.array != -1) if (v.group != g || v.array != -1)
continue; continue;
stream << RecordTag::Value; writeStream(stream, RecordTag::Value);
stream << v.key.toUtf8(); writeStream(stream, v.key.toUtf8());
writePackedVariant(stream, v.value); writePackedVariant(stream, v.value);
//qDebug()<<"#key (standalone)"<<v.key;
} }
} }
@ -347,30 +335,28 @@ void SettingsSerializer::readSerialized()
while (!stream.atEnd()) while (!stream.atEnd())
{ {
RecordTag tag; RecordTag tag;
stream >> tag; readStream(stream, tag);
if (tag == RecordTag::Value) if (tag == RecordTag::Value)
{ {
QByteArray key; QByteArray key;
QByteArray value; QByteArray value;
stream >> key; readStream(stream, key);
stream >> value; readStream(stream, value);
setValue(QString::fromUtf8(key), QVariant(QString::fromUtf8(value))); setValue(QString::fromUtf8(key), QVariant(QString::fromUtf8(value)));
//qDebug() << "!Got key"<<key;
} }
else if (tag == RecordTag::GroupStart) else if (tag == RecordTag::GroupStart)
{ {
QByteArray prefix; QByteArray prefix;
stream >> prefix; readStream(stream, prefix);
beginGroup(QString::fromUtf8(prefix)); beginGroup(QString::fromUtf8(prefix));
//qDebug()<<"!Group start"<<prefix;
} }
else if (tag == RecordTag::ArrayStart) else if (tag == RecordTag::ArrayStart)
{ {
QByteArray prefix; QByteArray prefix;
stream >> prefix; readStream(stream, prefix);
beginReadArray(QString::fromUtf8(prefix)); beginReadArray(QString::fromUtf8(prefix));
QByteArray sizeData; QByteArray sizeData;
stream >> sizeData; readStream(stream, sizeData);
if (sizeData.isEmpty()) if (sizeData.isEmpty())
{ {
qWarning("The personal save file is corrupted!"); qWarning("The personal save file is corrupted!");
@ -378,12 +364,11 @@ void SettingsSerializer::readSerialized()
} }
quint64 size = dataToVUint(sizeData); quint64 size = dataToVUint(sizeData);
arrays[array].size = max(size, arrays[array].size); arrays[array].size = max(size, arrays[array].size);
//qDebug()<<"!Array start"<<prefix;
} }
else if (tag == RecordTag::ArrayValue) else if (tag == RecordTag::ArrayValue)
{ {
QByteArray indexData; QByteArray indexData;
stream >> indexData; readStream(stream, indexData);
if (indexData.isEmpty()) if (indexData.isEmpty())
{ {
qWarning("The personal save file is corrupted!"); qWarning("The personal save file is corrupted!");
@ -393,15 +378,13 @@ void SettingsSerializer::readSerialized()
setArrayIndex(index); setArrayIndex(index);
QByteArray key; QByteArray key;
QByteArray value; QByteArray value;
stream >> key; readStream(stream, key);
stream >> value; readStream(stream, value);
setValue(QString::fromUtf8(key), QVariant(QString::fromUtf8(value))); setValue(QString::fromUtf8(key), QVariant(QString::fromUtf8(value)));
//qDebug() << "!Got array key"<<key<<"index"<<index;
} }
else if (tag == RecordTag::ArrayEnd) else if (tag == RecordTag::ArrayEnd)
{ {
endArray(); endArray();
//qDebug() <<"!Array end";
} }
} }
@ -577,9 +560,9 @@ void SettingsSerializer::writePackedVariant(QDataStream& stream, const QVariant&
assert(v.canConvert(QVariant::String)); assert(v.canConvert(QVariant::String));
QString str = v.toString(); QString str = v.toString();
if (str == "true") if (str == "true")
stream << QString("1"); writeStream(stream, QString("1"));
else if (str == "false") else if (str == "false")
stream << QString("0"); writeStream(stream, QString("0"));
else else
stream << str.toUtf8(); writeStream(stream, str.toUtf8());
} }

View File

@ -66,8 +66,8 @@ private:
/// Not followed by any data /// Not followed by any data
ArrayEnd=4, ArrayEnd=4,
}; };
friend QDataStream& operator<<(QDataStream& dataStream, const SettingsSerializer::RecordTag& tag); friend QDataStream& writeStream(QDataStream& dataStream, const SettingsSerializer::RecordTag& tag);
friend QDataStream& operator>>(QDataStream& dataStream, SettingsSerializer::RecordTag& tag); friend QDataStream& readStream(QDataStream& dataStream, SettingsSerializer::RecordTag& tag);
struct Value struct Value
{ {