2 * Copyright (c) 1999-2005 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 * Sponsored in part by the Defense Advanced Research Projects
17 * Agency (DARPA) and Air Force Research Laboratory, Air Force
18 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
23 #include <sys/types.h>
24 #include <sys/param.h>
33 #endif /* STDC_HEADERS */
37 # ifdef HAVE_STRINGS_H
40 #endif /* HAVE_STRING_H */
41 #if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
43 #endif /* HAVE_MALLOC_H && !STDC_HEADERS */
47 # include "emul/err.h"
48 #endif /* HAVE_ERR_H */
49 #ifdef HAVE_INTTYPES_H
50 # include <inttypes.h>
56 __unused static const char rcsid[] = "$Sudo: alloc.c,v 1.23.2.4 2007/09/11 12:20:15 millert Exp $";
60 * If there is no SIZE_MAX or SIZE_T_MAX we have to assume that size_t
61 * could be signed (as it is on SunOS 4.x). This just means that
62 * emalloc2() and erealloc3() cannot allocate huge amounts on such a
63 * platform but that is OK since sudo doesn't need to do so anyway.
67 # define SIZE_MAX SIZE_T_MAX
69 # define SIZE_MAX INT_MAX
70 # endif /* SIZE_T_MAX */
74 * emalloc() calls the system malloc(3) and exits with an error if
84 errx(1, "internal error, tried to emalloc(0)");
86 if ((ptr = (VOID *) malloc(size)) == NULL)
87 errx(1, "unable to allocate memory");
92 * emalloc2() allocates nmemb * size bytes and exits with an error
93 * if overflow would occur or if the system malloc(3) fails.
102 if (nmemb == 0 || size == 0)
103 errx(1, "internal error, tried to emalloc2(0)");
104 if (nmemb > SIZE_MAX / size)
105 errx(1, "internal error, emalloc2() overflow");
108 if ((ptr = (VOID *) malloc(size)) == NULL)
109 errx(1, "unable to allocate memory");
114 * erealloc() calls the system realloc(3) and exits with an error if
115 * realloc(3) fails. You can call erealloc() with a NULL pointer even
116 * if the system realloc(3) does not support this.
125 errx(1, "internal error, tried to erealloc(0)");
127 ptr = ptr ? (VOID *) realloc(ptr, size) : (VOID *) malloc(size);
129 errx(1, "unable to allocate memory");
134 * erealloc3() realloc(3)s nmemb * size bytes and exits with an error
135 * if overflow would occur or if the system malloc(3)/realloc(3) fails.
136 * You can call erealloc() with a NULL pointer even if the system realloc(3)
137 * does not support this.
140 erealloc3(ptr, nmemb, size)
146 if (nmemb == 0 || size == 0)
147 errx(1, "internal error, tried to erealloc3(0)");
148 if (nmemb > SIZE_MAX / size)
149 errx(1, "internal error, erealloc3() overflow");
152 ptr = ptr ? (VOID *) realloc(ptr, size) : (VOID *) malloc(size);
154 errx(1, "unable to allocate memory");
159 * estrdup() is like strdup(3) except that it exits with an error if
160 * malloc(3) fails. NOTE: unlike strdup(3), estrdup(NULL) is legal.
170 size = strlen(src) + 1;
171 dst = (char *) emalloc(size);
172 (void) memcpy(dst, src, size);
178 * easprintf() calls vasprintf() and exits with an error if vasprintf()
179 * returns -1 (out of memory).
183 easprintf(char **ret, const char *fmt, ...)
185 easprintf(ret, fmt, va_alist)
198 len = vasprintf(ret, fmt, ap);
202 errx(1, "unable to allocate memory");
207 * evasprintf() calls vasprintf() and exits with an error if vasprintf()
208 * returns -1 (out of memory).
211 evasprintf(ret, format, args)
218 if ((len = vasprintf(ret, format, args)) == -1)
219 errx(1, "unable to allocate memory");
224 * Wrapper for free(3) so we can depend on C89 semantics.