diff options
author | Ben Longbons <b.r.longbons@gmail.com> | 2013-06-18 20:03:57 -0700 |
---|---|---|
committer | Ben Longbons <b.r.longbons@gmail.com> | 2013-06-18 20:11:16 -0700 |
commit | 83db3bbee4e19e7426a32ee89ad6c2d8e48260f2 (patch) | |
tree | a6fb6f6b6acaa09d5a43cd669f87c2f7f519a521 /src/common/utils.hpp | |
parent | 8b5370313dcc00a45ea5c3e8b4c497bc00fd8e13 (diff) | |
download | tmwa-83db3bbee4e19e7426a32ee89ad6c2d8e48260f2.tar.gz tmwa-83db3bbee4e19e7426a32ee89ad6c2d8e48260f2.tar.bz2 tmwa-83db3bbee4e19e7426a32ee89ad6c2d8e48260f2.tar.xz tmwa-83db3bbee4e19e7426a32ee89ad6c2d8e48260f2.zip |
Also poison memcpy, memmove, and memset
Diffstat (limited to 'src/common/utils.hpp')
-rw-r--r-- | src/common/utils.hpp | 39 |
1 files changed, 38 insertions, 1 deletions
diff --git a/src/common/utils.hpp b/src/common/utils.hpp index caa39ed..cd45aa1 100644 --- a/src/common/utils.hpp +++ b/src/common/utils.hpp @@ -7,11 +7,21 @@ #include <cstring> #include <string> +#include <type_traits> #include "const_array.hpp" #include "operators.hpp" #include "utils2.hpp" +template<class T> +struct is_trivially_copyable +: std::integral_constant<bool, + // come back when GCC actually implements the public traits properly + __has_trivial_copy(T) + && __has_trivial_assign(T) + && __has_trivial_destructor(T)> +{}; + int remove_control_chars(char *str); int e_mail_check(const char *email); int config_switch (const char *str); @@ -25,10 +35,37 @@ void strzcpy(char *dest, const char *src, size_t n) if (n) { strncpy(dest, src, n); - dest[n-1] = '\0'; + dest[n - 1] = '\0'; } } +inline +void really_memcpy(uint8_t *dest, const uint8_t *src, size_t n) +{ + memcpy(dest, src, n); +} + +inline +void really_memmove(uint8_t *dest, const uint8_t *src, size_t n) +{ + memmove(dest, src, n); +} + +inline +void really_memset0(uint8_t *dest, size_t n) +{ + memset(dest, '\0', n); +} +template<class T> +void really_memzero_this(T *v) +{ + static_assert(is_trivially_copyable<T>::value, "only for mostly-pod types"); + static_assert(std::is_class<T>::value || std::is_union<T>::value, "Only for user-defined structures (for now)"); + memset(v, '\0', sizeof(*v)); +} +template<class T, size_t n> +void really_memzero_this(T (&)[n]) = delete; + // Exists in place of time_t, to give it a predictable printf-format. // (on x86 and amd64, time_t == long, but not on x32) static_assert(sizeof(long long) >= sizeof(time_t), "long long >= time_t"); |