1 /* OPENBSD ORIGINAL: lib/libc/string/explicit_bzero.c */ 2 /* $OpenBSD: explicit_bzero.c,v 1.1 2014/01/22 21:06:45 tedu Exp $ */ 3 /* 4 * Public domain. 5 * Written by Ted Unangst 6 */ 7 8 #include <string.h> 9 10 /* 11 * explicit_bzero - don't let the compiler optimize away bzero 12 */ 13 14 #ifndef HAVE_EXPLICIT_BZERO 15 16 #include "util.h" 17 18 #ifdef HAVE_MEMSET_S 19 20 void explicit_bzero(void *p, size_t n) { 21 if (n == 0) 22 return; 23 (void) memset_s(p, n, 0, n); 24 } 25 26 #else /* HAVE_MEMSET_S */ 27 28 /* 29 * Indirect bzero through a volatile pointer to hopefully avoid 30 * dead-store optimisation eliminating the call. 31 */ 32 static void (*volatile ssh_bzero)(void *, size_t) = bzero; 33 34 void explicit_bzero(void *p, size_t n) { 35 if (n == 0) 36 return; 37 /* 38 * clang -fsanitize=memory needs to intercept memset-like functions 39 * to correctly detect memory initialisation. Make sure one is called 40 * directly since our indirection trick above successfully confuses it. 41 */ 42 #if defined(__has_feature) 43 #if __has_feature(memory_sanitizer) 44 memset(p, 0, n); 45 #endif 46 #endif 47 48 ssh_bzero(p, n); 49 } 50 51 #endif /* HAVE_MEMSET_S */ 52 53 #endif /* HAVE_EXPLICIT_BZERO */ 54