Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 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 "includes.h" |
| 9 | |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 10 | /* |
| 11 | * explicit_bzero - don't let the compiler optimize away bzero |
| 12 | */ |
Damien Miller | 3d673d1 | 2014-08-27 06:32:01 +1000 | [diff] [blame] | 13 | |
| 14 | #ifndef HAVE_EXPLICIT_BZERO |
| 15 | |
| 16 | #ifdef HAVE_MEMSET_S |
| 17 | |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 18 | void |
| 19 | explicit_bzero(void *p, size_t n) |
| 20 | { |
Damien Miller | 3d673d1 | 2014-08-27 06:32:01 +1000 | [diff] [blame] | 21 | (void)memset_s(p, n, 0, n); |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 22 | } |
Damien Miller | 3d673d1 | 2014-08-27 06:32:01 +1000 | [diff] [blame] | 23 | |
| 24 | #else /* HAVE_MEMSET_S */ |
| 25 | |
| 26 | /* |
| 27 | * Indirect bzero through a volatile pointer to hopefully avoid |
| 28 | * dead-store optimisation eliminating the call. |
| 29 | */ |
| 30 | static void (* volatile ssh_bzero)(void *, size_t) = bzero; |
| 31 | |
| 32 | void |
| 33 | explicit_bzero(void *p, size_t n) |
| 34 | { |
| 35 | ssh_bzero(p, n); |
| 36 | } |
| 37 | |
| 38 | #endif /* HAVE_MEMSET_S */ |
| 39 | |
| 40 | #endif /* HAVE_EXPLICIT_BZERO */ |