From f35827de0a15eb442a5629db6739fb2c6bc83f67 Mon Sep 17 00:00:00 2001 From: WarmUpTill <19472752+WarmUpTill@users.noreply.github.com> Date: Tue, 17 Nov 2026 21:15:47 +0100 Subject: [PATCH] Fix stack buffer overrun for 512 character window titles Size the buffer from the converted length instead of fixed 512 size. --- lib/win/advanced-scene-switcher-win.cpp | 9 ++++----- plugins/base/utils/windows/windows.cpp | 8 +++++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/win/advanced-scene-switcher-win.cpp b/lib/win/advanced-scene-switcher-win.cpp index eb5ea85f..06d49756 100644 --- a/lib/win/advanced-scene-switcher-win.cpp +++ b/lib/win/advanced-scene-switcher-win.cpp @@ -309,11 +309,10 @@ std::string GetCurrentWindowTitle() static HWND getHWNDfromTitle(const std::string &title) { - HWND hwnd = NULL; - wchar_t wTitle[512]; - os_utf8_to_wcs(title.c_str(), 0, wTitle, 512); - hwnd = FindWindowEx(NULL, NULL, NULL, wTitle); - return hwnd; + size_t len = os_utf8_to_wcs(title.c_str(), 0, nullptr, 0); + std::wstring wTitle(len, L'\0'); + os_utf8_to_wcs(title.c_str(), 0, wTitle.data(), len + 1); + return FindWindowEx(NULL, NULL, NULL, wTitle.c_str()); } static std::wstring GetControlText(HWND hwnd, IUIAutomationElement *element) diff --git a/plugins/base/utils/windows/windows.cpp b/plugins/base/utils/windows/windows.cpp index b7699413..ec3c7928 100644 --- a/plugins/base/utils/windows/windows.cpp +++ b/plugins/base/utils/windows/windows.cpp @@ -194,9 +194,11 @@ static std::vector getHWNDfromTitle(const std::string &title) HWND hwnd = nullptr; HWND hPrevWnd = nullptr; std::vector hwnds; - wchar_t wTitle[512]; - os_utf8_to_wcs(title.c_str(), 0, wTitle, 512); - while ((hwnd = FindWindowEx(NULL, hPrevWnd, NULL, wTitle)) != nullptr) { + size_t len = os_utf8_to_wcs(title.c_str(), 0, nullptr, 0); + std::wstring wTitle(len, L'\0'); + os_utf8_to_wcs(title.c_str(), 0, wTitle.data(), len + 1); + while ((hwnd = FindWindowEx(NULL, hPrevWnd, NULL, wTitle.c_str())) != + nullptr) { if (windowIsValid(hwnd)) { hwnds.emplace_back(hwnd); }