From 2674d592dbe2ab5e666185be097687ab9e291aa2 Mon Sep 17 00:00:00 2001 From: Sam Heybey Date: Thu, 9 Feb 2023 11:33:57 -0500 Subject: [PATCH 01/11] use AssocQueryString instead of directly querying the registry --- src/util/IncognitoBrowser.cpp | 91 +++++++++++++++++++++++++++-------- 1 file changed, 72 insertions(+), 19 deletions(-) diff --git a/src/util/IncognitoBrowser.cpp b/src/util/IncognitoBrowser.cpp index 0092d328423..b0cf8da32fe 100644 --- a/src/util/IncognitoBrowser.cpp +++ b/src/util/IncognitoBrowser.cpp @@ -2,14 +2,21 @@ #include #include -#include #include +#if defined(Q_OS_WIN) and defined(USEWINSDK) +# include +# include + +typedef HRESULT(CALLBACK *AssocQueryString_)(ASSOCF, ASSOCSTR, LPCWSTR, LPCWSTR, + LPWSTR, DWORD *); +#endif + namespace { using namespace chatterino; -#ifdef Q_OS_WIN +#if defined(Q_OS_WIN) and defined(USEWINSDK) QString injectPrivateSwitch(QString command) { // list of command line switches to turn on private browsing in browsers @@ -45,25 +52,71 @@ QString injectPrivateSwitch(QString command) return QString(); } +QString callAssocQueryString(ASSOCF flags, ASSOCSTR str, LPCWSTR pszAssoc, + LPCWSTR pszExtra) +{ + static HINSTANCE shlwapi = LoadLibrary(L"shlwapi"); + static auto assocQueryString = AssocQueryString_( + (shlwapi == NULL) ? NULL + : GetProcAddress(shlwapi, "AssocQueryStringW")); + + if (assocQueryString == NULL) + { + return QString(); + } + + // always error out instead of returning a truncated string when the + // buffer is too small - avoids race condition when the user changes their + // default browser between calls to AssocQueryString + flags |= ASSOCF_NOTRUNCATE; + + DWORD resultSize = 0; + assocQueryString(flags, str, pszAssoc, pszExtra, NULL, &resultSize); + if (resultSize == 0) + { + return QString(); + } + + QString result; + auto buf = new TCHAR[resultSize]; + if (SUCCEEDED( + assocQueryString(flags, str, pszAssoc, pszExtra, buf, &resultSize))) + { + result = QString::fromWCharArray(buf, resultSize); + } + delete[] buf; + return result; +} + QString getCommand() { - // get default browser prog id - auto browserId = QSettings("HKEY_CURRENT_" - "USER\\Software\\Microsoft\\Windows\\Shell\\" - "Associations\\UrlAssociatio" - "ns\\http\\UserChoice", - QSettings::NativeFormat) - .value("Progid") - .toString(); - - // get default browser start command - auto command = - QSettings("HKEY_CLASSES_ROOT\\" + browserId + "\\shell\\open\\command", - QSettings::NativeFormat) - .value("Default") - .toString(); + // get default browser start command, by protocol if possible, falling back to extension if not + QString command; + + // ASSOCF_IS_PROTOCOL was introduced in Windows 8 + if (IsWindows8OrGreater()) + { + command = callAssocQueryString(ASSOCF_IS_PROTOCOL | ASSOCF_VERIFY, + ASSOCSTR_COMMAND, L"http", NULL); + } + + if (command.isNull()) + { + // failed to fetch default browser by protocol, try by file extension instead + command = callAssocQueryString(ASSOCF_VERIFY, ASSOCSTR_COMMAND, + L".html", NULL); + } + + if (command.isNull()) + { + // also try the equivalent .htm extension + command = callAssocQueryString(ASSOCF_VERIFY, ASSOCSTR_COMMAND, L".htm", + NULL); + } + if (command.isNull()) { + // failed to find browser command return QString(); } @@ -84,7 +137,7 @@ namespace chatterino { bool supportsIncognitoLinks() { -#ifdef Q_OS_WIN +#if defined(Q_OS_WIN) and defined(USEWINSDK) return !getCommand().isNull(); #else return false; @@ -93,7 +146,7 @@ bool supportsIncognitoLinks() bool openLinkIncognito(const QString &link) { -#ifdef Q_OS_WIN +#if defined(Q_OS_WIN) and defined(USEWINSDK) auto command = getCommand(); // TODO: split command into program path and incognito argument From 80ba93287018a15ff1fddab235ac6fc20b3bd3e3 Mon Sep 17 00:00:00 2001 From: Sam Heybey Date: Thu, 9 Feb 2023 12:04:42 -0500 Subject: [PATCH 02/11] update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e6e0ecd5073..37d607ea706 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ - Bugfix: Fixed an issue on Windows when opening links in incognito mode that contained forward slashes in hash (#4307) - Bugfix: Fixed an issue where beta versions wouldn't update to stable versions correctly. (#4329) - Bugfix: Avoided crash that could occur when receiving channel point reward information. (#4360) +- Bugfix: Don't rely on undocumented registry keys to find the default browser on Windows. (#4362) - Dev: Changed sound backend from Qt to miniaudio. (#4334) - Dev: Remove protocol from QApplication's Organization Domain (so changed from `https://www.chatterino.com` to `chatterino.com`). (#4256) - Dev: Ignore `WM_SHOWWINDOW` hide events, causing fewer attempted rescales. (#4198) From 4dba2604cf37f21b64ebb934342ad40cffa06df1 Mon Sep 17 00:00:00 2001 From: Sam Heybey Date: Thu, 9 Feb 2023 15:35:08 -0500 Subject: [PATCH 03/11] move windows-specific code for opening incognito links to WindowsHelper.cpp --- src/util/IncognitoBrowser.cpp | 65 +++++------------------------------ src/util/WindowsHelper.cpp | 53 ++++++++++++++++++++++++++++ src/util/WindowsHelper.hpp | 7 ++++ 3 files changed, 68 insertions(+), 57 deletions(-) diff --git a/src/util/IncognitoBrowser.cpp b/src/util/IncognitoBrowser.cpp index b0cf8da32fe..e6bcad033a6 100644 --- a/src/util/IncognitoBrowser.cpp +++ b/src/util/IncognitoBrowser.cpp @@ -4,19 +4,15 @@ #include #include -#if defined(Q_OS_WIN) and defined(USEWINSDK) -# include -# include - -typedef HRESULT(CALLBACK *AssocQueryString_)(ASSOCF, ASSOCSTR, LPCWSTR, LPCWSTR, - LPWSTR, DWORD *); +#ifdef USEWINSDK +# include "WindowsHelper.hpp" #endif namespace { using namespace chatterino; -#if defined(Q_OS_WIN) and defined(USEWINSDK) +#ifdef USEWINSDK QString injectPrivateSwitch(QString command) { // list of command line switches to turn on private browsing in browsers @@ -52,66 +48,21 @@ QString injectPrivateSwitch(QString command) return QString(); } -QString callAssocQueryString(ASSOCF flags, ASSOCSTR str, LPCWSTR pszAssoc, - LPCWSTR pszExtra) -{ - static HINSTANCE shlwapi = LoadLibrary(L"shlwapi"); - static auto assocQueryString = AssocQueryString_( - (shlwapi == NULL) ? NULL - : GetProcAddress(shlwapi, "AssocQueryStringW")); - - if (assocQueryString == NULL) - { - return QString(); - } - - // always error out instead of returning a truncated string when the - // buffer is too small - avoids race condition when the user changes their - // default browser between calls to AssocQueryString - flags |= ASSOCF_NOTRUNCATE; - - DWORD resultSize = 0; - assocQueryString(flags, str, pszAssoc, pszExtra, NULL, &resultSize); - if (resultSize == 0) - { - return QString(); - } - - QString result; - auto buf = new TCHAR[resultSize]; - if (SUCCEEDED( - assocQueryString(flags, str, pszAssoc, pszExtra, buf, &resultSize))) - { - result = QString::fromWCharArray(buf, resultSize); - } - delete[] buf; - return result; -} - QString getCommand() { // get default browser start command, by protocol if possible, falling back to extension if not - QString command; - - // ASSOCF_IS_PROTOCOL was introduced in Windows 8 - if (IsWindows8OrGreater()) - { - command = callAssocQueryString(ASSOCF_IS_PROTOCOL | ASSOCF_VERIFY, - ASSOCSTR_COMMAND, L"http", NULL); - } + QString command = getAssociatedCommand(AQT_PROTOCOL, L"http"); if (command.isNull()) { // failed to fetch default browser by protocol, try by file extension instead - command = callAssocQueryString(ASSOCF_VERIFY, ASSOCSTR_COMMAND, - L".html", NULL); + command = getAssociatedCommand(AQT_FILE_EXTENSION, L".html"); } if (command.isNull()) { // also try the equivalent .htm extension - command = callAssocQueryString(ASSOCF_VERIFY, ASSOCSTR_COMMAND, L".htm", - NULL); + command = getAssociatedCommand(AQT_FILE_EXTENSION, L".htm"); } if (command.isNull()) @@ -137,7 +88,7 @@ namespace chatterino { bool supportsIncognitoLinks() { -#if defined(Q_OS_WIN) and defined(USEWINSDK) +#ifdef USEWINSDK return !getCommand().isNull(); #else return false; @@ -146,7 +97,7 @@ bool supportsIncognitoLinks() bool openLinkIncognito(const QString &link) { -#if defined(Q_OS_WIN) and defined(USEWINSDK) +#ifdef USEWINSDK auto command = getCommand(); // TODO: split command into program path and incognito argument diff --git a/src/util/WindowsHelper.cpp b/src/util/WindowsHelper.cpp index 06fc0dd798c..70490a77d2d 100644 --- a/src/util/WindowsHelper.cpp +++ b/src/util/WindowsHelper.cpp @@ -6,6 +6,9 @@ #ifdef USEWINSDK +# include +# include + namespace chatterino { typedef enum MONITOR_DPI_TYPE { @@ -17,6 +20,8 @@ typedef enum MONITOR_DPI_TYPE { typedef HRESULT(CALLBACK *GetDpiForMonitor_)(HMONITOR, MONITOR_DPI_TYPE, UINT *, UINT *); +typedef HRESULT(CALLBACK *AssocQueryString_)(ASSOCF, ASSOCSTR, LPCWSTR, LPCWSTR, + LPWSTR, DWORD *); boost::optional getWindowDpi(HWND hwnd) { @@ -83,6 +88,54 @@ void setRegisteredForStartup(bool isRegistered) } } +QString getAssociatedCommand(ASSOCIATION_QUERY_TYPE queryType, LPCWSTR query) +{ + static HINSTANCE shlwapi = LoadLibrary(L"shlwapi"); + static auto assocQueryString = AssocQueryString_( + (shlwapi == NULL) ? NULL + : GetProcAddress(shlwapi, "AssocQueryStringW")); + + if (assocQueryString == NULL) + { + return QString(); + } + + // always error out instead of returning a truncated string when the + // buffer is too small - avoids race condition when the user changes their + // default browser between calls to AssocQueryString + auto flags = ASSOCF_VERIFY | ASSOCF_NOTRUNCATE; + + if (queryType == AQT_PROTOCOL) + { + // ASSOCF_IS_PROTOCOL was introduced in Windows 8 + if (IsWindows8OrGreater()) + { + flags |= ASSOCF_IS_PROTOCOL; + } + else + { + return QString(); + } + } + + DWORD resultSize = 0; + assocQueryString(flags, ASSOCSTR_COMMAND, query, NULL, NULL, &resultSize); + if (resultSize == 0) + { + return QString(); + } + + QString result; + auto buf = new TCHAR[resultSize]; + if (SUCCEEDED(assocQueryString(flags, ASSOCSTR_COMMAND, query, NULL, buf, + &resultSize))) + { + result = QString::fromWCharArray(buf, resultSize); + } + delete[] buf; + return result; +} + } // namespace chatterino #endif diff --git a/src/util/WindowsHelper.hpp b/src/util/WindowsHelper.hpp index f39e66512e8..174244ba242 100644 --- a/src/util/WindowsHelper.hpp +++ b/src/util/WindowsHelper.hpp @@ -7,12 +7,19 @@ namespace chatterino { +typedef enum ASSOCIATION_QUERY_TYPE { + AQT_PROTOCOL, + AQT_FILE_EXTENSION +} ASSOCIATION_QUERY_TYPE; + boost::optional getWindowDpi(HWND hwnd); void flushClipboard(); bool isRegisteredForStartup(); void setRegisteredForStartup(bool isRegistered); +QString getAssociatedCommand(ASSOCIATION_QUERY_TYPE queryType, LPCWSTR query); + } // namespace chatterino #endif From 6be7c8a11b41e833f7f6750c2e7c1f974bc56806 Mon Sep 17 00:00:00 2001 From: Sam Heybey Date: Thu, 9 Feb 2023 15:35:23 -0500 Subject: [PATCH 04/11] update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 37d607ea706..19ec22bbdd6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,7 +30,6 @@ - Bugfix: Fixed an issue on Windows when opening links in incognito mode that contained forward slashes in hash (#4307) - Bugfix: Fixed an issue where beta versions wouldn't update to stable versions correctly. (#4329) - Bugfix: Avoided crash that could occur when receiving channel point reward information. (#4360) -- Bugfix: Don't rely on undocumented registry keys to find the default browser on Windows. (#4362) - Dev: Changed sound backend from Qt to miniaudio. (#4334) - Dev: Remove protocol from QApplication's Organization Domain (so changed from `https://www.chatterino.com` to `chatterino.com`). (#4256) - Dev: Ignore `WM_SHOWWINDOW` hide events, causing fewer attempted rescales. (#4198) @@ -48,6 +47,7 @@ - Dev: Added CMake Install Support on Windows. (#4300) - Dev: Changed conan generator to [`CMakeDeps`](https://docs.conan.io/en/latest/reference/conanfile/tools/cmake/cmakedeps.html) and [`CMakeToolchain`](https://docs.conan.io/en/latest/reference/conanfile/tools/cmake/cmaketoolchain.html). See PR for migration notes. (#4335) - Dev: Refactored 7TV EventAPI implementation. (#4342) +- Dev: Don't rely on undocumented registry keys to find the default browser on Windows. (#4362) ## 2.4.0 From 7f11f21fbd81a703a83b174fce03a19b3a58000a Mon Sep 17 00:00:00 2001 From: Sam Heybey Date: Thu, 9 Feb 2023 23:00:52 -0500 Subject: [PATCH 05/11] getAssociatedCommand() style fixes - use enum class instead of typedef and correct naming style for AssociationQueryType - use correct include path and order for local include - don't use unnecessary ASSOCF_VERIFY flag - check return code of both AssocQueryStringW calls - use wchar_t instead of TCHAR alias --- src/util/IncognitoBrowser.cpp | 16 +++++++++------- src/util/WindowsHelper.cpp | 13 +++++++------ src/util/WindowsHelper.hpp | 7 ++----- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/util/IncognitoBrowser.cpp b/src/util/IncognitoBrowser.cpp index e6bcad033a6..074f07c1b9e 100644 --- a/src/util/IncognitoBrowser.cpp +++ b/src/util/IncognitoBrowser.cpp @@ -1,13 +1,12 @@ #include "util/IncognitoBrowser.hpp" +#ifdef USEWINSDK +# include "util/WindowsHelper.hpp" +#endif #include #include #include -#ifdef USEWINSDK -# include "WindowsHelper.hpp" -#endif - namespace { using namespace chatterino; @@ -51,18 +50,21 @@ QString injectPrivateSwitch(QString command) QString getCommand() { // get default browser start command, by protocol if possible, falling back to extension if not - QString command = getAssociatedCommand(AQT_PROTOCOL, L"http"); + QString command = + getAssociatedCommand(AssociationQueryType::Protocol, L"http"); if (command.isNull()) { // failed to fetch default browser by protocol, try by file extension instead - command = getAssociatedCommand(AQT_FILE_EXTENSION, L".html"); + command = + getAssociatedCommand(AssociationQueryType::FileExtension, L".html"); } if (command.isNull()) { // also try the equivalent .htm extension - command = getAssociatedCommand(AQT_FILE_EXTENSION, L".htm"); + command = + getAssociatedCommand(AssociationQueryType::FileExtension, L".htm"); } if (command.isNull()) diff --git a/src/util/WindowsHelper.cpp b/src/util/WindowsHelper.cpp index 70490a77d2d..c6e8aa2caf1 100644 --- a/src/util/WindowsHelper.cpp +++ b/src/util/WindowsHelper.cpp @@ -88,7 +88,7 @@ void setRegisteredForStartup(bool isRegistered) } } -QString getAssociatedCommand(ASSOCIATION_QUERY_TYPE queryType, LPCWSTR query) +QString getAssociatedCommand(AssociationQueryType queryType, LPCWSTR query) { static HINSTANCE shlwapi = LoadLibrary(L"shlwapi"); static auto assocQueryString = AssocQueryString_( @@ -103,9 +103,9 @@ QString getAssociatedCommand(ASSOCIATION_QUERY_TYPE queryType, LPCWSTR query) // always error out instead of returning a truncated string when the // buffer is too small - avoids race condition when the user changes their // default browser between calls to AssocQueryString - auto flags = ASSOCF_VERIFY | ASSOCF_NOTRUNCATE; + ASSOCF flags = ASSOCF_NOTRUNCATE; - if (queryType == AQT_PROTOCOL) + if (queryType == AssociationQueryType::Protocol) { // ASSOCF_IS_PROTOCOL was introduced in Windows 8 if (IsWindows8OrGreater()) @@ -119,14 +119,15 @@ QString getAssociatedCommand(ASSOCIATION_QUERY_TYPE queryType, LPCWSTR query) } DWORD resultSize = 0; - assocQueryString(flags, ASSOCSTR_COMMAND, query, NULL, NULL, &resultSize); - if (resultSize == 0) + if (!SUCCEEDED(assocQueryString(flags, ASSOCSTR_COMMAND, query, NULL, NULL, + &resultSize)) || + resultSize == 0) { return QString(); } QString result; - auto buf = new TCHAR[resultSize]; + auto buf = new wchar_t[resultSize]; if (SUCCEEDED(assocQueryString(flags, ASSOCSTR_COMMAND, query, NULL, buf, &resultSize))) { diff --git a/src/util/WindowsHelper.hpp b/src/util/WindowsHelper.hpp index 174244ba242..478368b81b3 100644 --- a/src/util/WindowsHelper.hpp +++ b/src/util/WindowsHelper.hpp @@ -7,10 +7,7 @@ namespace chatterino { -typedef enum ASSOCIATION_QUERY_TYPE { - AQT_PROTOCOL, - AQT_FILE_EXTENSION -} ASSOCIATION_QUERY_TYPE; +enum class AssociationQueryType { Protocol, FileExtension }; boost::optional getWindowDpi(HWND hwnd); void flushClipboard(); @@ -18,7 +15,7 @@ void flushClipboard(); bool isRegisteredForStartup(); void setRegisteredForStartup(bool isRegistered); -QString getAssociatedCommand(ASSOCIATION_QUERY_TYPE queryType, LPCWSTR query); +QString getAssociatedCommand(AssociationQueryType queryType, LPCWSTR query); } // namespace chatterino From e5d40f6f684012e38346c6683eb8cba3e713974c Mon Sep 17 00:00:00 2001 From: Sam Heybey Date: Fri, 10 Feb 2023 08:04:59 -0500 Subject: [PATCH 06/11] use FAILED instead of !SUCCEEDED --- src/util/WindowsHelper.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/util/WindowsHelper.cpp b/src/util/WindowsHelper.cpp index c6e8aa2caf1..f01ab2c4691 100644 --- a/src/util/WindowsHelper.cpp +++ b/src/util/WindowsHelper.cpp @@ -119,8 +119,8 @@ QString getAssociatedCommand(AssociationQueryType queryType, LPCWSTR query) } DWORD resultSize = 0; - if (!SUCCEEDED(assocQueryString(flags, ASSOCSTR_COMMAND, query, NULL, NULL, - &resultSize)) || + if (FAILED(assocQueryString(flags, ASSOCSTR_COMMAND, query, NULL, NULL, + &resultSize)) || resultSize == 0) { return QString(); From 434e513a9830672b5f6b4101cadc5c000d0611d5 Mon Sep 17 00:00:00 2001 From: Sam Heybey Date: Fri, 10 Feb 2023 15:25:40 -0500 Subject: [PATCH 07/11] return early if LoadLibrary fails --- src/util/WindowsHelper.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/util/WindowsHelper.cpp b/src/util/WindowsHelper.cpp index f01ab2c4691..6ad9378ec50 100644 --- a/src/util/WindowsHelper.cpp +++ b/src/util/WindowsHelper.cpp @@ -91,10 +91,13 @@ void setRegisteredForStartup(bool isRegistered) QString getAssociatedCommand(AssociationQueryType queryType, LPCWSTR query) { static HINSTANCE shlwapi = LoadLibrary(L"shlwapi"); - static auto assocQueryString = AssocQueryString_( - (shlwapi == NULL) ? NULL - : GetProcAddress(shlwapi, "AssocQueryStringW")); + if (shlwapi == NULL) + { + return QString(); + } + static auto assocQueryString = + AssocQueryString_(GetProcAddress(shlwapi, "AssocQueryStringW")); if (assocQueryString == NULL) { return QString(); From 75b6c3a9284f074f7a3d8eaab158f663b4bba026 Mon Sep 17 00:00:00 2001 From: Sam Heybey Date: Fri, 10 Feb 2023 15:52:33 -0500 Subject: [PATCH 08/11] fix off-by-one error in getAssociatedCommand() --- src/util/WindowsHelper.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/util/WindowsHelper.cpp b/src/util/WindowsHelper.cpp index 6ad9378ec50..7268796e899 100644 --- a/src/util/WindowsHelper.cpp +++ b/src/util/WindowsHelper.cpp @@ -134,7 +134,10 @@ QString getAssociatedCommand(AssociationQueryType queryType, LPCWSTR query) if (SUCCEEDED(assocQueryString(flags, ASSOCSTR_COMMAND, query, NULL, buf, &resultSize))) { - result = QString::fromWCharArray(buf, resultSize); + // QString::fromWCharArray expects the length in characters *not + // including* the null terminator, but AssocQueryStringW calculates + // length including the null terminator + result = QString::fromWCharArray(buf, resultSize - 1); } delete[] buf; return result; From 9b481dcbde69d34643eaf4ef715ef842bfbb18cf Mon Sep 17 00:00:00 2001 From: Sam Heybey Date: Fri, 10 Feb 2023 16:10:37 -0500 Subject: [PATCH 09/11] return early if association is the empty string This avoids returning an empty but not null QString if the stored association has an empty command. --- src/util/WindowsHelper.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/util/WindowsHelper.cpp b/src/util/WindowsHelper.cpp index 7268796e899..64042fb69a4 100644 --- a/src/util/WindowsHelper.cpp +++ b/src/util/WindowsHelper.cpp @@ -124,7 +124,9 @@ QString getAssociatedCommand(AssociationQueryType queryType, LPCWSTR query) DWORD resultSize = 0; if (FAILED(assocQueryString(flags, ASSOCSTR_COMMAND, query, NULL, NULL, &resultSize)) || - resultSize == 0) + // resultSize includes the null terminator. if resultSize is 1, the + // returned value would be the empty string. + resultSize == 0 || resultSize == 1) { return QString(); } From 96633eb8a719a3d7f3809c145c0e6f69e5b426b4 Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Fri, 10 Feb 2023 23:05:48 +0100 Subject: [PATCH 10/11] Check resultSize <= 1 instead of == 0 || == 1 Also split up the FAILED check & resultSize checks into separate if-statements --- src/util/WindowsHelper.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/util/WindowsHelper.cpp b/src/util/WindowsHelper.cpp index 64042fb69a4..187aa4de593 100644 --- a/src/util/WindowsHelper.cpp +++ b/src/util/WindowsHelper.cpp @@ -123,11 +123,15 @@ QString getAssociatedCommand(AssociationQueryType queryType, LPCWSTR query) DWORD resultSize = 0; if (FAILED(assocQueryString(flags, ASSOCSTR_COMMAND, query, NULL, NULL, - &resultSize)) || + &resultSize))) + { + return QString(); + } + + if (resultSize <= 1) + { // resultSize includes the null terminator. if resultSize is 1, the // returned value would be the empty string. - resultSize == 0 || resultSize == 1) - { return QString(); } From 90ec75777248909f546b0be020bfc215158cfd3f Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Fri, 10 Feb 2023 23:11:09 +0100 Subject: [PATCH 11/11] Replace `NULL`s with `nullptr` --- src/util/WindowsHelper.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/util/WindowsHelper.cpp b/src/util/WindowsHelper.cpp index 187aa4de593..d46d29158ad 100644 --- a/src/util/WindowsHelper.cpp +++ b/src/util/WindowsHelper.cpp @@ -91,14 +91,14 @@ void setRegisteredForStartup(bool isRegistered) QString getAssociatedCommand(AssociationQueryType queryType, LPCWSTR query) { static HINSTANCE shlwapi = LoadLibrary(L"shlwapi"); - if (shlwapi == NULL) + if (shlwapi == nullptr) { return QString(); } static auto assocQueryString = AssocQueryString_(GetProcAddress(shlwapi, "AssocQueryStringW")); - if (assocQueryString == NULL) + if (assocQueryString == nullptr) { return QString(); } @@ -122,8 +122,8 @@ QString getAssociatedCommand(AssociationQueryType queryType, LPCWSTR query) } DWORD resultSize = 0; - if (FAILED(assocQueryString(flags, ASSOCSTR_COMMAND, query, NULL, NULL, - &resultSize))) + if (FAILED(assocQueryString(flags, ASSOCSTR_COMMAND, query, nullptr, + nullptr, &resultSize))) { return QString(); } @@ -137,7 +137,7 @@ QString getAssociatedCommand(AssociationQueryType queryType, LPCWSTR query) QString result; auto buf = new wchar_t[resultSize]; - if (SUCCEEDED(assocQueryString(flags, ASSOCSTR_COMMAND, query, NULL, buf, + if (SUCCEEDED(assocQueryString(flags, ASSOCSTR_COMMAND, query, nullptr, buf, &resultSize))) { // QString::fromWCharArray expects the length in characters *not