From b04df162ae5b36598db0d26d92ea28aaba49323c Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Thu, 12 Jun 2025 13:36:16 +0200 Subject: [PATCH] Common: Require explicit handle validation Using operator bool() hid ambiguity when handle was polymorfic with bool. Using operator!() reqired !! to test for validity which results in awkward code. Signed-off-by: Simon Rozman --- include/WinStd/COM.h | 6 +++--- include/WinStd/Win.h | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/WinStd/COM.h b/include/WinStd/COM.h index f7cb5da3..ba8429be 100644 --- a/include/WinStd/COM.h +++ b/include/WinStd/COM.h @@ -1527,7 +1527,7 @@ namespace winstd }; sa = SafeArrayCreate(vt, 2, dim); } - if (!sa) + if (!sa.valid()) throw std::bad_alloc(); size_t elem_size; @@ -1581,7 +1581,7 @@ namespace winstd }; sa = SafeArrayCreate(VT_VARIANT, 2, dim); } - if (!sa) + if (!sa.valid()) throw std::bad_alloc(); // Support VARIANT types that may be used for SAFEARRAY @@ -1707,7 +1707,7 @@ namespace winstd // Allocate. size_t pallete_size = sizeof(RGBQUAD) * bmh.biClrUsed; safearray sa = SafeArrayCreateVector(VT_UI1, 0, static_cast(sizeof(BITMAPFILEHEADER) + sizeof(bmh) + pallete_size + bmh.biSizeImage)); - if (!sa) + if (!sa.valid()) throw std::bad_alloc(); // Locate BITMAPFILEHEADER, BITMAPINFO and pixel map. diff --git a/include/WinStd/Win.h b/include/WinStd/Win.h index 3d4f0147..ac6d04cd 100644 --- a/include/WinStd/Win.h +++ b/include/WinStd/Win.h @@ -1562,7 +1562,7 @@ namespace winstd if (!AdjustTokenPrivileges(thread_token, FALSE, &privileges, sizeof(privileges), NULL, NULL)) goto revert; process_snapshot process_snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); - if (!process_snapshot) + if (!process_snapshot.valid()) goto revert; PROCESSENTRY32 entry = { sizeof(PROCESSENTRY32) }; if (!Process32First(process_snapshot, &entry)) @@ -1571,7 +1571,7 @@ namespace winstd if (!Process32Next(process_snapshot, &entry)) goto revert; process winlogon_process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, entry.th32ProcessID); - if (!winlogon_process) + if (!winlogon_process.valid()) goto revert; if (!OpenProcessToken(winlogon_process, TOKEN_IMPERSONATE | TOKEN_DUPLICATE, &h)) goto revert; @@ -2033,7 +2033,7 @@ namespace winstd { std::wstring wstr; winstd::library ntdll(LoadLibraryW(L"NTDLL.DLL")); - if (ntdll && FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS, (HMODULE)ntdll, num, dwLanguageId, wstr, NULL)) { + if (ntdll.valid() && FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS, (HMODULE)ntdll, num, dwLanguageId, wstr, NULL)) { // Stock Windows error messages contain CRLF. Well... Trim all the trailing white space. wstr.erase(wstr.find_last_not_of(L" \t\n\r\f\v") + 1); } else