GitHub CI: update list of container images
[tinc] / src / xalloc.h
1 #ifndef TINC_XALLOC_H
2 #define TINC_XALLOC_H
3
4 /*
5    xalloc.h -- malloc and related functions with out of memory checking
6    Copyright (C) 1990, 91, 92, 93, 94, 95, 96, 97 Free Software Foundation, Inc.
7    Copyright (C) 2011-2013 Guus Sliepen <guus@tinc-vpn.org>
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2, or (at your option)
12    any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License along
20    with this program; if not, write to the Free Software Foundation, Inc., Foundation,
21    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24 #include "system.h"
25
26 #include <assert.h>
27
28 static inline void *xmalloc(size_t n) ATTR_MALLOC;
29 static inline void *xmalloc(size_t n) {
30         void *p = malloc(n);
31
32         if(!p) {
33                 abort();
34         }
35
36         return p;
37 }
38
39 static inline void *xzalloc(size_t n) ATTR_MALLOC;
40 static inline void *xzalloc(size_t n) {
41         void *p = calloc(1, n);
42
43         if(!p) {
44                 abort();
45         }
46
47         return p;
48 }
49
50 static inline void *xrealloc(void *p, size_t n) {
51         p = realloc(p, n);
52
53         if(!p) {
54                 abort();
55         }
56
57         return p;
58 }
59
60 static inline char *xstrdup(const char *s) ATTR_MALLOC ATTR_NONNULL;
61 static inline char *xstrdup(const char *s) {
62         char *p = strdup(s);
63
64         if(!p) {
65                 abort();
66         }
67
68         return p;
69 }
70
71 static inline int xvasprintf(char **strp, const char *fmt, va_list ap) ATTR_FORMAT(printf, 2, 0);
72 static inline int xvasprintf(char **strp, const char *fmt, va_list ap) {
73 #ifdef HAVE_WINDOWS
74         char buf[1024];
75         int result = vsnprintf(buf, sizeof(buf), fmt, ap);
76
77         if(result < 0) {
78                 abort();
79         }
80
81         *strp = xstrdup(buf);
82 #else
83         int result = vasprintf(strp, fmt, ap);
84
85         if(result < 0) {
86                 abort();
87         }
88
89 #endif
90         return result;
91 }
92
93 static inline int xasprintf(char **strp, const char *fmt, ...) ATTR_FORMAT(printf, 2, 3);
94 static inline int xasprintf(char **strp, const char *fmt, ...) {
95         va_list ap;
96         va_start(ap, fmt);
97         int result = xvasprintf(strp, fmt, ap);
98         va_end(ap);
99         return result;
100 }
101
102 // Zero out a block of memory containing sensitive information using whatever secure
103 // erase function is available on the platform (or an unreliable fallback if none are).
104 // The pointer must not be NULL. Length can be zero, in which case the call is a noop.
105 static inline void memzero(void *buf, size_t buflen) ATTR_NONNULL;
106 static inline void memzero(void *buf, size_t buflen) {
107         assert(buf);
108
109         if(!buflen) {
110                 return;
111         }
112
113 #if defined(HAVE_EXPLICIT_BZERO)
114         explicit_bzero(buf, buflen);
115 #elif defined(HAVE_EXPLICIT_MEMSET)
116         explicit_memset(buf, 0, buflen);
117 #elif defined(HAVE_MEMSET_S)
118         errno_t err = memset_s(buf, buflen, 0, buflen);
119         assert(err == 0);
120 #elif defined(HAVE_WINDOWS)
121         SecureZeroMemory(buf, buflen);
122 #else
123         volatile uint8_t *p = buf;
124
125         while(buflen--) {
126                 *p++ = 0;
127         }
128
129 #endif
130 }
131
132 // Zero out a buffer of size `len` located at `ptr` and free() it.
133 // Does nothing if called on NULL.
134 static inline void xzfree(void *ptr, size_t len) {
135         if(ptr) {
136                 memzero(ptr, len);
137                 free(ptr);
138         }
139 }
140
141 // Zero out a NULL-terminated string using memzero() and then free it.
142 // Does nothing if called on NULL.
143 static inline void free_string(char *str) {
144         if(str) {
145                 xzfree(str, strlen(str));
146         }
147 }
148
149 #endif // TINC_XALLOC_H