2 * Copyright (c) 1999-2003 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 */
53 static const char rcsid[] = "$Sudo: alloc.c,v 1.23 2004/06/01 16:23:32 millert Exp $";
57 * If there is no SIZE_MAX or SIZE_T_MAX we have to assume that size_t
58 * could be signed (as it is on SunOS 4.x). This just means that
59 * emalloc2() and erealloc3() cannot allocate huge amounts on such a
60 * platform but that is OK since sudo doesn't need to do so anyway.
64 # define SIZE_MAX SIZE_T_MAX
66 # define SIZE_MAX INT_MAX
67 # endif /* SIZE_T_MAX */
71 * emalloc() calls the system malloc(3) and exits with an error if
81 errx(1, "internal error, tried to emalloc(0)");
83 if ((ptr = (VOID *) malloc(size)) == NULL)
84 errx(1, "unable to allocate memory");
89 * emalloc2() allocates nmemb * size bytes and exits with an error
90 * if overflow would occur or if the system malloc(3) fails.
99 if (nmemb == 0 || size == 0)
100 errx(1, "internal error, tried to emalloc2(0)");
101 if (nmemb > SIZE_MAX / size)
102 errx(1, "internal error, emalloc2() overflow");
105 if ((ptr = (VOID *) malloc(size)) == NULL)
106 errx(1, "unable to allocate memory");
111 * erealloc() calls the system realloc(3) and exits with an error if
112 * realloc(3) fails. You can call erealloc() with a NULL pointer even
113 * if the system realloc(3) does not support this.
122 errx(1, "internal error, tried to erealloc(0)");
124 ptr = ptr ? (VOID *) realloc(ptr, size) : (VOID *) malloc(size);
126 errx(1, "unable to allocate memory");
131 * erealloc3() realloc(3)s nmemb * size bytes and exits with an error
132 * if overflow would occur or if the system malloc(3)/realloc(3) fails.
133 * You can call erealloc() with a NULL pointer even if the system realloc(3)
134 * does not support this.
137 erealloc3(ptr, nmemb, size)
143 if (nmemb == 0 || size == 0)
144 errx(1, "internal error, tried to erealloc3(0)");
145 if (nmemb > SIZE_MAX / size)
146 errx(1, "internal error, erealloc3() overflow");
149 ptr = ptr ? (VOID *) realloc(ptr, size) : (VOID *) malloc(size);
151 errx(1, "unable to allocate memory");
156 * estrdup() is like strdup(3) except that it exits with an error if
157 * malloc(3) fails. NOTE: unlike strdup(3), estrdup(NULL) is legal.
167 size = strlen(src) + 1;
168 dst = (char *) emalloc(size);
169 (void) memcpy(dst, src, size);
175 * easprintf() calls vasprintf() and exits with an error if vasprintf()
176 * returns -1 (out of memory).
180 easprintf(char **ret, const char *fmt, ...)
195 ret = va_arg(ap, char **);
196 fmt = va_arg(ap, const char *);
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");