1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/missing/explicit_bzero.c
nobu 24dcb1285d explicit_bzero.c: needs windows.h
* missing/explicit_bzero.c, random.c (explicit_bzero):
  SecureZeroMemory() needs windows.h.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52827 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-12-01 03:52:20 +00:00

55 lines
994 B
C

#include "ruby/missing.h"
#include <string.h>
#ifdef _WIN32
#include <windows.h>
#endif
/*
*BSD have explicit_bzero().
Windows, OS-X have memset_s().
Linux has none. *Sigh*
*/
/*
* Following URL explain why memset_s is added to the standard.
* http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1381.pdf
*/
#ifndef FUNC_UNOPTIMIZED
# define FUNC_UNOPTIMIZED(x) x
#endif
#ifndef HAVE_EXPLICIT_BZERO
/* Similar to bzero(), but have a guarantee not to be eliminated from compiler
optimization. */
#ifndef HAVE_MEMSET_S
FUNC_UNOPTIMIZED(void explicit_bzero(void *b, size_t len));
#endif
#undef explicit_bzero
void
explicit_bzero(void *b, size_t len)
{
#ifdef HAVE_MEMSET_S
memset_s(b, len, 0, len);
#elif defined SecureZeroMemory
SecureZeroMemory(b, 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