diff --git a/src/serverlist.cpp b/src/serverlist.cpp index 32c7d628ee..e03b6f092b 100644 --- a/src/serverlist.cpp +++ b/src/serverlist.cpp @@ -205,9 +205,15 @@ CServerListManager::CServerListManager ( const quint16 iNPortNum, // [this server country as QLocale ID] bool ok; const int iCountry = slServInfoSeparateParams[2].toInt ( &ok ); - if ( ok && iCountry >= 0 && iCountry <= QLocale::LastCountry ) + if ( ok && iCountry >= 0 && CLocale::IsCountryCodeSupported ( iCountry ) ) { - ThisServerListEntry.eCountry = static_cast ( iCountry ); + // Convert from externally-supplied format ("wire format", Qt5 codes) to + // native format. On Qt5 builds, this is a noop, on Qt6 builds, a conversion + // takes place. + // We try to do such conversions at the outer-most interface which is capable of doing it. + // Although the value comes from src/main -> src/server, this very place is + // the first where we have access to the parsed country code: + ThisServerListEntry.eCountry = CLocale::WireFormatCountryCodeToQtCountry ( iCountry ); } } diff --git a/src/util.cpp b/src/util.cpp index 3a6b48f076..6481c2e90a 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1423,11 +1423,16 @@ bool CLocale::IsCountryCodeSupported ( unsigned short iCountryCode ) #if QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) // On newer Qt versions there might be codes which do not have a Qt5 equivalent. // We have no way to support those sanely right now. + // Before we can check that via an array lookup, we have to ensure that + // we are within the boundaries of that array: + if ( iCountryCode >= qt6CountryToWireFormatLen ) + { + return false; + } return qt6CountryToWireFormat[iCountryCode] != -1; #else // All Qt5 codes are supported. - Q_UNUSED ( iCountryCode ); - return true; + return iCountryCode <= QLocale::LastCountry; #endif } diff --git a/src/util.h b/src/util.h index 6a828b5b83..8ed8725fff 100644 --- a/src/util.h +++ b/src/util.h @@ -853,6 +853,7 @@ class CLocale 195, 196, 114, 254, 197, 198, 201, 202, 203, 205, 206, 207, 208, 209, 210, 211, 62, 212, 213, 214, 215, 253, 216, 217, 218, 219, 220, 221, 222, 223, 224, 226, 225, 234, 227, 228, 229, 230, 231, 232, 235, 236, 260, 237, 239, 240, }; + constexpr int const static qt6CountryToWireFormatLen = sizeof ( qt6CountryToWireFormat ) / sizeof ( qt6CountryToWireFormat[0] ); #endif };