diff options
author | Nuno Goncalves <[email protected]> | 2019-07-13 14:51:16 +0200 |
---|---|---|
committer | Nuno Goncalves <[email protected]> | 2019-10-14 15:59:38 +0200 |
commit | 385dd2fb263aeec795476243cd9fb5905932d77f (patch) | |
tree | 605bd2423156609cccd2126e0d603e829365055f /src/lib/utils | |
parent | 1a6ad661ce64287ccbe26460ccc3aa4247d86ba8 (diff) |
Add additional typecast and several static_asserts
Signed-off-by: Nuno Goncalves <[email protected]>
Diffstat (limited to 'src/lib/utils')
-rw-r--r-- | src/lib/utils/mem_ops.h | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/lib/utils/mem_ops.h b/src/lib/utils/mem_ops.h index 569cb409b..4206875b2 100644 --- a/src/lib/utils/mem_ops.h +++ b/src/lib/utils/mem_ops.h @@ -10,6 +10,7 @@ #include <botan/types.h> #include <cstring> +#include <type_traits> #include <vector> namespace Botan { @@ -113,6 +114,15 @@ template<typename T> inline void clear_mem(T* ptr, size_t n) clear_bytes(ptr, sizeof(T)*n); } + + +// is_trivially_copyable is missing in g++ < 5.0 +#if !__clang__ && __GNUG__ && __GNUC__ < 5 +#define IS_TRIVIALLY_COPYABLE(T) true +#else +#define IS_TRIVIALLY_COPYABLE(T) std::is_trivially_copyable<T>::value +#endif + /** * Copy memory * @param out the destination array @@ -121,6 +131,7 @@ template<typename T> inline void clear_mem(T* ptr, size_t n) */ template<typename T> inline void copy_mem(T* out, const T* in, size_t n) { + static_assert(std::is_trivial<typename std::decay<T>::type>::value, ""); if(n > 0) { std::memmove(out, in, sizeof(T)*n); @@ -129,11 +140,13 @@ template<typename T> inline void copy_mem(T* out, const T* in, size_t n) template<typename T> inline void typecast_copy(uint8_t out[], T in[], size_t N) { + static_assert(IS_TRIVIALLY_COPYABLE(T), ""); std::memcpy(out, in, sizeof(T)*N); } template<typename T> inline void typecast_copy(T out[], const uint8_t in[], size_t N) { + static_assert(std::is_trivial<T>::value, ""); std::memcpy(out, in, sizeof(T)*N); } @@ -144,9 +157,18 @@ template<typename T> inline void typecast_copy(uint8_t out[], T in) template<typename T> inline void typecast_copy(T& out, const uint8_t in[]) { + static_assert(std::is_trivial<typename std::decay<T>::type>::value, ""); typecast_copy(&out, in, 1); } +template <class To, class From> inline To typecast_copy(const From *src) noexcept + { + static_assert(IS_TRIVIALLY_COPYABLE(From) && std::is_trivial<To>::value, ""); + To dst; + std::memcpy(&dst, src, sizeof(To)); + return dst; + } + /** * Set memory to a fixed value * @param ptr a pointer to an array of bytes |