Imported Upstream version 1.7.6p1
[debian/sudo] / alloc.c
1 /*
2  * Copyright (c) 1999-2005, 2007, 2010
3  *      Todd C. Miller <Todd.Miller@courtesan.com>
4  *
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.
8  *
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.
16  *
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.
20  */
21
22 #include <config.h>
23
24 #include <sys/types.h>
25 #include <sys/param.h>
26 #include <stdio.h>
27 #ifdef STDC_HEADERS
28 # include <stdlib.h>
29 # include <stddef.h>
30 #else
31 # ifdef HAVE_STDLIB_H
32 #  include <stdlib.h>
33 # endif
34 #endif /* STDC_HEADERS */
35 #ifdef HAVE_STRING_H
36 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
37 #  include <memory.h>
38 # endif
39 # include <string.h>
40 #endif /* HAVE_STRING_H */
41 #ifdef HAVE_STRINGS_H
42 # include <strings.h>
43 #endif /* HAVE_STRING_H */
44 #if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
45 # include <malloc.h>
46 #endif /* HAVE_MALLOC_H && !STDC_HEADERS */
47 #ifdef HAVE_INTTYPES_H
48 # include <inttypes.h>
49 #endif
50
51 #include "missing.h"
52 #include "alloc.h"
53 #include "error.h"
54
55 /*
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.
60  */
61 #ifndef SIZE_MAX
62 # ifdef SIZE_T_MAX
63 #  define SIZE_MAX      SIZE_T_MAX
64 # else
65 #  define SIZE_MAX      INT_MAX
66 # endif /* SIZE_T_MAX */
67 #endif /* SIZE_MAX */
68
69 /*
70  * emalloc() calls the system malloc(3) and exits with an error if
71  * malloc(3) fails.
72  */
73 void *
74 emalloc(size)
75     size_t size;
76 {
77     void *ptr;
78
79     if (size == 0)
80         errorx(1, "internal error, tried to emalloc(0)");
81
82     if ((ptr = malloc(size)) == NULL)
83         errorx(1, "unable to allocate memory");
84     return ptr;
85 }
86
87 /*
88  * emalloc2() allocates nmemb * size bytes and exits with an error
89  * if overflow would occur or if the system malloc(3) fails.
90  */
91 void *
92 emalloc2(nmemb, size)
93     size_t nmemb;
94     size_t size;
95 {
96     void *ptr;
97
98     if (nmemb == 0 || size == 0)
99         errorx(1, "internal error, tried to emalloc2(0)");
100     if (nmemb > SIZE_MAX / size)
101         errorx(1, "internal error, emalloc2() overflow");
102
103     size *= nmemb;
104     if ((ptr = malloc(size)) == NULL)
105         errorx(1, "unable to allocate memory");
106     return ptr;
107 }
108
109 /*
110  * erealloc() calls the system realloc(3) and exits with an error if
111  * realloc(3) fails.  You can call erealloc() with a NULL pointer even
112  * if the system realloc(3) does not support this.
113  */
114 void *
115 erealloc(ptr, size)
116     void *ptr;
117     size_t size;
118 {
119
120     if (size == 0)
121         errorx(1, "internal error, tried to erealloc(0)");
122
123     ptr = ptr ? realloc(ptr, size) : malloc(size);
124     if (ptr == NULL)
125         errorx(1, "unable to allocate memory");
126     return ptr;
127 }
128
129 /*
130  * erealloc3() realloc(3)s nmemb * size bytes and exits with an error
131  * if overflow would occur or if the system malloc(3)/realloc(3) fails.
132  * You can call erealloc() with a NULL pointer even if the system realloc(3)
133  * does not support this.
134  */
135 void *
136 erealloc3(ptr, nmemb, size)
137     void *ptr;
138     size_t nmemb;
139     size_t size;
140 {
141
142     if (nmemb == 0 || size == 0)
143         errorx(1, "internal error, tried to erealloc3(0)");
144     if (nmemb > SIZE_MAX / size)
145         errorx(1, "internal error, erealloc3() overflow");
146
147     size *= nmemb;
148     ptr = ptr ? realloc(ptr, size) : malloc(size);
149     if (ptr == NULL)
150         errorx(1, "unable to allocate memory");
151     return ptr;
152 }
153
154 /*
155  * estrdup() is like strdup(3) except that it exits with an error if
156  * malloc(3) fails.  NOTE: unlike strdup(3), estrdup(NULL) is legal.
157  */
158 char *
159 estrdup(src)
160     const char *src;
161 {
162     char *dst = NULL;
163     size_t len;
164
165     if (src != NULL) {
166         len = strlen(src);
167         dst = (char *) emalloc(len + 1);
168         (void) memcpy(dst, src, len);
169         dst[len] = '\0';
170     }
171     return dst;
172 }
173
174 /*
175  * easprintf() calls vasprintf() and exits with an error if vasprintf()
176  * returns -1 (out of memory).
177  */
178 int
179 #ifdef __STDC__
180 easprintf(char **ret, const char *fmt, ...)
181 #else
182 easprintf(ret, fmt, va_alist)
183     char **ret;
184     const char *fmt;
185     va_dcl
186 #endif
187 {
188     int len;
189     va_list ap;
190 #ifdef __STDC__
191     va_start(ap, fmt);
192 #else
193     va_start(ap);
194 #endif
195     len = vasprintf(ret, fmt, ap);
196     va_end(ap);
197
198     if (len == -1)
199         errorx(1, "unable to allocate memory");
200     return len;
201 }
202
203 /*
204  * evasprintf() calls vasprintf() and exits with an error if vasprintf()
205  * returns -1 (out of memory).
206  */
207 int
208 evasprintf(ret, format, args)
209     char **ret;
210     const char *format;
211     va_list args;
212 {
213     int len;
214
215     if ((len = vasprintf(ret, format, args)) == -1)
216         errorx(1, "unable to allocate memory");
217     return len;
218 }
219
220 /*
221  * Wrapper for free(3) so we can depend on C89 semantics.
222  */
223 void
224 efree(ptr)
225     void *ptr;
226 {
227     if (ptr != NULL)
228         free(ptr);
229 }