[BACK]Return to alloc.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / sudo

Annotation of src/usr.bin/sudo/alloc.c, Revision 1.9

1.1       millert     1: /*
1.9     ! millert     2:  * Copyright (c) 1999-2005 Todd C. Miller <Todd.Miller@courtesan.com>
1.1       millert     3:  *
1.8       millert     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.
                      7:  *
                      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.
1.7       millert    15:  *
                     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.
1.1       millert    19:  */
                     20:
1.9     ! millert    21: #include <config.h>
1.1       millert    22:
1.3       millert    23: #include <sys/types.h>
                     24: #include <sys/param.h>
1.1       millert    25: #include <stdio.h>
                     26: #ifdef STDC_HEADERS
1.3       millert    27: # include <stdlib.h>
                     28: # include <stddef.h>
                     29: #else
                     30: # ifdef HAVE_STDLIB_H
                     31: #  include <stdlib.h>
                     32: # endif
1.1       millert    33: #endif /* STDC_HEADERS */
                     34: #ifdef HAVE_STRING_H
1.3       millert    35: # include <string.h>
                     36: #else
                     37: # ifdef HAVE_STRINGS_H
                     38: #  include <strings.h>
                     39: # endif
1.1       millert    40: #endif /* HAVE_STRING_H */
                     41: #if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
1.3       millert    42: # include <malloc.h>
1.1       millert    43: #endif /* HAVE_MALLOC_H && !STDC_HEADERS */
1.6       millert    44: #ifdef HAVE_ERR_H
                     45: # include <err.h>
                     46: #else
                     47: # include "emul/err.h"
                     48: #endif /* HAVE_ERR_H */
1.1       millert    49:
                     50: #include "sudo.h"
                     51:
                     52: #ifndef lint
1.9     ! millert    53: __unused static const char rcsid[] = "$Sudo: alloc.c,v 1.23.2.3 2007/06/12 01:43:01 millert Exp $";
1.1       millert    54: #endif /* lint */
                     55:
1.5       millert    56: /*
                     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.
                     61:  */
                     62: #ifndef SIZE_MAX
                     63: # ifdef SIZE_T_MAX
                     64: #  define SIZE_MAX     SIZE_T_MAX
                     65: # else
1.8       millert    66: #  define SIZE_MAX     INT_MAX
1.5       millert    67: # endif /* SIZE_T_MAX */
                     68: #endif /* SIZE_MAX */
                     69:
1.1       millert    70: /*
                     71:  * emalloc() calls the system malloc(3) and exits with an error if
                     72:  * malloc(3) fails.
                     73:  */
                     74: VOID *
                     75: emalloc(size)
                     76:     size_t size;
                     77: {
                     78:     VOID *ptr;
                     79:
1.6       millert    80:     if (size == 0)
                     81:        errx(1, "internal error, tried to emalloc(0)");
                     82:
                     83:     if ((ptr = (VOID *) malloc(size)) == NULL)
                     84:        errx(1, "unable to allocate memory");
1.5       millert    85:     return(ptr);
                     86: }
                     87:
                     88: /*
                     89:  * emalloc2() allocates nmemb * size bytes and exits with an error
                     90:  * if overflow would occur or if the system malloc(3) fails.
                     91:  */
                     92: VOID *
                     93: emalloc2(nmemb, size)
                     94:     size_t nmemb;
                     95:     size_t size;
                     96: {
                     97:     VOID *ptr;
                     98:
1.6       millert    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");
                    103:
1.5       millert   104:     size *= nmemb;
1.6       millert   105:     if ((ptr = (VOID *) malloc(size)) == NULL)
                    106:        errx(1, "unable to allocate memory");
1.1       millert   107:     return(ptr);
                    108: }
                    109:
                    110: /*
                    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.
                    114:  */
                    115: VOID *
                    116: erealloc(ptr, size)
                    117:     VOID *ptr;
                    118:     size_t size;
                    119: {
                    120:
1.6       millert   121:     if (size == 0)
                    122:        errx(1, "internal error, tried to erealloc(0)");
                    123:
1.5       millert   124:     ptr = ptr ? (VOID *) realloc(ptr, size) : (VOID *) malloc(size);
1.6       millert   125:     if (ptr == NULL)
                    126:        errx(1, "unable to allocate memory");
1.5       millert   127:     return(ptr);
                    128: }
                    129:
                    130: /*
                    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.
                    135:  */
                    136: VOID *
                    137: erealloc3(ptr, nmemb, size)
                    138:     VOID *ptr;
                    139:     size_t nmemb;
                    140:     size_t size;
                    141: {
                    142:
1.6       millert   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");
                    147:
1.5       millert   148:     size *= nmemb;
1.4       millert   149:     ptr = ptr ? (VOID *) realloc(ptr, size) : (VOID *) malloc(size);
1.6       millert   150:     if (ptr == NULL)
                    151:        errx(1, "unable to allocate memory");
1.1       millert   152:     return(ptr);
                    153: }
                    154:
                    155: /*
                    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.
                    158:  */
                    159: char *
                    160: estrdup(src)
                    161:     const char *src;
                    162: {
                    163:     char *dst = NULL;
1.5       millert   164:     size_t size;
1.1       millert   165:
                    166:     if (src != NULL) {
1.5       millert   167:        size = strlen(src) + 1;
                    168:        dst = (char *) emalloc(size);
                    169:        (void) memcpy(dst, src, size);
1.1       millert   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:  */
1.2       millert   178: int
1.1       millert   179: #ifdef __STDC__
                    180: easprintf(char **ret, const char *fmt, ...)
                    181: #else
1.9     ! millert   182: easprintf(ret, fmt, va_alist)
        !           183:     char **ret;
        !           184:     const char *fmt;
1.1       millert   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:
1.6       millert   198:     if (len == -1)
                    199:        errx(1, "unable to allocate memory");
1.2       millert   200:     return(len);
1.1       millert   201: }
                    202:
                    203: /*
                    204:  * evasprintf() calls vasprintf() and exits with an error if vasprintf()
                    205:  * returns -1 (out of memory).
                    206:  */
1.2       millert   207: int
1.1       millert   208: evasprintf(ret, format, args)
                    209:     char **ret;
                    210:     const char *format;
                    211:     va_list args;
                    212: {
1.2       millert   213:     int len;
1.1       millert   214:
1.6       millert   215:     if ((len = vasprintf(ret, format, args)) == -1)
                    216:        errx(1, "unable to allocate memory");
1.2       millert   217:     return(len);
1.9     ! millert   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);
1.1       millert   229: }