From 0322c9cdd78ce2dde2235c9351891aa54ddb4e12 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Thomas=20Wei=C3=9Fschuh?= Date: Fri, 22 Sep 2023 12:23:03 +0200 Subject: [PATCH] include/c.h: implement reallocarray MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit reallocarray() is not specified by any standard and may be missing from older versions of glibc. Fixes #2491 Signed-off-by: Thomas Weißschuh --- configure.ac | 1 + include/c.h | 13 +++++++++++++ meson.build | 1 + 3 files changed, 15 insertions(+) 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 -- 2.47.3