aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/libc_compat/explicit_bzero.c
blob: f7a6d81ff5446f576fe21549387d9e30c8ac0241 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*-------------------------------------------------------------------------
 *
 * explicit_bzero.c
 *
 * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  src/port/explicit_bzero.c
 *
 *-------------------------------------------------------------------------
 */

#include <stddef.h>

#if defined(HAVE_MEMSET_S)

void
explicit_bzero(void *buf, size_t len)
{
	(void) memset_s(buf, len, 0, len);
}

#elif defined(WIN32)

#include <Windows.h>

void
explicit_bzero(void *buf, size_t len)
{
	(void) SecureZeroMemory(buf, len);
}

#else

/*
 * Indirect call through a volatile pointer to hopefully avoid dead-store
 * optimisation eliminating the call.  (Idea taken from OpenSSH.)  We can't
 * assume bzero() is present either, so for simplicity we define our own.
 */

static void
bzero2(void *buf, size_t len)
{
	memset(buf, 0, len);
}

static void (*volatile bzero_p) (void *, size_t) = bzero2;

void
explicit_bzero(void *buf, size_t len)
{
	bzero_p(buf, len);
}

#endif