diff options
Diffstat (limited to 'lib/explicit_bzero.c')
-rw-r--r-- | lib/explicit_bzero.c | 34 |
1 files changed, 29 insertions, 5 deletions
diff --git a/lib/explicit_bzero.c b/lib/explicit_bzero.c index 668c7e36a38..f50ed0875d7 100644 --- a/lib/explicit_bzero.c +++ b/lib/explicit_bzero.c @@ -25,8 +25,18 @@ # include <config.h> #endif +/* memset_s need this define */ +#if HAVE_MEMSET_S +# define __STDC_WANT_LIB_EXT1__ 1 +#endif + #include <string.h> +#if defined _WIN32 && !defined __CYGWIN__ +# define WIN32_LEAN_AND_MEAN +# include <windows.h> +#endif + #if _LIBC /* glibc-internal users use __explicit_bzero_chk, and explicit_bzero redirects to that. */ @@ -38,13 +48,27 @@ void explicit_bzero (void *s, size_t len) { -#ifdef HAVE_EXPLICIT_MEMSET - explicit_memset (s, 0, len); -#else +#if defined _WIN32 && !defined __CYGWIN__ + (void) SecureZeroMemory (s, len); +#elif HAVE_EXPLICIT_MEMSET + explicit_memset (s, '\0', len); +#elif HAVE_MEMSET_S + (void) memset_s (s, len, '\0', len); +#elif defined __GNUC__ && !defined __clang__ memset (s, '\0', len); -# if defined __GNUC__ && !defined __clang__ /* Compiler barrier. */ asm volatile ("" ::: "memory"); -# endif +#elif defined __clang__ + memset (s, '\0', len); + /* Compiler barrier. */ + /* With asm ("" ::: "memory") LLVM analyzes uses of 's' and finds that the + whole thing is dead and eliminates it. Use 'g' to work around this + problem. See <https://bugs.llvm.org/show_bug.cgi?id=15495#c11>. */ + __asm__ volatile ("" : : "g"(s) : "memory"); +#else + /* Invoke memset through a volatile function pointer. This defeats compiler + optimizations. */ + void * (* const volatile volatile_memset) (void *, int, size_t) = memset; + (void) volatile_memset (s, '\0', len); #endif } |