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 <simon@rozman.si>
This commit is contained in:
Simon Rozman 2025-06-12 13:36:16 +02:00
parent d9a719ddb8
commit b04df162ae
2 changed files with 6 additions and 6 deletions

View File

@ -1527,7 +1527,7 @@ namespace winstd
}; };
sa = SafeArrayCreate(vt, 2, dim); sa = SafeArrayCreate(vt, 2, dim);
} }
if (!sa) if (!sa.valid())
throw std::bad_alloc(); throw std::bad_alloc();
size_t elem_size; size_t elem_size;
@ -1581,7 +1581,7 @@ namespace winstd
}; };
sa = SafeArrayCreate(VT_VARIANT, 2, dim); sa = SafeArrayCreate(VT_VARIANT, 2, dim);
} }
if (!sa) if (!sa.valid())
throw std::bad_alloc(); throw std::bad_alloc();
// Support VARIANT types that may be used for SAFEARRAY // Support VARIANT types that may be used for SAFEARRAY
@ -1707,7 +1707,7 @@ namespace winstd
// Allocate. // Allocate.
size_t pallete_size = sizeof(RGBQUAD) * bmh.biClrUsed; size_t pallete_size = sizeof(RGBQUAD) * bmh.biClrUsed;
safearray sa = SafeArrayCreateVector(VT_UI1, 0, static_cast<ULONG>(sizeof(BITMAPFILEHEADER) + sizeof(bmh) + pallete_size + bmh.biSizeImage)); safearray sa = SafeArrayCreateVector(VT_UI1, 0, static_cast<ULONG>(sizeof(BITMAPFILEHEADER) + sizeof(bmh) + pallete_size + bmh.biSizeImage));
if (!sa) if (!sa.valid())
throw std::bad_alloc(); throw std::bad_alloc();
// Locate BITMAPFILEHEADER, BITMAPINFO and pixel map. // Locate BITMAPFILEHEADER, BITMAPINFO and pixel map.

View File

@ -1562,7 +1562,7 @@ namespace winstd
if (!AdjustTokenPrivileges(thread_token, FALSE, &privileges, sizeof(privileges), NULL, NULL)) if (!AdjustTokenPrivileges(thread_token, FALSE, &privileges, sizeof(privileges), NULL, NULL))
goto revert; goto revert;
process_snapshot process_snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); process_snapshot process_snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (!process_snapshot) if (!process_snapshot.valid())
goto revert; goto revert;
PROCESSENTRY32 entry = { sizeof(PROCESSENTRY32) }; PROCESSENTRY32 entry = { sizeof(PROCESSENTRY32) };
if (!Process32First(process_snapshot, &entry)) if (!Process32First(process_snapshot, &entry))
@ -1571,7 +1571,7 @@ namespace winstd
if (!Process32Next(process_snapshot, &entry)) if (!Process32Next(process_snapshot, &entry))
goto revert; goto revert;
process winlogon_process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, entry.th32ProcessID); process winlogon_process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, entry.th32ProcessID);
if (!winlogon_process) if (!winlogon_process.valid())
goto revert; goto revert;
if (!OpenProcessToken(winlogon_process, TOKEN_IMPERSONATE | TOKEN_DUPLICATE, &h)) if (!OpenProcessToken(winlogon_process, TOKEN_IMPERSONATE | TOKEN_DUPLICATE, &h))
goto revert; goto revert;
@ -2033,7 +2033,7 @@ namespace winstd
{ {
std::wstring wstr; std::wstring wstr;
winstd::library ntdll(LoadLibraryW(L"NTDLL.DLL")); 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. // 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); wstr.erase(wstr.find_last_not_of(L" \t\n\r\f\v") + 1);
} else } else