From: Todd C. Miller Date: Wed, 27 May 2015 16:04:32 +0000 (-0600) Subject: Add standalone reallocarray.c from OpenBSD instead of rolling our own. X-Git-Tag: SUDO_1_8_14^2~107 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=40d72f26e44ba72e6b795e414a3a8bee3f11f9f1;p=sudo Add standalone reallocarray.c from OpenBSD instead of rolling our own. --- diff --git a/MANIFEST b/MANIFEST index 2ec449fb0..d16ed2ba0 100644 --- a/MANIFEST +++ b/MANIFEST @@ -112,6 +112,7 @@ lib/util/mktemp.c lib/util/parseln.c lib/util/progname.c lib/util/pw_dup.c +lib/util/reallocarray.c lib/util/regress/atofoo/atofoo_test.c lib/util/regress/fnmatch/fnm_test.c lib/util/regress/fnmatch/fnm_test.in diff --git a/configure.ac b/configure.ac index 99fcb9258..0d114937b 100644 --- a/configure.ac +++ b/configure.ac @@ -2446,6 +2446,7 @@ AC_CHECK_FUNCS([getline], [], [ AC_CHECK_FUNCS([fgetln]) ]) AC_CHECK_FUNCS([reallocarray], [], [ + AC_LIBOBJ(reallocarray) SUDO_APPEND_COMPAT_EXP(sudo_reallocarray) ]) dnl diff --git a/lib/util/alloc.c b/lib/util/alloc.c index 77164c9cd..325d2072e 100644 --- a/lib/util/alloc.c +++ b/lib/util/alloc.c @@ -48,7 +48,6 @@ #elif defined(HAVE_INTTYPES_H) # include #endif -#include #include #define DEFAULT_TEXT_DOMAIN "sudo" @@ -258,17 +257,3 @@ sudo_evasprintf_v1(char **ret, const char *fmt, va_list args) sudo_fatal_nodebug(NULL); return len; } - -#ifndef HAVE_REALLOCARRAY -void * -sudo_reallocarray(void *ptr, size_t nmemb, size_t size) -{ - if (nmemb > SIZE_MAX / size) { - errno = EOVERFLOW; - return NULL; - } - - size *= nmemb; - return ptr ? realloc(ptr, size) : malloc(size); -} -#endif /* HAVE_REALLOCARRAY */ diff --git a/lib/util/reallocarray.c b/lib/util/reallocarray.c new file mode 100644 index 000000000..7d2588e37 --- /dev/null +++ b/lib/util/reallocarray.c @@ -0,0 +1,61 @@ +/* $OpenBSD: reallocarray.c,v 1.2 2014/12/08 03:45:00 bcook Exp $ */ +/* + * Copyright (c) 2008 Otto Moerbeek + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include + +#ifndef HAVE_REALLOCARRAY + +#include +#ifdef STDC_HEADERS +# include +# include +#else +# ifdef HAVE_STDLIB_H +# include +# endif +#endif /* STDC_HEADERS */ +#if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS) +# include +#endif /* HAVE_MALLOC_H && !STDC_HEADERS */ +#if defined(HAVE_STDINT_H) +# include +#elif defined(HAVE_INTTYPES_H) +# include +#endif +#include +#include + +#include "sudo_compat.h" + +/* + * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX + * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW + */ +#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4)) + +void * +sudo_reallocarray(void *optr, size_t nmemb, size_t size) +{ + if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && + nmemb > 0 && SIZE_MAX / nmemb < size) { + errno = ENOMEM; + return NULL; + } + return realloc(optr, size * nmemb); +} + +#endif /* HAVE_REALLOCARRAY */