From 3595807f4e36c3b809289cfbcc3bf3ff164b2303 Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Thu, 14 May 2015 10:13:18 -0600 Subject: [PATCH] Add reallocarray() for those without it. --- config.h.in | 3 +++ configure | 3 +++ configure.ac | 2 +- include/sudo_compat.h | 7 ++++++- lib/util/alloc.c | 15 +++++++++++++++ 5 files changed, 28 insertions(+), 2 deletions(-) diff --git a/config.h.in b/config.h.in index 8f0b83b16..290dccc2f 100644 --- a/config.h.in +++ b/config.h.in @@ -556,6 +556,9 @@ /* Define to 1 if you have the `random' function. */ #undef HAVE_RANDOM +/* Define to 1 if you have the `reallocarray' function. */ +#undef HAVE_REALLOCARRAY + /* Define to 1 if you have the `revoke' function. */ #undef HAVE_REVOKE diff --git a/configure b/configure index 4f1607499..6fb5e4991 100755 --- a/configure +++ b/configure @@ -2653,6 +2653,7 @@ as_fn_append ac_header_list " sys/stropts.h" as_fn_append ac_header_list " sys/sysmacros.h" as_fn_append ac_func_list " killpg" as_fn_append ac_func_list " nl_langinfo" +as_fn_append ac_func_list " reallocarray" as_fn_append ac_func_list " strftime" as_fn_append ac_func_list " tzset" as_fn_append ac_func_list " seteuid" @@ -17921,6 +17922,8 @@ done + + for ac_func in getgrouplist do : ac_fn_c_check_func "$LINENO" "getgrouplist" "ac_cv_func_getgrouplist" diff --git a/configure.ac b/configure.ac index 915347ada..d2be85ef9 100644 --- a/configure.ac +++ b/configure.ac @@ -2395,7 +2395,7 @@ dnl dnl Function checks dnl AC_FUNC_GETGROUPS -AC_CHECK_FUNCS_ONCE([killpg nl_langinfo strftime tzset]) +AC_CHECK_FUNCS_ONCE([killpg nl_langinfo reallocarray strftime tzset]) AC_CHECK_FUNCS([getgrouplist], [], [ case "$host_os" in aix*) diff --git a/include/sudo_compat.h b/include/sudo_compat.h index 9d100a44f..fa8d6d7ff 100644 --- a/include/sudo_compat.h +++ b/include/sudo_compat.h @@ -368,7 +368,7 @@ __dso_public long long sudo_strtonum(const char *, long long, long long, const c * All libc replacements are prefixed with "sudo_" to avoid namespace issues. */ -struct timeval; +struct passwd; struct timespec; #ifndef HAVE_CLOSEFROM @@ -479,5 +479,10 @@ __dso_public const char *sudo_getprogname(void); # undef getprogname # define getprogname() sudo_getprogname() #endif /* HAVE_GETPROGNAME */ +#ifndef HAVE_REALLOCARRAY +__dso_public void *sudo_reallocarray(void *ptr, size_t nmemb, size_t size); +# undef reallocarray +# define reallocarray(_a, _b, _c) sudo_reallocarray((_a), (_b), (_c)) +#endif /* HAVE_REALLOCARRAY */ #endif /* _SUDO_COMPAT_H */ diff --git a/lib/util/alloc.c b/lib/util/alloc.c index f94c04bd7..5f2208d52 100644 --- a/lib/util/alloc.c +++ b/lib/util/alloc.c @@ -48,6 +48,7 @@ #elif defined(HAVE_INTTYPES_H) # include #endif +#include #include #define DEFAULT_TEXT_DOMAIN "sudo" @@ -257,3 +258,17 @@ 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 */ -- 2.50.1