Compare commits

..

3 Commits

Author SHA1 Message Date
75c5630dbc Common: Require explicit handle validation
Some checks failed
CodeQL / Analyze (cpp) (push) Has been cancelled
Doxygen Action / build (push) Has been cancelled
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>
2025-07-08 11:15:54 +02:00
9e992db24e Obsolete unnecessary function wrappers
Some checks failed
CodeQL / Analyze (cpp) (push) Has been cancelled
Doxygen Action / build (push) Has been cancelled
Let's keep our codebase minimal.

Signed-off-by: Simon Rozman <simon@rozman.si>
2025-07-08 10:39:40 +02:00
23b856f2e6 Common: Enforce explicit handle validation
Some invalid handle values are -1 or INVALID_HANDLE_VALUE, hence our
handle template has a specific invalid handle value parameter we should
always test against. Time and again, I find my code simply comparing
handle against zero.

Signed-off-by: Simon Rozman <simon@rozman.si>
2025-07-08 10:38:40 +02:00
7 changed files with 28 additions and 3 deletions

View File

@@ -125,7 +125,7 @@ namespace winstd
if (this != std::addressof(h)) { if (this != std::addressof(h)) {
if (m_h != invalid) if (m_h != invalid)
free_internal(); free_internal();
if (h) if (h.valid())
duplicate_internal(h); duplicate_internal(h);
else else
m_h = invalid; m_h = invalid;

View File

@@ -1665,7 +1665,7 @@ namespace winstd
}; };
sa = SafeArrayCreate(VT_VARIANT, _countof(dim), dim); sa = SafeArrayCreate(VT_VARIANT, _countof(dim), dim);
} }
if (!sa) if (!sa.valid())
throw std::bad_alloc(); throw std::bad_alloc();
assert(SafeArrayGetElemsize(sa) == sizeof(VARIANT)); assert(SafeArrayGetElemsize(sa) == sizeof(VARIANT));

View File

@@ -914,6 +914,9 @@ namespace winstd
handle(_In_ const handle<handle_type, INVAL> &h) noexcept {}; handle(_In_ const handle<handle_type, INVAL> &h) noexcept {};
handle<handle_type, INVAL>& operator=(_In_ const handle<handle_type, INVAL> &h) noexcept {}; handle<handle_type, INVAL>& operator=(_In_ const handle<handle_type, INVAL> &h) noexcept {};
// Force use of valid() method when testing handle.
operator bool() const;
public: public:
/// ///
/// Attaches already available object handle. /// Attaches already available object handle.

View File

@@ -806,6 +806,7 @@ namespace winstd
/// ///
/// \sa [CertGetCertificateChain function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376078.aspx) /// \sa [CertGetCertificateChain function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376078.aspx)
/// ///
[[deprecated("Use original function and have handle::handle_type* handle::operator&() set the handle")]]
static BOOL CertGetCertificateChain(_In_opt_ HCERTCHAINENGINE hChainEngine, _In_ PCCERT_CONTEXT pCertContext, _In_opt_ LPFILETIME pTime, _In_opt_ HCERTSTORE hAdditionalStore, _In_ PCERT_CHAIN_PARA pChainPara, _In_ DWORD dwFlags, _Reserved_ LPVOID pvReserved, _Inout_ winstd::cert_chain_context &ctx) static BOOL CertGetCertificateChain(_In_opt_ HCERTCHAINENGINE hChainEngine, _In_ PCCERT_CONTEXT pCertContext, _In_opt_ LPFILETIME pTime, _In_opt_ HCERTSTORE hAdditionalStore, _In_ PCERT_CHAIN_PARA pChainPara, _In_ DWORD dwFlags, _Reserved_ LPVOID pvReserved, _Inout_ winstd::cert_chain_context &ctx)
{ {
PCCERT_CHAIN_CONTEXT pChainContext; PCCERT_CHAIN_CONTEXT pChainContext;
@@ -816,6 +817,7 @@ static BOOL CertGetCertificateChain(_In_opt_ HCERTCHAINENGINE hChainEngine, _In_
} }
/// @copydoc CryptAcquireContextW() /// @copydoc CryptAcquireContextW()
[[deprecated("Use original function and have handle::handle_type* handle::operator&() set the handle")]]
static BOOL CryptAcquireContextA(_Inout_ winstd::crypt_prov &prov, _In_opt_ LPCSTR szContainer, _In_opt_ LPCSTR szProvider, _In_ DWORD dwProvType, _In_ DWORD dwFlags) static BOOL CryptAcquireContextA(_Inout_ winstd::crypt_prov &prov, _In_opt_ LPCSTR szContainer, _In_opt_ LPCSTR szProvider, _In_ DWORD dwProvType, _In_ DWORD dwFlags)
{ {
HCRYPTPROV h; HCRYPTPROV h;
@@ -830,6 +832,7 @@ static BOOL CryptAcquireContextA(_Inout_ winstd::crypt_prov &prov, _In_opt_ LPCS
/// ///
/// \sa [CryptAcquireContext function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379886.aspx) /// \sa [CryptAcquireContext function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379886.aspx)
/// ///
[[deprecated("Use original function and have handle::handle_type* handle::operator&() set the handle")]]
static BOOL CryptAcquireContextW(_Inout_ winstd::crypt_prov &prov, _In_opt_ LPCWSTR szContainer, _In_opt_ LPCWSTR szProvider, _In_ DWORD dwProvType, _In_ DWORD dwFlags) static BOOL CryptAcquireContextW(_Inout_ winstd::crypt_prov &prov, _In_opt_ LPCWSTR szContainer, _In_opt_ LPCWSTR szProvider, _In_ DWORD dwProvType, _In_ DWORD dwFlags)
{ {
HCRYPTPROV h; HCRYPTPROV h;
@@ -844,6 +847,7 @@ static BOOL CryptAcquireContextW(_Inout_ winstd::crypt_prov &prov, _In_opt_ LPCW
/// ///
/// \sa [CryptCreateHash function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379908.aspx) /// \sa [CryptCreateHash function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379908.aspx)
/// ///
[[deprecated("Use original function and have handle::handle_type* handle::operator&() set the handle")]]
static BOOL CryptCreateHash(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ HCRYPTKEY hKey, _In_ DWORD dwFlags, _Inout_ winstd::crypt_hash &hash) static BOOL CryptCreateHash(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ HCRYPTKEY hKey, _In_ DWORD dwFlags, _Inout_ winstd::crypt_hash &hash)
{ {
HCRYPTHASH h; HCRYPTHASH h;
@@ -858,6 +862,7 @@ static BOOL CryptCreateHash(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ HCRYP
/// ///
/// \sa [CryptGenKey function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379941.aspx) /// \sa [CryptGenKey function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379941.aspx)
/// ///
[[deprecated("Use original function and have handle::handle_type* handle::operator&() set the handle")]]
static BOOL CryptGenKey(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ DWORD dwFlags, _Inout_ winstd::crypt_key &key) static BOOL CryptGenKey(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ DWORD dwFlags, _Inout_ winstd::crypt_key &key)
{ {
HCRYPTKEY h; HCRYPTKEY h;
@@ -872,6 +877,7 @@ static BOOL CryptGenKey(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ DWORD dwF
/// ///
/// \sa [CryptImportKey function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa380207.aspx) /// \sa [CryptImportKey function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa380207.aspx)
/// ///
[[deprecated("Use original function and have handle::handle_type* handle::operator&() set the handle")]]
static bool CryptImportKey(_In_ HCRYPTPROV hProv, __in_bcount(dwDataLen) LPCBYTE pbData, _In_ DWORD dwDataLen, _In_ HCRYPTKEY hPubKey, _In_ DWORD dwFlags, _Inout_ winstd::crypt_key &key) static bool CryptImportKey(_In_ HCRYPTPROV hProv, __in_bcount(dwDataLen) LPCBYTE pbData, _In_ DWORD dwDataLen, _In_ HCRYPTKEY hPubKey, _In_ DWORD dwFlags, _Inout_ winstd::crypt_key &key)
{ {
HCRYPTKEY h; HCRYPTKEY h;
@@ -886,6 +892,7 @@ static bool CryptImportKey(_In_ HCRYPTPROV hProv, __in_bcount(dwDataLen) LPCBYTE
/// ///
/// \sa [CryptImportPublicKeyInfo function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa380209.aspx) /// \sa [CryptImportPublicKeyInfo function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa380209.aspx)
/// ///
[[deprecated("Use original function and have handle::handle_type* handle::operator&() set the handle")]]
static bool CryptImportPublicKeyInfo(_In_ HCRYPTPROV hCryptProv, _In_ DWORD dwCertEncodingType, _In_ PCERT_PUBLIC_KEY_INFO pInfo, _Inout_ winstd::crypt_key &key) static bool CryptImportPublicKeyInfo(_In_ HCRYPTPROV hCryptProv, _In_ DWORD dwCertEncodingType, _In_ PCERT_PUBLIC_KEY_INFO pInfo, _Inout_ winstd::crypt_key &key)
{ {
HCRYPTKEY h; HCRYPTKEY h;
@@ -900,6 +907,7 @@ static bool CryptImportPublicKeyInfo(_In_ HCRYPTPROV hCryptProv, _In_ DWORD dwCe
/// ///
/// \sa [CryptDeriveKey function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379916.aspx) /// \sa [CryptDeriveKey function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379916.aspx)
/// ///
[[deprecated("Use original function and have handle::handle_type* handle::operator&() set the handle")]]
static bool CryptDeriveKey(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ HCRYPTHASH hBaseData, _In_ DWORD dwFlags, _Inout_ winstd::crypt_key &key) static bool CryptDeriveKey(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ HCRYPTHASH hBaseData, _In_ DWORD dwFlags, _Inout_ winstd::crypt_key &key)
{ {
HCRYPTKEY h; HCRYPTKEY h;

View File

@@ -163,12 +163,15 @@ namespace winstd
/// \addtogroup WinStdWLANAPI /// \addtogroup WinStdWLANAPI
/// @{ /// @{
#pragma warning(push)
#pragma warning(disable: 4505) // Don't warn on unused code
/// ///
/// Opens a connection to the server. /// Opens a connection to the server.
/// ///
/// \sa [WlanOpenHandle function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms706759.aspx) /// \sa [WlanOpenHandle function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms706759.aspx)
/// ///
#pragma warning(suppress: 4505) // Don't warn on unused code [[deprecated("Use original function and have handle::handle_type* handle::operator&() set the handle")]]
static DWORD WlanOpenHandle( static DWORD WlanOpenHandle(
_In_ DWORD dwClientVersion, _In_ DWORD dwClientVersion,
_Reserved_ PVOID pReserved, _Reserved_ PVOID pReserved,
@@ -182,4 +185,6 @@ static DWORD WlanOpenHandle(
return dwResult; return dwResult;
} }
#pragma warning(pop)
/// @} /// @}

View File

@@ -2054,6 +2054,7 @@ namespace winstd
#pragma warning(disable: 4505) // Don't warn on unused code #pragma warning(disable: 4505) // Don't warn on unused code
/// @copydoc RegCreateKeyExW() /// @copydoc RegCreateKeyExW()
[[deprecated("Use original function and have handle::handle_type* handle::operator&() set the handle")]]
static LSTATUS RegCreateKeyExA( static LSTATUS RegCreateKeyExA(
_In_ HKEY hKey, _In_ HKEY hKey,
_In_ LPCSTR lpSubKey, _In_ LPCSTR lpSubKey,
@@ -2077,6 +2078,7 @@ static LSTATUS RegCreateKeyExA(
/// ///
/// \sa [RegCreateKeyEx function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724844.aspx) /// \sa [RegCreateKeyEx function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724844.aspx)
/// ///
[[deprecated("Use original function and have handle::handle_type* handle::operator&() set the handle")]]
static LSTATUS RegCreateKeyExW( static LSTATUS RegCreateKeyExW(
_In_ HKEY hKey, _In_ HKEY hKey,
_In_ LPCWSTR lpSubKey, _In_ LPCWSTR lpSubKey,
@@ -2096,6 +2098,7 @@ static LSTATUS RegCreateKeyExW(
} }
/// @copydoc RegOpenKeyExW() /// @copydoc RegOpenKeyExW()
[[deprecated("Use original function and have handle::handle_type* handle::operator&() set the handle")]]
static LSTATUS RegOpenKeyExA( static LSTATUS RegOpenKeyExA(
_In_ HKEY hKey, _In_ HKEY hKey,
_In_opt_ LPCSTR lpSubKey, _In_opt_ LPCSTR lpSubKey,
@@ -2115,6 +2118,7 @@ static LSTATUS RegOpenKeyExA(
/// ///
/// \sa [RegOpenKeyEx function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724897.aspx) /// \sa [RegOpenKeyEx function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724897.aspx)
/// ///
[[deprecated("Use original function and have handle::handle_type* handle::operator&() set the handle")]]
static LSTATUS RegOpenKeyExW( static LSTATUS RegOpenKeyExW(
_In_ HKEY hKey, _In_ HKEY hKey,
_In_opt_ LPCWSTR lpSubKey, _In_opt_ LPCWSTR lpSubKey,
@@ -2134,6 +2138,7 @@ static LSTATUS RegOpenKeyExW(
/// ///
/// \sa [OpenProcessToken function](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-openprocesstoken) /// \sa [OpenProcessToken function](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-openprocesstoken)
/// ///
[[deprecated("Use original function and have handle::handle_type* handle::operator&() set the handle")]]
static BOOL OpenProcessToken(_In_ HANDLE ProcessHandle, _In_ DWORD DesiredAccess, _Inout_ winstd::win_handle<NULL> &TokenHandle) static BOOL OpenProcessToken(_In_ HANDLE ProcessHandle, _In_ DWORD DesiredAccess, _Inout_ winstd::win_handle<NULL> &TokenHandle)
{ {
HANDLE h; HANDLE h;
@@ -2149,6 +2154,7 @@ static BOOL OpenProcessToken(_In_ HANDLE ProcessHandle, _In_ DWORD DesiredAccess
/// ///
/// \sa [DuplicateTokenEx function](https://learn.microsoft.com/en-us/windows/win32/api/securitybaseapi/nf-securitybaseapi-duplicatetokenex) /// \sa [DuplicateTokenEx function](https://learn.microsoft.com/en-us/windows/win32/api/securitybaseapi/nf-securitybaseapi-duplicatetokenex)
/// ///
[[deprecated("Use original function and have handle::handle_type* handle::operator&() set the handle")]]
static BOOL DuplicateTokenEx(_In_ HANDLE hExistingToken, _In_ DWORD dwDesiredAccess, _In_opt_ LPSECURITY_ATTRIBUTES lpTokenAttributes, _In_ SECURITY_IMPERSONATION_LEVEL ImpersonationLevel, _In_ TOKEN_TYPE TokenType, _Inout_ winstd::win_handle<NULL> &NewToken) static BOOL DuplicateTokenEx(_In_ HANDLE hExistingToken, _In_ DWORD dwDesiredAccess, _In_opt_ LPSECURITY_ATTRIBUTES lpTokenAttributes, _In_ SECURITY_IMPERSONATION_LEVEL ImpersonationLevel, _In_ TOKEN_TYPE TokenType, _Inout_ winstd::win_handle<NULL> &NewToken)
{ {
HANDLE h; HANDLE h;
@@ -2164,6 +2170,7 @@ static BOOL DuplicateTokenEx(_In_ HANDLE hExistingToken, _In_ DWORD dwDesiredAcc
/// ///
/// \sa [AllocateAndInitializeSid function](https://learn.microsoft.com/en-us/windows/win32/api/securitybaseapi/nf-securitybaseapi-allocateandinitializesid) /// \sa [AllocateAndInitializeSid function](https://learn.microsoft.com/en-us/windows/win32/api/securitybaseapi/nf-securitybaseapi-allocateandinitializesid)
/// ///
[[deprecated("Use original function and have handle::handle_type* handle::operator&() set the handle")]]
static BOOL AllocateAndInitializeSid(_In_ PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority, _In_ BYTE nSubAuthorityCount, _In_ DWORD nSubAuthority0, _In_ DWORD nSubAuthority1, _In_ DWORD nSubAuthority2, _In_ DWORD nSubAuthority3, _In_ DWORD nSubAuthority4, _In_ DWORD nSubAuthority5, _In_ DWORD nSubAuthority6, _In_ DWORD nSubAuthority7, _Inout_ winstd::security_id& Sid) static BOOL AllocateAndInitializeSid(_In_ PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority, _In_ BYTE nSubAuthorityCount, _In_ DWORD nSubAuthority0, _In_ DWORD nSubAuthority1, _In_ DWORD nSubAuthority2, _In_ DWORD nSubAuthority3, _In_ DWORD nSubAuthority4, _In_ DWORD nSubAuthority5, _In_ DWORD nSubAuthority6, _In_ DWORD nSubAuthority7, _Inout_ winstd::security_id& Sid)
{ {
PSID h; PSID h;

View File

@@ -187,6 +187,7 @@ namespace winstd
#pragma warning(disable: 4505) // Don't warn on unused code #pragma warning(disable: 4505) // Don't warn on unused code
/// @copydoc GetAddrInfoW() /// @copydoc GetAddrInfoW()
[[deprecated("Use original function and have handle::handle_type* handle::operator&() set the handle")]]
static INT GetAddrInfoA( static INT GetAddrInfoA(
_In_opt_ PCSTR pNodeName, _In_opt_ PCSTR pNodeName,
_In_opt_ PCSTR pServiceName, _In_opt_ PCSTR pServiceName,
@@ -205,6 +206,7 @@ static INT GetAddrInfoA(
/// ///
/// \sa [GetAddrInfoW function](https://docs.microsoft.com/en-us/windows/desktop/api/ws2tcpip/nf-ws2tcpip-getaddrinfow) /// \sa [GetAddrInfoW function](https://docs.microsoft.com/en-us/windows/desktop/api/ws2tcpip/nf-ws2tcpip-getaddrinfow)
/// ///
[[deprecated("Use original function and have handle::handle_type* handle::operator&() set the handle")]]
static INT GetAddrInfoW( static INT GetAddrInfoW(
_In_opt_ PCWSTR pNodeName, _In_opt_ PCWSTR pNodeName,
_In_opt_ PCWSTR pServiceName, _In_opt_ PCWSTR pServiceName,