Path: blob/main/contrib/libfido2/openbsd-compat/explicit_bzero.c
39534 views
/* OPENBSD ORIGINAL: lib/libc/string/explicit_bzero.c */1/* $OpenBSD: explicit_bzero.c,v 1.1 2014/01/22 21:06:45 tedu Exp $ */2/*3* Public domain.4* Written by Ted Unangst5*/67#include "openbsd-compat.h"89#if !defined(HAVE_EXPLICIT_BZERO) && !defined(_WIN32)1011#include <string.h>1213/*14* explicit_bzero - don't let the compiler optimize away bzero15*/1617#ifdef HAVE_MEMSET_S1819void20explicit_bzero(void *p, size_t n)21{22if (n == 0)23return;24(void)memset_s(p, n, 0, n);25}2627#else /* HAVE_MEMSET_S */2829/*30* Indirect bzero through a volatile pointer to hopefully avoid31* dead-store optimisation eliminating the call.32*/33static void (* volatile ssh_bzero)(void *, size_t) = bzero;3435void36explicit_bzero(void *p, size_t n)37{38if (n == 0)39return;40/*41* clang -fsanitize=memory needs to intercept memset-like functions42* to correctly detect memory initialisation. Make sure one is called43* directly since our indirection trick above successfully confuses it.44*/45#if defined(__has_feature)46# if __has_feature(memory_sanitizer)47memset(p, 0, n);48# endif49#endif5051ssh_bzero(p, n);52}5354#endif /* HAVE_MEMSET_S */5556#endif /* !defined(HAVE_EXPLICIT_BZERO) && !defined(_WIN32) */575859