From 9b8f44cdaea3677731fc83c15f8ee4681cbc4470 Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Wed, 10 Apr 2024 09:27:53 -0400 Subject: [PATCH] New approach for checked_cast MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: René Meusel --- src/lib/utils/int_utils.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/lib/utils/int_utils.h b/src/lib/utils/int_utils.h index 95b31e16db5..2c943f4d402 100644 --- a/src/lib/utils/int_utils.h +++ b/src/lib/utils/int_utils.h @@ -9,6 +9,7 @@ #include #include +#include #include namespace Botan { @@ -46,13 +47,12 @@ constexpr inline std::optional checked_mul(T a, T b) { return r; } -template -std::optional checked_cast(T input) { - const RT cast = static_cast(input); - if(static_cast(cast) != input) { +template +constexpr inline std::optional checked_cast(std::unsigned_integral auto input) { + if(std::numeric_limits::max() < input) { return {}; } - return cast; + return static_cast(input); } } // namespace Botan