mirror of
https://github.com/WarmUpTill/SceneSwitcher.git
synced 2026-11-18 07:34:59 -06:00
Fix stack buffer overrun for 512 character window titles
Size the buffer from the converted length instead of fixed 512 size.
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -194,9 +194,11 @@ static std::vector<HWND> getHWNDfromTitle(const std::string &title)
|
||||
HWND hwnd = nullptr;
|
||||
HWND hPrevWnd = nullptr;
|
||||
std::vector<HWND> 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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user