Report type mismatch when reading registry values

Instead of asserting, report a user-visible error when trying to use a wrong
key, this should give more information to the developers and users if (when)
things go wrong.

See #16719.
This commit is contained in:
Vadim Zeitlin 2016-01-30 00:42:25 +01:00
parent 3d476369ec
commit 142edf8b78

View File

@ -136,6 +136,33 @@ static wxString GetFullName(const wxRegKey *pKey, const wxString& szValue);
// to NULL
static inline const wxChar *RegValueStr(const wxString& szValue);
// Return the user-readable name of the given REG_XXX type constant.
static wxString GetTypeString(DWORD dwType)
{
#define REG_TYPE_TO_STR(type) case REG_ ## type: return wxS(#type)
switch ( dwType )
{
REG_TYPE_TO_STR(NONE);
REG_TYPE_TO_STR(SZ);
REG_TYPE_TO_STR(EXPAND_SZ);
REG_TYPE_TO_STR(BINARY);
REG_TYPE_TO_STR(DWORD);
// REG_TYPE_TO_STR(DWORD_LITTLE_ENDIAN); -- same as REG_DWORD
REG_TYPE_TO_STR(DWORD_BIG_ENDIAN);
REG_TYPE_TO_STR(LINK);
REG_TYPE_TO_STR(MULTI_SZ);
REG_TYPE_TO_STR(RESOURCE_LIST);
REG_TYPE_TO_STR(FULL_RESOURCE_DESCRIPTOR);
REG_TYPE_TO_STR(RESOURCE_REQUIREMENTS_LIST);
REG_TYPE_TO_STR(QWORD);
// REG_TYPE_TO_STR(QWORD_LITTLE_ENDIAN); -- same as REG_QWORD
default:
return wxString::Format(_("unknown (%lu)"), dwType);
}
}
// ============================================================================
// implementation of wxRegKey class
// ============================================================================
@ -897,13 +924,15 @@ bool wxRegKey::QueryValue(const wxString& szValue, long *plValue) const
GetName().c_str());
return false;
}
else {
// check that we read the value of right type
wxASSERT_MSG( IsNumericValue(szValue),
wxT("Type mismatch in wxRegKey::QueryValue().") );
return true;
// check that we read the value of right type
if ( dwType != REG_DWORD_LITTLE_ENDIAN && dwType != REG_DWORD_BIG_ENDIAN ) {
wxLogError(_("Registry value \"%s\" is not numeric (but of type %s)"),
GetFullName(this, szValue), GetTypeString(dwType));
return false;
}
return true;
}
else
return false;
@ -939,6 +968,12 @@ bool wxRegKey::QueryValue(const wxString& szValue, wxMemoryBuffer& buffer) const
&dwType, NULL, &dwSize);
if ( m_dwLastError == ERROR_SUCCESS ) {
if ( dwType != REG_BINARY ) {
wxLogError(_("Registry value \"%s\" is not binary (but of type %s)"),
GetFullName(this, szValue), GetTypeString(dwType));
return false;
}
if ( dwSize ) {
const RegBinary pBuf = (RegBinary)buffer.GetWriteBuf(dwSize);
m_dwLastError = RegQueryValueEx((HKEY) m_hKey,
@ -981,6 +1016,13 @@ bool wxRegKey::QueryValue(const wxString& szValue,
&dwType, NULL, &dwSize);
if ( m_dwLastError == ERROR_SUCCESS )
{
if ( dwType != REG_SZ && dwType != REG_EXPAND_SZ )
{
wxLogError(_("Registry value \"%s\" is not text (but of type %s)"),
GetFullName(this, szValue), GetTypeString(dwType));
return false;
}
if ( !dwSize )
{
// must treat this case specially as GetWriteBuf() doesn't like
@ -1034,13 +1076,7 @@ bool wxRegKey::QueryValue(const wxString& szValue,
}
if ( m_dwLastError == ERROR_SUCCESS )
{
// check that it was the right type
wxASSERT_MSG( !IsNumericValue(szValue),
wxT("Type mismatch in wxRegKey::QueryValue().") );
return true;
}
}
}