With the proposed changes for SafeConvert (see below), I'm wondering if we should add some asserts for debug builds.
The assert will alert of a potential problem with the conversion, so
those who don't check return values will be made aware of potential
problems with their code.
CRYPTOPP_ASSERT raises a SIGTRAP, so it won't degrade the debugging experience. Under GDB, the user can press "c" to continue.
**********
template <class T1, class T2>
inline bool SafeConvert(T1 from, T2 &to)
{
// Original code: always perform the assignment
to = (T2)from;
// Check for sign difference
if(std::numeric_limits<T1>::is_signed ^ std::numeric_limits<T2>::is_signed)
{
// Handle T1 is signed
if(std::numeric_limits<T1>::is_signed && from < 0)
return false;
// Fall through for T1 is unsigned
}
if(from > static_cast<T1>(std::numeric_limits<T2>::max()))
return false;
return true;
}