2 * Copyright (c) 1999-2005, 2007, 2010-2011
3 * Todd C. Miller <Todd.Miller@courtesan.com>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * Sponsored in part by the Defense Advanced Research Projects
18 * Agency (DARPA) and Air Force Research Laboratory, Air Force
19 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
24 #include <sys/types.h>
25 #include <sys/param.h>
34 #endif /* STDC_HEADERS */
36 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
40 #endif /* HAVE_STRING_H */
43 #endif /* HAVE_STRING_H */
44 #if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
46 #endif /* HAVE_MALLOC_H && !STDC_HEADERS */
47 #ifdef HAVE_INTTYPES_H
48 # include <inttypes.h>
56 * If there is no SIZE_MAX or SIZE_T_MAX we have to assume that size_t
57 * could be signed (as it is on SunOS 4.x). This just means that
58 * emalloc2() and erealloc3() cannot allocate huge amounts on such a
59 * platform but that is OK since sudo doesn't need to do so anyway.
63 # define SIZE_MAX SIZE_T_MAX
65 # define SIZE_MAX INT_MAX
66 # endif /* SIZE_T_MAX */
70 * emalloc() calls the system malloc(3) and exits with an error if
79 errorx(1, "internal error, tried to emalloc(0)");
81 if ((ptr = malloc(size)) == NULL)
82 errorx(1, "unable to allocate memory");
87 * emalloc2() allocates nmemb * size bytes and exits with an error
88 * if overflow would occur or if the system malloc(3) fails.
91 emalloc2(size_t nmemb, size_t size)
95 if (nmemb == 0 || size == 0)
96 errorx(1, "internal error, tried to emalloc2(0)");
97 if (nmemb > SIZE_MAX / size)
98 errorx(1, "internal error, emalloc2() overflow");
101 if ((ptr = malloc(size)) == NULL)
102 errorx(1, "unable to allocate memory");
107 * erealloc() calls the system realloc(3) and exits with an error if
108 * realloc(3) fails. You can call erealloc() with a NULL pointer even
109 * if the system realloc(3) does not support this.
112 erealloc(void *ptr, size_t size)
116 errorx(1, "internal error, tried to erealloc(0)");
118 ptr = ptr ? realloc(ptr, size) : malloc(size);
120 errorx(1, "unable to allocate memory");
125 * erealloc3() realloc(3)s nmemb * size bytes and exits with an error
126 * if overflow would occur or if the system malloc(3)/realloc(3) fails.
127 * You can call erealloc() with a NULL pointer even if the system realloc(3)
128 * does not support this.
131 erealloc3(void *ptr, size_t nmemb, size_t size)
134 if (nmemb == 0 || size == 0)
135 errorx(1, "internal error, tried to erealloc3(0)");
136 if (nmemb > SIZE_MAX / size)
137 errorx(1, "internal error, erealloc3() overflow");
140 ptr = ptr ? realloc(ptr, size) : malloc(size);
142 errorx(1, "unable to allocate memory");
147 * estrdup() is like strdup(3) except that it exits with an error if
148 * malloc(3) fails. NOTE: unlike strdup(3), estrdup(NULL) is legal.
151 estrdup(const char *src)
158 dst = (char *) emalloc(len + 1);
159 (void) memcpy(dst, src, len);
166 * estrdup() is like strndup(3) except that it exits with an error if
167 * malloc(3) fails. NOTE: unlike strdup(3), estrdup(NULL) is legal.
170 estrndup(const char *src, size_t maxlen)
179 dst = (char *) emalloc(len + 1);
180 (void) memcpy(dst, src, len);
187 * easprintf() calls vasprintf() and exits with an error if vasprintf()
188 * returns -1 (out of memory).
191 easprintf(char **ret, const char *fmt, ...)
196 len = vasprintf(ret, fmt, ap);
200 errorx(1, "unable to allocate memory");
205 * evasprintf() calls vasprintf() and exits with an error if vasprintf()
206 * returns -1 (out of memory).
209 evasprintf(char **ret, const char *format, va_list args)
213 if ((len = vasprintf(ret, format, args)) == -1)
214 errorx(1, "unable to allocate memory");
219 * Wrapper for free(3) so we can depend on C89 semantics.