Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * explicit_bzero.c
4 : *
5 : * Portions Copyright (c) 1996-2026, 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 :
15 : #define __STDC_WANT_LIB_EXT1__ 1 /* needed to access memset_s() */
16 :
17 : #include "c.h"
18 :
19 : #if HAVE_DECL_MEMSET_S
20 :
21 : void
22 35131 : explicit_bzero(void *buf, size_t len)
23 : {
24 35131 : (void) memset_s(buf, len, 0, len);
25 35131 : }
26 :
27 : #elif defined(WIN32)
28 :
29 : void
30 : explicit_bzero(void *buf, size_t len)
31 : {
32 : (void) SecureZeroMemory(buf, len);
33 : }
34 :
35 : #else
36 :
37 : /*
38 : * Indirect call through a volatile pointer to hopefully avoid dead-store
39 : * optimisation eliminating the call. (Idea taken from OpenSSH.) We can't
40 : * assume bzero() is present either, so for simplicity we define our own.
41 : */
42 :
43 : static void
44 : bzero2(void *buf, size_t len)
45 : {
46 : memset(buf, 0, len);
47 : }
48 :
49 : static void (*volatile bzero_p) (void *, size_t) = bzero2;
50 :
51 : void
52 : explicit_bzero(void *buf, size_t len)
53 : {
54 : bzero_p(buf, len);
55 : }
56 :
57 : #endif
|