2015-11-30 19:07:21 -05:00
|
|
|
#include "ruby/missing.h"
|
2015-11-30 17:53:21 -05:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-11-30 19:07:21 -05:00
|
|
|
/*
|
|
|
|
*BSD have explicit_bzero().
|
|
|
|
Windows, OS-X have memset_s().
|
|
|
|
Linux has none. *Sigh*
|
|
|
|
*/
|
|
|
|
|
2015-11-30 19:35:59 -05:00
|
|
|
/*
|
|
|
|
* Following URL explain why memset_s is added to the standard.
|
|
|
|
* http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1381.pdf
|
|
|
|
*/
|
|
|
|
|
2015-11-30 20:05:48 -05:00
|
|
|
#ifndef FUNC_UNOPTIMIZED
|
|
|
|
# define FUNC_UNOPTIMIZED(x) x
|
2015-11-30 19:35:59 -05:00
|
|
|
#endif
|
|
|
|
|
2015-11-30 19:07:21 -05:00
|
|
|
#ifndef HAVE_EXPLICIT_BZERO
|
|
|
|
/* Similar to bzero(), but have a guarantee not to be eliminated from compiler
|
|
|
|
optimization. */
|
2015-11-30 20:24:23 -05:00
|
|
|
|
|
|
|
#ifndef HAVE_MEMSET_S
|
2015-11-30 20:05:48 -05:00
|
|
|
FUNC_UNOPTIMIZED(void explicit_bzero(void *b, size_t len));
|
2015-11-30 20:24:23 -05:00
|
|
|
#endif
|
|
|
|
#undef explicit_bzero
|
2015-11-30 20:05:48 -05:00
|
|
|
|
2015-11-30 17:53:21 -05:00
|
|
|
void
|
2015-11-30 19:07:21 -05:00
|
|
|
explicit_bzero(void *b, size_t len)
|
2015-11-30 17:53:21 -05:00
|
|
|
{
|
2015-11-30 19:07:21 -05:00
|
|
|
#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
|
2015-11-30 17:53:21 -05:00
|
|
|
}
|
2015-11-30 19:07:21 -05:00
|
|
|
#endif
|