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