From d9ba68f01c70001c05e8b5eb6a8e4d1c13ee8b77 Mon Sep 17 00:00:00 2001 From: terik23 Date: Sun, 31 Mar 2019 20:29:02 +0500 Subject: [PATCH] add check to enum_to_string --- include/magic_enum.hpp | 3 +++ 1 file changed, 3 insertions(+) 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)); }