diff options
Diffstat (limited to 'missing/explicit_bzero.c')
-rw-r--r-- | missing/explicit_bzero.c | 32 |
1 files changed, 29 insertions, 3 deletions
diff --git a/missing/explicit_bzero.c b/missing/explicit_bzero.c index cb11bd6da1..f140acb374 100644 --- a/missing/explicit_bzero.c +++ b/missing/explicit_bzero.c @@ -1,8 +1,34 @@ +#include "ruby/missing.h" #include <string.h> -/* prevent the compiler from optimizing away memset or bzero */ +/* + *BSD have explicit_bzero(). + Windows, OS-X have memset_s(). + Linux has none. *Sigh* +*/ + +#ifndef HAVE_EXPLICIT_BZERO +/* Similar to bzero(), but have a guarantee not to be eliminated from compiler + optimization. */ void -explicit_bzero(void *p, size_t n) +explicit_bzero(void *b, size_t len) { - memset(p, 0, n); +#ifdef HAVE_MEMSET_S + memset_s(b, len, 0, len); +#else + { + /* + * TODO: volatile is not enough if compiler have a LTO (link time + * optimization) + */ + volatile char* p = (volatile char*)b; + + while(len) { + *p = 0; + p++; + len--; + } + } +#endif } +#endif |