add doc about interaction with RAMRUN to README.Debian in response to #581393
[debian/sudo] / alloc.c
1 /*
2  * Copyright (c) 1999-2005, 2007
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 # include <string.h>
37 #else
38 # ifdef HAVE_STRINGS_H
39 #  include <strings.h>
40 # endif
41 #endif /* HAVE_STRING_H */
42 #if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
43 # include <malloc.h>
44 #endif /* HAVE_MALLOC_H && !STDC_HEADERS */
45 #ifdef HAVE_INTTYPES_H
46 # include <inttypes.h>
47 #endif
48
49 #include "sudo.h"
50
51 /*
52  * If there is no SIZE_MAX or SIZE_T_MAX we have to assume that size_t
53  * could be signed (as it is on SunOS 4.x).  This just means that
54  * emalloc2() and erealloc3() cannot allocate huge amounts on such a
55  * platform but that is OK since sudo doesn't need to do so anyway.
56  */
57 #ifndef SIZE_MAX
58 # ifdef SIZE_T_MAX
59 #  define SIZE_MAX      SIZE_T_MAX
60 # else
61 #  define SIZE_MAX      INT_MAX
62 # endif /* SIZE_T_MAX */
63 #endif /* SIZE_MAX */
64
65 /*
66  * emalloc() calls the system malloc(3) and exits with an error if
67  * malloc(3) fails.
68  */
69 void *
70 emalloc(size)
71     size_t size;
72 {
73     void *ptr;
74
75     if (size == 0)
76         errorx(1, "internal error, tried to emalloc(0)");
77
78     if ((ptr = malloc(size)) == NULL)
79         errorx(1, "unable to allocate memory");
80     return(ptr);
81 }
82
83 /*
84  * emalloc2() allocates nmemb * size bytes and exits with an error
85  * if overflow would occur or if the system malloc(3) fails.
86  */
87 void *
88 emalloc2(nmemb, size)
89     size_t nmemb;
90     size_t size;
91 {
92     void *ptr;
93
94     if (nmemb == 0 || size == 0)
95         errorx(1, "internal error, tried to emalloc2(0)");
96     if (nmemb > SIZE_MAX / size)
97         errorx(1, "internal error, emalloc2() overflow");
98
99     size *= nmemb;
100     if ((ptr = malloc(size)) == NULL)
101         errorx(1, "unable to allocate memory");
102     return(ptr);
103 }
104
105 /*
106  * erealloc() calls the system realloc(3) and exits with an error if
107  * realloc(3) fails.  You can call erealloc() with a NULL pointer even
108  * if the system realloc(3) does not support this.
109  */
110 void *
111 erealloc(ptr, size)
112     void *ptr;
113     size_t size;
114 {
115
116     if (size == 0)
117         errorx(1, "internal error, tried to erealloc(0)");
118
119     ptr = ptr ? realloc(ptr, size) : malloc(size);
120     if (ptr == NULL)
121         errorx(1, "unable to allocate memory");
122     return(ptr);
123 }
124
125 /*
126  * erealloc3() realloc(3)s nmemb * size bytes and exits with an error
127  * if overflow would occur or if the system malloc(3)/realloc(3) fails.
128  * You can call erealloc() with a NULL pointer even if the system realloc(3)
129  * does not support this.
130  */
131 void *
132 erealloc3(ptr, nmemb, size)
133     void *ptr;
134     size_t nmemb;
135     size_t size;
136 {
137
138     if (nmemb == 0 || size == 0)
139         errorx(1, "internal error, tried to erealloc3(0)");
140     if (nmemb > SIZE_MAX / size)
141         errorx(1, "internal error, erealloc3() overflow");
142
143     size *= nmemb;
144     ptr = ptr ? realloc(ptr, size) : malloc(size);
145     if (ptr == NULL)
146         errorx(1, "unable to allocate memory");
147     return(ptr);
148 }
149
150 /*
151  * estrdup() is like strdup(3) except that it exits with an error if
152  * malloc(3) fails.  NOTE: unlike strdup(3), estrdup(NULL) is legal.
153  */
154 char *
155 estrdup(src)
156     const char *src;
157 {
158     char *dst = NULL;
159     size_t size;
160
161     if (src != NULL) {
162         size = strlen(src) + 1;
163         dst = (char *) emalloc(size);
164         (void) memcpy(dst, src, size);
165     }
166     return(dst);
167 }
168
169 /*
170  * easprintf() calls vasprintf() and exits with an error if vasprintf()
171  * returns -1 (out of memory).
172  */
173 int
174 #ifdef __STDC__
175 easprintf(char **ret, const char *fmt, ...)
176 #else
177 easprintf(ret, fmt, va_alist)
178     char **ret;
179     const char *fmt;
180     va_dcl
181 #endif
182 {
183     int len;
184     va_list ap;
185 #ifdef __STDC__
186     va_start(ap, fmt);
187 #else
188     va_start(ap);
189 #endif
190     len = vasprintf(ret, fmt, ap);
191     va_end(ap);
192
193     if (len == -1)
194         errorx(1, "unable to allocate memory");
195     return(len);
196 }
197
198 /*
199  * evasprintf() calls vasprintf() and exits with an error if vasprintf()
200  * returns -1 (out of memory).
201  */
202 int
203 evasprintf(ret, format, args)
204     char **ret;
205     const char *format;
206     va_list args;
207 {
208     int len;
209
210     if ((len = vasprintf(ret, format, args)) == -1)
211         errorx(1, "unable to allocate memory");
212     return(len);
213 }
214
215 /*
216  * Wrapper for free(3) so we can depend on C89 semantics.
217  */
218 void
219 efree(ptr)
220     void *ptr;
221 {
222     if (ptr != NULL)
223         free(ptr);
224 }