Import OpenSSH-7.6p1
[dragonfly.git] / crypto / openssh / openbsd-compat / explicit_bzero.c
blob53a003472721d4038a572a431d54f8948dff448f
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 */
8 #include "includes.h"
10 #include <string.h>
13 * explicit_bzero - don't let the compiler optimize away bzero
16 #ifndef HAVE_EXPLICIT_BZERO
18 #ifdef HAVE_MEMSET_S
20 void
21 explicit_bzero(void *p, size_t n)
23 if (n == 0)
24 return;
25 (void)memset_s(p, n, 0, n);
28 #else /* HAVE_MEMSET_S */
31 * Indirect bzero through a volatile pointer to hopefully avoid
32 * dead-store optimisation eliminating the call.
34 static void (* volatile ssh_bzero)(void *, size_t) = bzero;
36 void
37 explicit_bzero(void *p, size_t n)
39 if (n == 0)
40 return;
42 * clang -fsanitize=memory needs to intercept memset-like functions
43 * to correctly detect memory initialisation. Make sure one is called
44 * directly since our indirection trick above sucessfully confuses it.
46 #if defined(__has_feature)
47 # if __has_feature(memory_sanitizer)
48 memset(p, 0, n);
49 # endif
50 #endif
52 ssh_bzero(p, n);
55 #endif /* HAVE_MEMSET_S */
57 #endif /* HAVE_EXPLICIT_BZERO */