]> eyrie.org Git - kerberos/krb5-strength.git/blob - util/xmalloc.c
Simplify TinyCDB Autoconf probes
[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(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  * xstrndup behaves like xstrdup but only copies the given number of
37  * characters.  It allocates an additional byte over its second argument and
38  * always nul-terminates the string.
39  *
40  * xasprintf and xvasprintf behave just like their GNU glibc library
41  * implementations except that they do the same checking as described above.
42  * xasprintf will only be able to provide accurate file and line information
43  * on systems that support variadic macros.
44  *
45  * The default error handler, if none is set by the caller, prints an error
46  * message to stderr and exits with exit status 1.  An error handler must take
47  * a const char * (function name), size_t (bytes allocated), const char *
48  * (file), and int (line).
49  *
50  * xmalloc will return a pointer to a valid memory region on an xmalloc of 0
51  * bytes, ensuring this by allocating space for one character instead of 0
52  * bytes.
53  *
54  * The functions defined here are actually x_malloc, x_realloc, etc.  The
55  * header file defines macros named xmalloc, etc. that pass the file name and
56  * line number to these functions.
57  *
58  * The canonical version of this file is maintained in the rra-c-util package,
59  * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
60  *
61  * Copyright 2012
62  *     The Board of Trustees of the Leland Stanford Junior University
63  * Copyright (c) 2004, 2005, 2006
64  *     by Internet Systems Consortium, Inc. ("ISC")
65  * Copyright (c) 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
66  *     2002, 2003 by The Internet Software Consortium and Rich Salz
67  *
68  * This code is derived from software contributed to the Internet Software
69  * Consortium by Rich Salz.
70  *
71  * Permission to use, copy, modify, and distribute this software for any
72  * purpose with or without fee is hereby granted, provided that the above
73  * copyright notice and this permission notice appear in all copies.
74  *
75  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
76  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
77  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
78  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
79  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
80  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
81  * PERFORMANCE OF THIS SOFTWARE.
82  */
83
84 #include <config.h>
85 #include <portable/system.h>
86
87 #include <util/messages.h>
88 #include <util/xmalloc.h>
89
90
91 /*
92  * The default error handler.
93  */
94 void
95 xmalloc_fail(const char *function, size_t size, const char *file, int line)
96 {
97     sysdie("failed to %s %lu bytes at %s line %d", function,
98            (unsigned long) size, file, line);
99 }
100
101 /* Assign to this variable to choose a handler other than the default. */
102 xmalloc_handler_type xmalloc_error_handler = xmalloc_fail;
103
104
105 void *
106 x_malloc(size_t size, const char *file, int line)
107 {
108     void *p;
109     size_t real_size;
110
111     real_size = (size > 0) ? size : 1;
112     p = malloc(real_size);
113     while (p == NULL) {
114         (*xmalloc_error_handler)("malloc", size, file, line);
115         p = malloc(real_size);
116     }
117     return p;
118 }
119
120
121 void *
122 x_calloc(size_t n, size_t size, const char *file, int line)
123 {
124     void *p;
125
126     n = (n > 0) ? n : 1;
127     size = (size > 0) ? size : 1;
128     p = calloc(n, size);
129     while (p == NULL) {
130         (*xmalloc_error_handler)("calloc", n * size, file, line);
131         p = calloc(n, size);
132     }
133     return p;
134 }
135
136
137 void *
138 x_realloc(void *p, size_t size, const char *file, int line)
139 {
140     void *newp;
141
142     newp = realloc(p, size);
143     while (newp == NULL && size > 0) {
144         (*xmalloc_error_handler)("realloc", size, file, line);
145         newp = realloc(p, size);
146     }
147     return newp;
148 }
149
150
151 char *
152 x_strdup(const char *s, const char *file, int line)
153 {
154     char *p;
155     size_t len;
156
157     len = strlen(s) + 1;
158     p = malloc(len);
159     while (p == NULL) {
160         (*xmalloc_error_handler)("strdup", len, file, line);
161         p = malloc(len);
162     }
163     memcpy(p, s, len);
164     return p;
165 }
166
167
168 /*
169  * Avoid using the system strndup function since it may not exist (on Mac OS
170  * X, for example), and there's no need to introduce another portability
171  * requirement.
172  */
173 char *
174 x_strndup(const char *s, size_t size, const char *file, int line)
175 {
176     const char *p;
177     size_t length;
178     char *copy;
179
180     /* Don't assume that the source string is nul-terminated. */
181     for (p = s; (size_t) (p - s) < size && *p != '\0'; p++)
182         ;
183     length = p - s;
184     copy = malloc(length + 1);
185     while (copy == NULL) {
186         (*xmalloc_error_handler)("strndup", length + 1, file, line);
187         copy = malloc(length + 1);
188     }
189     memcpy(copy, s, length);
190     copy[length] = '\0';
191     return copy;
192 }
193
194
195 void
196 x_vasprintf(char **strp, const char *fmt, va_list args, const char *file,
197             int line)
198 {
199     va_list args_copy;
200     int status;
201
202     va_copy(args_copy, args);
203     status = vasprintf(strp, fmt, args_copy);
204     va_end(args_copy);
205     while (status < 0) {
206         va_copy(args_copy, args);
207         status = vsnprintf(NULL, 0, fmt, args_copy);
208         va_end(args_copy);
209         (*xmalloc_error_handler)("vasprintf", status + 1, file, line);
210         va_copy(args_copy, args);
211         status = vasprintf(strp, fmt, args_copy);
212         va_end(args_copy);
213     }
214 }
215
216
217 #if HAVE_C99_VAMACROS || HAVE_GNU_VAMACROS
218 void
219 x_asprintf(char **strp, const char *file, int line, const char *fmt, ...)
220 {
221     va_list args, args_copy;
222     int status;
223
224     va_start(args, fmt);
225     va_copy(args_copy, args);
226     status = vasprintf(strp, fmt, args_copy);
227     va_end(args_copy);
228     while (status < 0) {
229         va_copy(args_copy, args);
230         status = vsnprintf(NULL, 0, fmt, args_copy);
231         va_end(args_copy);
232         (*xmalloc_error_handler)("asprintf", status + 1, file, line);
233         va_copy(args_copy, args);
234         status = vasprintf(strp, fmt, args_copy);
235         va_end(args_copy);
236     }
237 }
238 #else /* !(HAVE_C99_VAMACROS || HAVE_GNU_VAMACROS) */
239 void
240 x_asprintf(char **strp, const char *fmt, ...)
241 {
242     va_list args, args_copy;
243     int status;
244
245     va_start(args, fmt);
246     va_copy(args_copy, args);
247     status = vasprintf(strp, fmt, args_copy);
248     va_end(args_copy);
249     while (status < 0) {
250         va_copy(args_copy, args);
251         status = vsnprintf(NULL, 0, fmt, args_copy);
252         va_end(args_copy);
253         (*xmalloc_error_handler)("asprintf", status + 1, __FILE__, __LINE__);
254         va_copy(args_copy, args);
255         status = vasprintf(strp, fmt, args_copy);
256         va_end(args_copy);
257     }
258 }
259 #endif /* !(HAVE_C99_VAMACROS || HAVE_GNU_VAMACROS) */