Annotation of embedaddon/sudo/common/alloc.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * Copyright (c) 1999-2005, 2007, 2010-2011
                      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: #define DEFAULT_TEXT_DOMAIN    "sudo"
                     56: #include "gettext.h"
                     57: 
                     58: /*
                     59:  * If there is no SIZE_MAX or SIZE_T_MAX we have to assume that size_t
                     60:  * could be signed (as it is on SunOS 4.x).  This just means that
                     61:  * emalloc2() and erealloc3() cannot allocate huge amounts on such a
                     62:  * platform but that is OK since sudo doesn't need to do so anyway.
                     63:  */
                     64: #ifndef SIZE_MAX
                     65: # ifdef SIZE_T_MAX
                     66: #  define SIZE_MAX     SIZE_T_MAX
                     67: # else
                     68: #  define SIZE_MAX     INT_MAX
                     69: # endif /* SIZE_T_MAX */
                     70: #endif /* SIZE_MAX */
                     71: 
                     72: /*
                     73:  * emalloc() calls the system malloc(3) and exits with an error if
                     74:  * malloc(3) fails.
                     75:  */
                     76: void *
                     77: emalloc(size_t size)
                     78: {
                     79:     void *ptr;
                     80: 
                     81:     if (size == 0)
                     82:        errorx(1, _("internal error, tried to emalloc(0)"));
                     83: 
                     84:     if ((ptr = malloc(size)) == NULL)
                     85:        errorx(1, _("unable to allocate memory"));
                     86:     return ptr;
                     87: }
                     88: 
                     89: /*
                     90:  * emalloc2() allocates nmemb * size bytes and exits with an error
                     91:  * if overflow would occur or if the system malloc(3) fails.
                     92:  */
                     93: void *
                     94: emalloc2(size_t nmemb, 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(void *ptr, size_t size)
                    116: {
                    117: 
                    118:     if (size == 0)
                    119:        errorx(1, _("internal error, tried to erealloc(0)"));
                    120: 
                    121:     ptr = ptr ? realloc(ptr, size) : malloc(size);
                    122:     if (ptr == NULL)
                    123:        errorx(1, _("unable to allocate memory"));
                    124:     return ptr;
                    125: }
                    126: 
                    127: /*
                    128:  * erealloc3() realloc(3)s nmemb * size bytes and exits with an error
                    129:  * if overflow would occur or if the system malloc(3)/realloc(3) fails.
                    130:  * You can call erealloc() with a NULL pointer even if the system realloc(3)
                    131:  * does not support this.
                    132:  */
                    133: void *
                    134: erealloc3(void *ptr, size_t nmemb, size_t size)
                    135: {
                    136: 
                    137:     if (nmemb == 0 || size == 0)
                    138:        errorx(1, _("internal error, tried to erealloc3(0)"));
                    139:     if (nmemb > SIZE_MAX / size)
                    140:        errorx(1, _("internal error, erealloc3() overflow"));
                    141: 
                    142:     size *= nmemb;
                    143:     ptr = ptr ? realloc(ptr, size) : malloc(size);
                    144:     if (ptr == NULL)
                    145:        errorx(1, _("unable to allocate memory"));
                    146:     return ptr;
                    147: }
                    148: 
                    149: /*
                    150:  * estrdup() is like strdup(3) except that it exits with an error if
                    151:  * malloc(3) fails.  NOTE: unlike strdup(3), estrdup(NULL) is legal.
                    152:  */
                    153: char *
                    154: estrdup(const char *src)
                    155: {
                    156:     char *dst = NULL;
                    157:     size_t len;
                    158: 
                    159:     if (src != NULL) {
                    160:        len = strlen(src);
                    161:        dst = (char *) emalloc(len + 1);
                    162:        (void) memcpy(dst, src, len);
                    163:        dst[len] = '\0';
                    164:     }
                    165:     return dst;
                    166: }
                    167: 
                    168: /*
                    169:  * estrdup() is like strndup(3) except that it exits with an error if
                    170:  * malloc(3) fails.  NOTE: unlike strdup(3), estrdup(NULL) is legal.
                    171:  */
                    172: char *
                    173: estrndup(const char *src, size_t maxlen)
                    174: {
                    175:     char *dst = NULL;
                    176:     size_t len;
                    177: 
                    178:     if (src != NULL) {
                    179:        len = strlen(src);
                    180:        if (len > maxlen)
                    181:            len = maxlen;
                    182:        dst = (char *) emalloc(len + 1);
                    183:        (void) memcpy(dst, src, len);
                    184:        dst[len] = '\0';
                    185:     }
                    186:     return dst;
                    187: }
                    188: 
                    189: /*
                    190:  * easprintf() calls vasprintf() and exits with an error if vasprintf()
                    191:  * returns -1 (out of memory).
                    192:  */
                    193: int
                    194: easprintf(char **ret, const char *fmt, ...)
                    195: {
                    196:     int len;
                    197:     va_list ap;
                    198:     va_start(ap, fmt);
                    199:     len = vasprintf(ret, fmt, ap);
                    200:     va_end(ap);
                    201: 
                    202:     if (len == -1)
                    203:        errorx(1, _("unable to allocate memory"));
                    204:     return len;
                    205: }
                    206: 
                    207: /*
                    208:  * evasprintf() calls vasprintf() and exits with an error if vasprintf()
                    209:  * returns -1 (out of memory).
                    210:  */
                    211: int
                    212: evasprintf(char **ret, const char *format, va_list args)
                    213: {
                    214:     int len;
                    215: 
                    216:     if ((len = vasprintf(ret, format, args)) == -1)
                    217:        errorx(1, _("unable to allocate memory"));
                    218:     return len;
                    219: }
                    220: 
                    221: /*
                    222:  * Wrapper for free(3) so we can depend on C89 semantics.
                    223:  */
                    224: void
                    225: efree(void *ptr)
                    226: {
                    227:     if (ptr != NULL)
                    228:        free(ptr);
                    229: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>