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
|
#include "first.h"
#include "safe_memclear.h"
#include <string.h>
#if !defined(HAVE_MEMSET_S) && !defined(HAVE_EXPLICIT_BZERO) && !defined(HAVE_EXPLICIT_MEMSET)
# if defined(HAVE_WEAK_SYMBOLS)
/* it seems weak functions are never inlined, even for static builds */
__attribute__((weak)) void __li_safe_memset_hook(void *buf, size_t len);
void __li_safe_memset_hook(void *buf, size_t len)
{
UNUSED(buf);
UNUSED(len);
}
# endif /* HAVE_WEAK_SYMBOLS */
static void* safe_memset(void *s, int c, size_t n)
{
if (n > 0) {
volatile unsigned volatile_zero = 0;
volatile unsigned char *vs = (volatile unsigned char*)s;
do {
memset(s, c, n);
} while (vs[volatile_zero] != (unsigned char)c);
# if defined(HAVE_WEAK_SYMBOLS)
__li_safe_memset_hook(s, n);
# endif /* HAVE_WEAK_SYMBOLS */
}
return s;
}
#endif /* !defined(HAVE_MEMSET_S) && !defined(HAVE_EXPLICIT_BZERO) */
void safe_memclear(void *s, size_t n) {
#if defined(HAVE_MEMSET_S)
memset_s(s, n, 0, n);
#elif defined(HAVE_EXPLICIT_BZERO)
explicit_bzero(s, n);
#elif defined(HAVE_EXPLICIT_MEMSET)
explicit_memset(s, 0, n);
#else
safe_memset(s, 0, n);
#endif
}
|