--- embedaddon/sudo/common/alloc.c 2012/02/21 16:23:02 1.1.1.1 +++ embedaddon/sudo/common/alloc.c 2014/06/15 16:12:54 1.1.1.6 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999-2005, 2007, 2010-2011 + * Copyright (c) 1999-2005, 2007, 2010-2013 * Todd C. Miller * * Permission to use, copy, modify, and distribute this software for any @@ -22,7 +22,6 @@ #include #include -#include #include #ifdef STDC_HEADERS # include @@ -47,14 +46,15 @@ #ifdef HAVE_INTTYPES_H # include #endif +#include +#define DEFAULT_TEXT_DOMAIN "sudo" +#include "gettext.h" /* must be included before missing.h */ + #include "missing.h" #include "alloc.h" -#include "error.h" +#include "fatal.h" -#define DEFAULT_TEXT_DOMAIN "sudo" -#include "gettext.h" - /* * If there is no SIZE_MAX or SIZE_T_MAX we have to assume that size_t * could be signed (as it is on SunOS 4.x). This just means that @@ -79,10 +79,10 @@ emalloc(size_t size) void *ptr; if (size == 0) - errorx(1, _("internal error, tried to emalloc(0)")); + fatalx_nodebug(_("internal error, tried to emalloc(0)")); if ((ptr = malloc(size)) == NULL) - errorx(1, _("unable to allocate memory")); + fatal_nodebug(NULL); return ptr; } @@ -96,17 +96,40 @@ emalloc2(size_t nmemb, size_t size) void *ptr; if (nmemb == 0 || size == 0) - errorx(1, _("internal error, tried to emalloc2(0)")); + fatalx_nodebug(_("internal error, tried to emalloc2(0)")); if (nmemb > SIZE_MAX / size) - errorx(1, _("internal error, emalloc2() overflow")); + fatalx_nodebug(_("internal error, %s overflow"), "emalloc2()"); size *= nmemb; if ((ptr = malloc(size)) == NULL) - errorx(1, _("unable to allocate memory")); + fatal_nodebug(NULL); return ptr; } /* + * ecalloc() allocates nmemb * size bytes and exits with an error + * if overflow would occur or if the system malloc(3) fails. + * On success, the allocated space is zero-filled. + */ +void * +ecalloc(size_t nmemb, size_t size) +{ + void *ptr; + + if (nmemb == 0 || size == 0) + fatalx_nodebug(_("internal error, tried to ecalloc(0)")); + if (nmemb != 1) { + if (nmemb > SIZE_MAX / size) + fatalx_nodebug(_("internal error, %s overflow"), "ecalloc()"); + size *= nmemb; + } + if ((ptr = malloc(size)) == NULL) + fatal_nodebug(NULL); + memset(ptr, 0, size); + return ptr; +} + +/* * erealloc() calls the system realloc(3) and exits with an error if * realloc(3) fails. You can call erealloc() with a NULL pointer even * if the system realloc(3) does not support this. @@ -116,11 +139,11 @@ erealloc(void *ptr, size_t size) { if (size == 0) - errorx(1, _("internal error, tried to erealloc(0)")); + fatalx_nodebug(_("internal error, tried to erealloc(0)")); ptr = ptr ? realloc(ptr, size) : malloc(size); if (ptr == NULL) - errorx(1, _("unable to allocate memory")); + fatal_nodebug(NULL); return ptr; } @@ -135,18 +158,45 @@ erealloc3(void *ptr, size_t nmemb, size_t size) { if (nmemb == 0 || size == 0) - errorx(1, _("internal error, tried to erealloc3(0)")); + fatalx_nodebug(_("internal error, tried to erealloc3(0)")); if (nmemb > SIZE_MAX / size) - errorx(1, _("internal error, erealloc3() overflow")); + fatalx_nodebug(_("internal error, %s overflow"), "erealloc3()"); size *= nmemb; ptr = ptr ? realloc(ptr, size) : malloc(size); if (ptr == NULL) - errorx(1, _("unable to allocate memory")); + fatal_nodebug(NULL); return ptr; } /* + * erecalloc() realloc(3)s nmemb * msize bytes and exits with an error + * if overflow would occur or if the system malloc(3)/realloc(3) fails. + * On success, the new space is zero-filled. You can call erealloc() + * with a NULL pointer even if the system realloc(3) does not support this. + */ +void * +erecalloc(void *ptr, size_t onmemb, size_t nmemb, size_t msize) +{ + size_t size; + + if (nmemb == 0 || msize == 0) + fatalx_nodebug(_("internal error, tried to erecalloc(0)")); + if (nmemb > SIZE_MAX / msize) + fatalx_nodebug(_("internal error, %s overflow"), "erecalloc()"); + + size = nmemb * msize; + ptr = ptr ? realloc(ptr, size) : malloc(size); + if (ptr == NULL) + fatal_nodebug(NULL); + if (nmemb > onmemb) { + size = (nmemb - onmemb) * msize; + memset((char *)ptr + (onmemb * msize), 0, size); + } + return ptr; +} + +/* * estrdup() is like strdup(3) except that it exits with an error if * malloc(3) fails. NOTE: unlike strdup(3), estrdup(NULL) is legal. */ @@ -173,12 +223,13 @@ char * estrndup(const char *src, size_t maxlen) { char *dst = NULL; - size_t len; + size_t len = 0; if (src != NULL) { - len = strlen(src); - if (len > maxlen) - len = maxlen; + while (maxlen != 0 && src[len] != '\0') { + len++; + maxlen--; + } dst = (char *) emalloc(len + 1); (void) memcpy(dst, src, len); dst[len] = '\0'; @@ -195,12 +246,13 @@ easprintf(char **ret, const char *fmt, ...) { int len; va_list ap; + va_start(ap, fmt); len = vasprintf(ret, fmt, ap); va_end(ap); if (len == -1) - errorx(1, _("unable to allocate memory")); + fatal_nodebug(NULL); return len; } @@ -214,16 +266,6 @@ evasprintf(char **ret, const char *format, va_list arg int len; if ((len = vasprintf(ret, format, args)) == -1) - errorx(1, _("unable to allocate memory")); + fatal_nodebug(NULL); return len; -} - -/* - * Wrapper for free(3) so we can depend on C89 semantics. - */ -void -efree(void *ptr) -{ - if (ptr != NULL) - free(ptr); }