diff options
author | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2016-10-03 18:41:30 +0200 |
---|---|---|
committer | Tor Arne Vestbø <tor.arne.vestbo@theqtcompany.com> | 2016-10-05 11:57:41 +0000 |
commit | 764f5bf48cc87f4c72550b853ab93b815454cd48 (patch) | |
tree | ed8b076c842572290624e049993333dd17a11c72 /src/corelib/io/qsettings_mac.cpp | |
parent | 483aff406a8cd9bb824cbd39a0a20619fe9b1fa2 (diff) |
Apple OS: Handle QSetting strings with embedded zero-bytes
Saving strings with embedded zero-bytes (\0) as CFStrings would
sometimes fail, and only write the part of the string leading
up to the first zero-byte, instead of all the way to the final
zero-terminator. This bug was revealed by the code-path that
falls back to storing e.g. QTime as strings, via the helper
method QSettingsPrivate::variantToString().
We now use the same approach as on platforms such as Windows
and WinRT, where the string produced by variantToString() is
checked for null-bytes, and if so, stored using a binary
representation instead of as a string. For our case that
means we fall back to CFData when detecting the null-byte.
To separate strings from regular byte arrays, new logic has
been added to variantToString() that wraps the null-byte
strings in @String(). That way we can implement a fast-path
when converting back from CFData, that doesn't go via the
slow and lossy conversion via UTF8, and the resulting QVariant
will be of type QVariant::ByteArray. The reason for using
UTF-8 as the binary representation of the string is that
in the case of storing a QByteArray("@foo") we need to
still be able to convert it back to the same byte array,
which doesn't work if the on-disk format is UTF-16.
Task-number: QTBUG-56124
Change-Id: Iab2f71cf96cf3225de48dc5e71870d74b6dde1e8
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/io/qsettings_mac.cpp')
-rw-r--r-- | src/corelib/io/qsettings_mac.cpp | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/src/corelib/io/qsettings_mac.cpp b/src/corelib/io/qsettings_mac.cpp index bc397055ff..77f185d735 100644 --- a/src/corelib/io/qsettings_mac.cpp +++ b/src/corelib/io/qsettings_mac.cpp @@ -214,7 +214,11 @@ static QCFType<CFPropertyListRef> macValue(const QVariant &value) case QVariant::String: string_case: default: - result = QCFString::toCFStringRef(QSettingsPrivate::variantToString(value)); + QString string = QSettingsPrivate::variantToString(value); + if (string.contains(QChar::Null)) + result = string.toUtf8().toCFData(); + else + result = string.toCFString(); } return result; } @@ -266,9 +270,16 @@ static QVariant qtValue(CFPropertyListRef cfvalue) } else if (typeId == CFBooleanGetTypeID()) { return (bool)CFBooleanGetValue(static_cast<CFBooleanRef>(cfvalue)); } else if (typeId == CFDataGetTypeID()) { - CFDataRef cfdata = static_cast<CFDataRef>(cfvalue); - return QByteArray(reinterpret_cast<const char *>(CFDataGetBytePtr(cfdata)), - CFDataGetLength(cfdata)); + QByteArray byteArray = QByteArray::fromRawCFData(static_cast<CFDataRef>(cfvalue)); + + // Fast-path for QByteArray, so that we don't have to go + // though the expensive and lossy conversion via UTF-8. + if (!byteArray.startsWith('@')) + return byteArray; + + const QString str = QString::fromUtf8(byteArray.constData(), byteArray.size()); + return QSettingsPrivate::stringToVariant(str); + } else if (typeId == CFDictionaryGetTypeID()) { CFDictionaryRef cfdict = static_cast<CFDictionaryRef>(cfvalue); CFTypeID arrayTypeId = CFArrayGetTypeID(); |