]> eyrie.org Git - kerberos/krb5-strength.git/blob - util/xmalloc.c
Declare fast forward from 3.1-2
[kerberos/krb5-strength.git] / util / xmalloc.c
1 /*
2  * malloc routines with failure handling.
3  *
4  * Usage:
5  *
6  *      extern xmalloc_handler_t memory_error;
7  *      extern const char *string;
8  *      char *buffer;
9  *      va_list args;
10  *
11  *      xmalloc_error_handler = memory_error;
12  *      buffer = xmalloc(1024);
13  *      xrealloc(buffer, 2048);
14  *      free(buffer);
15  *      buffer = xcalloc(1, 1024);
16  *      free(buffer);
17  *      buffer = xstrdup(string);
18  *      free(buffer);
19  *      buffer = xstrndup(string, 25);
20  *      free(buffer);
21  *      xasprintf(&buffer, "%s", "some string");
22  *      free(buffer);
23  *      xvasprintf(&buffer, "%s", args);
24  *
25  * xmalloc, xcalloc, xrealloc, and xstrdup behave exactly like their C library
26  * counterparts without the leading x except that they will never return NULL.
27  * Instead, on error, they call xmalloc_error_handler, passing it the name of
28  * the function whose memory allocation failed, the amount of the allocation,
29  * and the file and line number where the allocation function was invoked
30  * (from __FILE__ and __LINE__).  This function may do whatever it wishes,
31  * such as some action to free up memory or a call to sleep to hope that
32  * system resources return.  If the handler returns, the interrupted memory
33  * allocation function will try its allocation again (calling the handler
34  * again if it still fails).
35  *
36  * xreallocarray behaves the same as the OpenBSD reallocarray function but for
37  * the same error checking, which in turn is the same as realloc but with
38  * calloc-style arguments and size overflow checking.
39  *
40  * xstrndup behaves like xstrdup but only copies the given number of
41  * characters.  It allocates an additional byte over its second argument and
42  * always nul-terminates the string.
43  *
44  * xasprintf and xvasprintf behave just like their GNU glibc library
45  * implementations except that they do the same checking as described above.
46  * xasprintf will only be able to provide accurate file and line information
47  * on systems that support variadic macros.
48  *
49  * The default error handler, if none is set by the caller, prints an error
50  * message to stderr and exits with exit status 1.  An error handler must take
51  * a const char * (function name), size_t (bytes allocated), const char *
52  * (file), and int (line).
53  *
54  * xmalloc will return a pointer to a valid memory region on an xmalloc of 0
55  * bytes, ensuring this by allocating space for one character instead of 0
56  * bytes.
57  *
58  * The functions defined here are actually x_malloc, x_realloc, etc.  The
59  * header file defines macros named xmalloc, etc. that pass the file name and
60  * line number to these functions.
61  *
62  * The canonical version of this file is maintained in the rra-c-util package,
63  * which can be found at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
64  *
65  * Written by Russ Allbery <eagle@eyrie.org>
66  * Copyright 2015 Russ Allbery <eagle@eyrie.org>
67  * Copyright 2012-2014
68  *     The Board of Trustees of the Leland Stanford Junior University
69  * Copyright 2004-2006 Internet Systems Consortium, Inc. ("ISC")
70  * Copyright 1991, 1994-2003 The Internet Software Consortium and Rich Salz
71  *
72  * This code is derived from software contributed to the Internet Software
73  * Consortium by Rich Salz.
74  *
75  * Permission to use, copy, modify, and distribute this software for any
76  * purpose with or without fee is hereby granted, provided that the above
77  * copyright notice and this permission notice appear in all copies.
78  *
79  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
80  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
81  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
82  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
83  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
84  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
85  * PERFORMANCE OF THIS SOFTWARE.
86  *
87  * SPDX-License-Identifier: ISC
88  */
89
90 #include <config.h>
91 #include <portable/system.h>
92
93 #include <util/messages.h>
94 #include <util/xmalloc.h>
95
96
97 /*
98  * The default error handler.
99  */
100 void
101 xmalloc_fail(const char *function, size_t size, const char *file, int line)
102 {
103     if (size == 0)
104         sysdie("failed to format output with %s at %s line %d", function, file,
105                line);
106     else
107         sysdie("failed to %s %lu bytes at %s line %d", function,
108                (unsigned long) size, file, line);
109 }
110
111 /* Assign to this variable to choose a handler other than the default. */
112 xmalloc_handler_type xmalloc_error_handler = xmalloc_fail;
113
114
115 void *
116 x_malloc(size_t size, const char *file, int line)
117 {
118     void *p;
119     size_t real_size;
120
121     real_size = (size > 0) ? size : 1;
122     p = malloc(real_size);
123     while (p == NULL) {
124         (*xmalloc_error_handler)("malloc", size, file, line);
125         p = malloc(real_size);
126     }
127     return p;
128 }
129
130
131 void *
132 x_calloc(size_t n, size_t size, const char *file, int line)
133 {
134     void *p;
135
136     n = (n > 0) ? n : 1;
137     size = (size > 0) ? size : 1;
138     p = calloc(n, size);
139     while (p == NULL) {
140         (*xmalloc_error_handler)("calloc", n * size, file, line);
141         p = calloc(n, size);
142     }
143     return p;
144 }
145
146
147 void *
148 x_realloc(void *p, size_t size, const char *file, int line)
149 {
150     void *newp;
151
152     newp = realloc(p, size);
153     while (newp == NULL && size > 0) {
154         (*xmalloc_error_handler)("realloc", size, file, line);
155         newp = realloc(p, size);
156     }
157     return newp;
158 }
159
160
161 void *
162 x_reallocarray(void *p, size_t n, size_t size, const char *file, int line)
163 {
164     void *newp;
165
166     newp = reallocarray(p, n, size);
167     while (newp == NULL && size > 0 && n > 0) {
168         (*xmalloc_error_handler)("reallocarray", n * size, file, line);
169         newp = reallocarray(p, n, size);
170     }
171     return newp;
172 }
173
174
175 char *
176 x_strdup(const char *s, const char *file, int line)
177 {
178     char *p;
179     size_t len;
180
181     len = strlen(s) + 1;
182     p = malloc(len);
183     while (p == NULL) {
184         (*xmalloc_error_handler)("strdup", len, file, line);
185         p = malloc(len);
186     }
187     memcpy(p, s, len);
188     return p;
189 }
190
191
192 /*
193  * Avoid using the system strndup function since it may not exist (on Mac OS
194  * X, for example), and there's no need to introduce another portability
195  * requirement.
196  */
197 char *
198 x_strndup(const char *s, size_t size, const char *file, int line)
199 {
200     const char *p;
201     size_t length;
202     char *copy;
203
204     /* Don't assume that the source string is nul-terminated. */
205     for (p = s; (size_t)(p - s) < size && *p != '\0'; p++)
206         ;
207     length = p - s;
208     copy = malloc(length + 1);
209     while (copy == NULL) {
210         (*xmalloc_error_handler)("strndup", length + 1, file, line);
211         copy = malloc(length + 1);
212     }
213     memcpy(copy, s, length);
214     copy[length] = '\0';
215     return copy;
216 }
217
218
219 void
220 x_vasprintf(char **strp, const char *fmt, va_list args, const char *file,
221             int line)
222 {
223     va_list args_copy;
224     int status;
225
226     va_copy(args_copy, args);
227     status = vasprintf(strp, fmt, args_copy);
228     va_end(args_copy);
229     while (status < 0) {
230         va_copy(args_copy, args);
231         status = vsnprintf(NULL, 0, fmt, args_copy);
232         va_end(args_copy);
233         status = (status < 0) ? 0 : status + 1;
234         (*xmalloc_error_handler)("vasprintf", status, file, line);
235         va_copy(args_copy, args);
236         status = vasprintf(strp, fmt, args_copy);
237         va_end(args_copy);
238     }
239 }
240
241
242 #if HAVE_C99_VAMACROS || HAVE_GNU_VAMACROS
243 void
244 x_asprintf(char **strp, const char *file, int line, const char *fmt, ...)
245 {
246     va_list args, args_copy;
247     int status;
248
249     va_start(args, fmt);
250     va_copy(args_copy, args);
251     status = vasprintf(strp, fmt, args_copy);
252     va_end(args_copy);
253     while (status < 0) {
254         va_copy(args_copy, args);
255         status = vsnprintf(NULL, 0, fmt, args_copy);
256         va_end(args_copy);
257         status = (status < 0) ? 0 : status + 1;
258         (*xmalloc_error_handler)("asprintf", status, file, line);
259         va_copy(args_copy, args);
260         status = vasprintf(strp, fmt, args_copy);
261         va_end(args_copy);
262     }
263     va_end(args);
264 }
265 #else  /* !(HAVE_C99_VAMACROS || HAVE_GNU_VAMACROS) */
266 void
267 x_asprintf(char **strp, const char *fmt, ...)
268 {
269     va_list args, args_copy;
270     int status;
271
272     va_start(args, fmt);
273     va_copy(args_copy, args);
274     status = vasprintf(strp, fmt, args_copy);
275     va_end(args_copy);
276     while (status < 0) {
277         va_copy(args_copy, args);
278         status = vsnprintf(NULL, 0, fmt, args_copy);
279         va_end(args_copy);
280         status = (status < 0) ? 0 : status + 1;
281         (*xmalloc_error_handler)("asprintf", status, __FILE__, __LINE__);
282         va_copy(args_copy, args);
283         status = vasprintf(strp, fmt, args_copy);
284         va_end(args_copy);
285     }
286     va_end(args);
287 }
288 #endif /* !(HAVE_C99_VAMACROS || HAVE_GNU_VAMACROS) */