From: Thomas Weißschuh Date: Fri, 22 Sep 2023 10:23:03 +0000 (+0200) Subject: include/c.h: implement reallocarray X-Git-Tag: v2.40-rc1~132^2~5 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=0322c9cdd78ce2dde2235c9351891aa54ddb4e12;p=thirdparty%2Futil-linux.git include/c.h: implement reallocarray reallocarray() is not specified by any standard and may be missing from older versions of glibc. Fixes #2491 Signed-off-by: Thomas Weißschuh --- diff --git a/configure.ac b/configure.ac index ac39d595d1..913e50449f 100644 --- a/configure.ac +++ b/configure.ac @@ -606,6 +606,7 @@ AC_CHECK_FUNCS([ \ posix_fadvise \ prctl \ qsort_r \ + reallocarray \ rpmatch \ scandirat \ sched_setattr \ diff --git a/include/c.h b/include/c.h index 224a8e54fe..36eb2a9e7f 100644 --- a/include/c.h +++ b/include/c.h @@ -576,4 +576,17 @@ static inline void print_features(const char **features, const char *prefix) #define SINT_MAX(t) (((t)1 << (sizeof(t) * 8 - 2)) - (t)1 + ((t)1 << (sizeof(t) * 8 - 2))) +#ifndef HAVE_REALLOCARRAY +static inline void *reallocarray(void *ptr, size_t nmemb, size_t size) +{ + size_t s = nmemb * size; + + if (nmemb != 0 && s / nmemb != size) { + errno = ENOMEM; + return NULL; + } + return realloc(ptr, s); +} +#endif + #endif /* UTIL_LINUX_C_H */ diff --git a/meson.build b/meson.build index f7b14cf3f8..fb2818a5d3 100644 --- a/meson.build +++ b/meson.build @@ -553,6 +553,7 @@ funcs = ''' posix_fadvise prctl qsort_r + reallocarray rpmatch scandirat setprogname