Properly escape bytearray data outside the ascii range when using a codec

Some codecs can't handle the range outside ascii properly and would then
fail to read the data back in correctly.

Task-number: QTBUG-15543
Change-Id: I4c02921e787a939eeec0c7a11603b5896d756aef
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Lars Knoll 2014-06-04 13:52:37 +02:00 committed by The Qt Project
parent a149d2f73e
commit 7df8b1ada4
2 changed files with 28 additions and 2 deletions

View File

@ -696,6 +696,9 @@ void QSettingsPrivate::iniEscapedString(const QString &str, QByteArray &result,
{
bool needsQuotes = false;
bool escapeNextIfDigit = false;
bool useCodec = codec && !str.startsWith(QLatin1String("@ByteArray("))
&& !str.startsWith(QLatin1String("@Variant("));
int i;
int startPos = result.size();
@ -748,12 +751,12 @@ void QSettingsPrivate::iniEscapedString(const QString &str, QByteArray &result,
result += (char)ch;
break;
default:
if (ch <= 0x1F || (ch >= 0x7F && !codec)) {
if (ch <= 0x1F || (ch >= 0x7F && !useCodec)) {
result += "\\x";
result += QByteArray::number(ch, 16);
escapeNextIfDigit = true;
#ifndef QT_NO_TEXTCODEC
} else if (codec) {
} else if (useCodec) {
// slow
result += codec->fromUnicode(str.at(i));
#endif

View File

@ -166,6 +166,7 @@ private slots:
void testByteArray_data();
void testByteArray();
void iniCodec();
private:
const bool m_canWriteNativeSystemSettings;
@ -695,6 +696,28 @@ void tst_QSettings::testByteArray()
}
}
void tst_QSettings::iniCodec()
{
{
QSettings settings("QtProject", "tst_qsettings");
settings.setIniCodec("cp1251");
QByteArray ba;
ba.resize(256);
for (int i = 0; i < ba.size(); i++)
ba[i] = i;
settings.setValue("array",ba);
}
{
QSettings settings("QtProject", "tst_qsettings");
settings.setIniCodec("cp1251");
QByteArray ba = settings.value("array").toByteArray();
QCOMPARE(ba.size(), 256);
for (int i = 0; i < ba.size(); i++)
QCOMPARE((uchar)ba.at(i), (uchar)i);
}
}
void tst_QSettings::testErrorHandling_data()
{
QTest::addColumn<int>("filePerms"); // -1 means file should not exist