diff --git a/include/magic_enum.hpp b/include/magic_enum.hpp index 979dccd..8604248 100644 --- a/include/magic_enum.hpp +++ b/include/magic_enum.hpp @@ -170,6 +170,9 @@ template [[nodiscard]] constexpr std::optional enum_to_string(T value) noexcept { constexpr bool s = std::is_signed_v>>; constexpr int min = s ? -MAGIC_ENUM_MAX_SEARCH_DEPTH : 0; + if (static_cast(value) >= MAGIC_ENUM_MAX_SEARCH_DEPTH || static_cast(value) <= -MAGIC_ENUM_MAX_SEARCH_DEPTH) { + return std::nullopt; // Enum variable out of range MAGIC_ENUM_MAX_SEARCH_DEPTH. + } return detail::enum_to_string_impl_t, min>{}(static_cast(value)); }