From 142edf8b7821d26875e2a122f76a4a8136be981c Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 30 Jan 2016 00:42:25 +0100 Subject: [PATCH] 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. --- src/msw/registry.cpp | 58 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 11 deletions(-) diff --git a/src/msw/registry.cpp b/src/msw/registry.cpp index c3605a7a5d..707e343399 100644 --- a/src/msw/registry.cpp +++ b/src/msw/registry.cpp @@ -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; - } } }