forked from google/xsecurelock
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.c
More file actions
90 lines (76 loc) · 2.09 KB
/
Copy pathutil.c
File metadata and controls
90 lines (76 loc) · 2.09 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* Copyright 2017 Google Inc.
* SPDX-License-Identifier: Apache-2.0
*
* An earlier version of this file was originally released into the public
* domain by its authors. It has been modified to make the code compile and
* link as part of the Google Authenticator project. These changes are
* copyrighted by Google Inc. and released under the Apache License,
* Version 2.0.
*
* The previous authors' terms are included below:
*/
/*****************************************************************************
*
* File: util.c
*
* Purpose: Collection of cross file utility functions.
*
* This code is in the public domain
*
*****************************************************************************
*/
#include "config.h"
#include "util.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#ifndef HAVE_EXPLICIT_BZERO
#define HAVE_EXPLICIT_BZERO 0
#endif
#ifndef FORCE_EXPLICIT_BZERO_FALLBACK
#define FORCE_EXPLICIT_BZERO_FALLBACK 0
#endif
#if !HAVE_EXPLICIT_BZERO || FORCE_EXPLICIT_BZERO_FALLBACK
// Prefer libc explicit_bzero() when it exists. Otherwise, route memset()
// through a volatile function pointer so the wipe remains an observable call
// without relying on compiler-specific inline assembly.
static void *(*const volatile memset_impl)(void *, int, size_t) = memset;
void explicit_bzero(void *s, size_t len) { memset_impl(s, 0, len); }
#endif
int ClampInt(int value, int min_value, int max_value) {
assert(min_value <= max_value);
if (value < min_value) {
return min_value;
}
if (value > max_value) {
return max_value;
}
return value;
}
double ClampDouble(double value, double min_value, double max_value) {
assert(min_value <= max_value);
if (value < min_value) {
return min_value;
}
if (value > max_value) {
return max_value;
}
return value;
}
void ClearFreeString(char **p) {
if (p == NULL || *p == NULL) {
return;
}
explicit_bzero(*p, strlen(*p));
free(*p);
*p = NULL;
}
void ClearFreeBuffer(char **p, size_t len) {
if (p == NULL || *p == NULL) {
return;
}
explicit_bzero(*p, len);
free(*p);
*p = NULL;
}