From 83db3bbee4e19e7426a32ee89ad6c2d8e48260f2 Mon Sep 17 00:00:00 2001 From: Ben Longbons Date: Tue, 18 Jun 2013 20:03:57 -0700 Subject: Also poison memcpy, memmove, and memset --- src/common/utils.hpp | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'src/common/utils.hpp') 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 #include +#include #include "const_array.hpp" #include "operators.hpp" #include "utils2.hpp" +template +struct is_trivially_copyable +: std::integral_constant +{}; + 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 +void really_memzero_this(T *v) +{ + static_assert(is_trivially_copyable::value, "only for mostly-pod types"); + static_assert(std::is_class::value || std::is_union::value, "Only for user-defined structures (for now)"); + memset(v, '\0', sizeof(*v)); +} +template +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"); -- cgit v1.2.3-60-g2f50