]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
include/c.h: implement reallocarray
authorThomas Weißschuh <thomas@t-8ch.de>
Fri, 22 Sep 2023 10:23:03 +0000 (12:23 +0200)
committerThomas Weißschuh <thomas@t-8ch.de>
Fri, 1 Dec 2023 07:10:37 +0000 (08:10 +0100)
reallocarray() is not specified by any standard and may be missing from
older versions of glibc.

Fixes #2491
Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
configure.ac
include/c.h
meson.build

index ac39d595d13fde7eac5a0646d38f226927b170ba..913e50449f1fe521e447fbcdb140a479c08af4b0 100644 (file)
@@ -606,6 +606,7 @@ AC_CHECK_FUNCS([ \
        posix_fadvise \
        prctl \
        qsort_r \
+       reallocarray \
        rpmatch \
        scandirat \
        sched_setattr \
index 224a8e54feae8ad8aadf2460e6281ac656d62b4a..36eb2a9e7f80045588164f4cc4ef3f8a90f72321 100644 (file)
@@ -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 */
index f7b14cf3f832c97a8f224f70a65903a78bbc2a14..fb2818a5d3ac92e6454a7b382de8ca5ac40c21fd 100644 (file)
@@ -553,6 +553,7 @@ funcs = '''
         posix_fadvise
         prctl
         qsort_r
+        reallocarray
         rpmatch
         scandirat
         setprogname