2014-02-04 00:18:20 +00:00
|
|
|
/* OPENBSD ORIGINAL: lib/libc/string/explicit_bzero.c */
|
|
|
|
/* $OpenBSD: explicit_bzero.c,v 1.1 2014/01/22 21:06:45 tedu Exp $ */
|
|
|
|
/*
|
|
|
|
* Public domain.
|
|
|
|
* Written by Ted Unangst
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
|
|
|
|
2016-08-16 03:28:23 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2014-08-26 20:32:01 +00:00
|
|
|
/*
|
|
|
|
* explicit_bzero - don't let the compiler optimize away bzero
|
|
|
|
*/
|
|
|
|
|
2014-02-04 00:18:20 +00:00
|
|
|
#ifndef HAVE_EXPLICIT_BZERO
|
|
|
|
|
2020-11-21 12:22:23 +00:00
|
|
|
#ifdef HAVE_EXPLICIT_MEMSET
|
|
|
|
|
|
|
|
void
|
|
|
|
explicit_bzero(void *p, size_t n)
|
|
|
|
{
|
|
|
|
(void)explicit_memset(p, 0, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(HAVE_MEMSET_S)
|
2014-08-26 20:32:01 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
explicit_bzero(void *p, size_t n)
|
|
|
|
{
|
2017-07-14 04:26:36 +00:00
|
|
|
if (n == 0)
|
|
|
|
return;
|
2014-08-26 20:32:01 +00:00
|
|
|
(void)memset_s(p, n, 0, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* HAVE_MEMSET_S */
|
|
|
|
|
2014-02-04 00:18:20 +00:00
|
|
|
/*
|
2014-08-26 20:32:01 +00:00
|
|
|
* Indirect bzero through a volatile pointer to hopefully avoid
|
|
|
|
* dead-store optimisation eliminating the call.
|
2014-02-04 00:18:20 +00:00
|
|
|
*/
|
2014-08-26 20:32:01 +00:00
|
|
|
static void (* volatile ssh_bzero)(void *, size_t) = bzero;
|
|
|
|
|
2014-02-04 00:18:20 +00:00
|
|
|
void
|
|
|
|
explicit_bzero(void *p, size_t n)
|
|
|
|
{
|
2017-07-14 04:26:36 +00:00
|
|
|
if (n == 0)
|
|
|
|
return;
|
2016-08-16 03:28:23 +00:00
|
|
|
/*
|
|
|
|
* clang -fsanitize=memory needs to intercept memset-like functions
|
|
|
|
* to correctly detect memory initialisation. Make sure one is called
|
2018-04-10 00:19:02 +00:00
|
|
|
* directly since our indirection trick above successfully confuses it.
|
2016-08-16 03:28:23 +00:00
|
|
|
*/
|
|
|
|
#if defined(__has_feature)
|
|
|
|
# if __has_feature(memory_sanitizer)
|
|
|
|
memset(p, 0, n);
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2014-08-26 20:32:01 +00:00
|
|
|
ssh_bzero(p, n);
|
2014-02-04 00:18:20 +00:00
|
|
|
}
|
2014-08-26 20:32:01 +00:00
|
|
|
|
|
|
|
#endif /* HAVE_MEMSET_S */
|
|
|
|
|
|
|
|
#endif /* HAVE_EXPLICIT_BZERO */
|