Skip to content

Commit

Permalink
Add specialization for SafeConvert
Browse files Browse the repository at this point in the history
  • Loading branch information
noloader committed Jun 23, 2023
1 parent 823ceca commit 7e63138
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,8 @@ template <class T1, class T2> inline const T1 UnsignedMin(const T1& a, const T2&
/// \return true if its safe to convert \p from to \p to, false otherwise.
/// \details if the function returns true, then it is safe to use \p to. If the function returns false,
/// then \p to is undefined and should not be used.
/// \note: for integral conversions, a template specialization should be provided. The specialization
/// will perform more efficiently, and avoid warnings for truncation and sign compares.
template <class T1, class T2>
inline bool SafeConvert(T1 from, T2 &to)
{
Expand Down Expand Up @@ -812,6 +814,38 @@ inline bool SafeConvert(sword32 from, word64 &to)
return true;
}

/// \brief Perform a conversion from \p from to \p to
/// \param from the first value
/// \param to the second value
/// \return true if its safe to convert \p from to \p to, false otherwise.
/// \details if the function returns true, then it is safe to use \p to. If the function returns false,
/// then \p to is undefined and should not be used.
/// \since Crypto++ 8.7
template<>
inline bool SafeConvert(word32 from, sword32 &to)
{
if (from > static_cast<word32>(std::numeric_limits<sword32>::max()))
return false;
to = static_cast<sword32>(from);
return true;
}

/// \brief Perform a conversion from \p from to \p to
/// \param from the first value
/// \param to the second value
/// \return true if its safe to convert \p from to \p to, false otherwise.
/// \details if the function returns true, then it is safe to use \p to. If the function returns false,
/// then \p to is undefined and should not be used.
/// \since Crypto++ 8.7
template<>
inline bool SafeConvert(lword from, size_t &to)
{
if (from > static_cast<lword>(std::numeric_limits<size_t>::max()))
return false;
to = static_cast<size_t>(from);
return true;
}

/// \brief Converts a value to a string
/// \tparam T class or type
/// \param value the value to convert
Expand Down

0 comments on commit 7e63138

Please sign in to comment.