explicit_bzero.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*-------------------------------------------------------------------------
  2. *
  3. * explicit_bzero.c
  4. *
  5. * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  6. * Portions Copyright (c) 1994, Regents of the University of California
  7. *
  8. *
  9. * IDENTIFICATION
  10. * src/port/explicit_bzero.c
  11. *
  12. *-------------------------------------------------------------------------
  13. */
  14. #include <stddef.h>
  15. #include <string.h>
  16. #if defined(HAVE_MEMSET_S)
  17. void
  18. explicit_bzero(void *buf, size_t len)
  19. {
  20. (void) memset_s(buf, len, 0, len);
  21. }
  22. #elif defined(WIN32)
  23. #include <Windows.h>
  24. void
  25. explicit_bzero(void *buf, size_t len)
  26. {
  27. (void) SecureZeroMemory(buf, len);
  28. }
  29. #else
  30. /*
  31. * Indirect call through a volatile pointer to hopefully avoid dead-store
  32. * optimisation eliminating the call. (Idea taken from OpenSSH.) We can't
  33. * assume bzero() is present either, so for simplicity we define our own.
  34. */
  35. static void
  36. bzero2(void *buf, size_t len)
  37. {
  38. memset(buf, 0, len);
  39. }
  40. static void (*volatile bzero_p) (void *, size_t) = bzero2;
  41. void
  42. explicit_bzero(void *buf, size_t len)
  43. {
  44. bzero_p(buf, len);
  45. }
  46. #endif