Windows: Fix registry string read failures

On Windows 7, it has been observed that the time zone registry key
is a string of size 256 padded with 0. Use QString::fromWCharArray(),
relying on 0-termination to cope with it.

Pick-to: 5.15
Fixes: QTBUG-84455
Change-Id: I5d242e2de73c1ea09344aee8de8eea941bc52bab
Reviewed-by: Oliver Wolff <oliver.wolff@qt.io>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Friedemann Kleint 2020-06-10 10:12:29 +02:00
parent 60f83a7675
commit dee140a79a

View File

@ -86,17 +86,15 @@ QString QWinRegistryKey::stringValue(QStringView subKey) const
|| (type != REG_SZ && type != REG_EXPAND_SZ) || size <= 2) {
return result;
}
// Reserve more for rare cases where trailing '\0' are missing in registry,
// otherwise chop off the '\0' received.
QString buffer(int(size / sizeof(wchar_t)), Qt::Uninitialized);
if (RegQueryValueEx(m_key, subKeyC, nullptr, &type,
reinterpret_cast<LPBYTE>(buffer.data()), &size) == ERROR_SUCCESS) {
if (buffer.endsWith(QChar::Null))
buffer.chop(1);
} else {
buffer.clear();
}
return buffer;
// Reserve more for rare cases where trailing '\0' are missing in registry.
// Rely on 0-termination since strings of size 256 padded with 0 have been
// observed (QTBUG-84455).
size += 2;
QVarLengthArray<unsigned char> buffer(static_cast<int>(size));
std::fill(buffer.data(), buffer.data() + size, 0u);
if (RegQueryValueEx(m_key, subKeyC, nullptr, &type, buffer.data(), &size) == ERROR_SUCCESS)
result = QString::fromWCharArray(reinterpret_cast<const wchar_t *>(buffer.constData()));
return result;
}
QPair<DWORD, bool> QWinRegistryKey::dwordValue(QStringView subKey) const