From b99b2fc19e6f6c141563729e13a08e59c3f117fe Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Tue, 8 Jul 2025 11:17:16 +0200 Subject: [PATCH] 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 --- include/stdex/stream.hpp | 3 +++ include/stdex/system.hpp | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/include/stdex/stream.hpp b/include/stdex/stream.hpp index 4bc3c2438..6e23c87be 100644 --- a/include/stdex/stream.hpp +++ b/include/stdex/stream.hpp @@ -2383,6 +2383,9 @@ namespace stdex socket(_In_ const socket& other); socket& operator =(_In_ const socket& other); + // Force use of valid() method when testing handle. + operator bool() const; + public: socket(_Inout_ socket&& other) noexcept : m_h(other.m_h) { diff --git a/include/stdex/system.hpp b/include/stdex/system.hpp index 43e64bdff..fa7aca56b 100644 --- a/include/stdex/system.hpp +++ b/include/stdex/system.hpp @@ -200,6 +200,10 @@ namespace stdex /// operator T() const noexcept { return m_h; } + private: + // Force use of valid() method when testing handle. + operator bool() const; + protected: T m_h; };