diff options
author | nicoo <nicoo@debian.org> | 2020-02-12 13:42:22 +0100 |
---|---|---|
committer | Nicolas Braud-Santoni <nicolas@braud-santoni.eu> | 2020-02-12 13:42:22 +0100 |
commit | c79050aa44b8836d836c5dd22a383a073c28b74b (patch) | |
tree | 7bcca9fabd7718bf87ca600a6594f57b76d8de7d /openbsd-compat/explicit_bzero.c |
Import upstream release 1.3.0
Closes: #951184
Diffstat (limited to 'openbsd-compat/explicit_bzero.c')
-rw-r--r-- | openbsd-compat/explicit_bzero.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/openbsd-compat/explicit_bzero.c b/openbsd-compat/explicit_bzero.c new file mode 100644 index 0000000..ac64e69 --- /dev/null +++ b/openbsd-compat/explicit_bzero.c | |||
@@ -0,0 +1,57 @@ | |||
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 "openbsd-compat.h" | ||
9 | |||
10 | #if !defined(HAVE_EXPLICIT_BZERO) && !defined(_WIN32) | ||
11 | |||
12 | #include <string.h> | ||
13 | |||
14 | /* | ||
15 | * explicit_bzero - don't let the compiler optimize away bzero | ||
16 | */ | ||
17 | |||
18 | #ifdef HAVE_MEMSET_S | ||
19 | |||
20 | void | ||
21 | explicit_bzero(void *p, size_t n) | ||
22 | { | ||
23 | if (n == 0) | ||
24 | return; | ||
25 | (void)memset_s(p, n, 0, n); | ||
26 | } | ||
27 | |||
28 | #else /* HAVE_MEMSET_S */ | ||
29 | |||
30 | /* | ||
31 | * Indirect bzero through a volatile pointer to hopefully avoid | ||
32 | * dead-store optimisation eliminating the call. | ||
33 | */ | ||
34 | static void (* volatile ssh_bzero)(void *, size_t) = bzero; | ||
35 | |||
36 | void | ||
37 | explicit_bzero(void *p, size_t n) | ||
38 | { | ||
39 | if (n == 0) | ||
40 | return; | ||
41 | /* | ||
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 successfully confuses it. | ||
45 | */ | ||
46 | #if defined(__has_feature) | ||
47 | # if __has_feature(memory_sanitizer) | ||
48 | memset(p, 0, n); | ||
49 | # endif | ||
50 | #endif | ||
51 | |||
52 | ssh_bzero(p, n); | ||
53 | } | ||
54 | |||
55 | #endif /* HAVE_MEMSET_S */ | ||
56 | |||
57 | #endif /* !defined(HAVE_EXPLICIT_BZERO) && !defined(_WIN32) */ | ||