From f5425fd63ca2b861b9e5d0f04213a581cdcb3614 Mon Sep 17 00:00:00 2001 From: Christian Hoffmann Date: Tue, 30 Aug 2022 21:56:44 +0200 Subject: [PATCH 1/2] Util: Make CLocale::IsCountryCodeSupported more robust On Qt6, we previously relied on the caller to only supply country codes within bounds. With this change we properly check before risking an uninitialized array access. On Qt5, we did not do any checks at all. Now we ensure that we what is provided is a valid Qt5 country code. Related: #2809 --- src/util.cpp | 9 +++++++-- src/util.h | 1 + 2 files changed, 8 insertions(+), 2 deletions(-) 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 }; From 20a25c41dd8dce8d5be593fc41c560160dc29c10 Mon Sep 17 00:00:00 2001 From: Christian Hoffmann Date: Tue, 30 Aug 2022 22:00:08 +0200 Subject: [PATCH 2/2] Server: Fix --serverinfo country code misinterpretation on Qt6 Previously, country codes in --serverinfo were interpreted natively. This worked for Qt5, but caused unintended changes on Qt6 builds as the codes differ. Not doing a conversion for --serverinfo was an oversight from the initial Qt6 compatibility work, which is now fixed with this change. Related: #2299 Fixes: #2809 --- src/serverlist.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) 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 ); } }