From b19b00b61a542dad5276691c818af87f2821f98c Mon Sep 17 00:00:00 2001 From: neargye Date: Sat, 26 Jun 2021 12:03:55 +0300 Subject: [PATCH] Fix underflow in out of range check Fix bool special case --- include/nameof.hpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/include/nameof.hpp b/include/nameof.hpp index 3ca6752..0121f69 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -429,6 +429,10 @@ constexpr bool cmp_less(L lhs, R rhs) noexcept { if constexpr (std::is_signed_v == std::is_signed_v) { // If same signedness (both signed or both unsigned). return lhs < rhs; + } else if constexpr (std::is_same_v) { // bool special case due to msvc's C4804, C4018 + return static_cast(lhs) < rhs; + } else if constexpr (std::is_same_v) { // bool special case due to msvc's C4804, C4018 + return lhs < static_cast(rhs); } else if constexpr (std::is_signed_v) { // If 'right' is negative, then result is 'false', otherwise cast & compare. return rhs > 0 && lhs < static_cast>(rhs); @@ -520,11 +524,11 @@ constexpr int reflected_min() noexcept { static_assert(lhs > (std::numeric_limits::min)(), "nameof::enum_range requires min must be greater than INT16_MIN."); constexpr auto rhs = (std::numeric_limits::min)(); - if constexpr (cmp_less(lhs, rhs)) { - return rhs; - } else { + if constexpr (cmp_less(rhs, lhs)) { static_assert(!is_valid(0)>(), "nameof::enum_range detects enum value smaller than min range size."); return lhs; + } else { + return rhs; } } }