chiark / gitweb /
tree-wide: use reallocarray instead of our home-grown realloc_multiply (#8279)
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 26 Feb 2018 20:20:00 +0000 (21:20 +0100)
committerSven Eden <yamakuzure@gmx.net>
Wed, 30 May 2018 05:59:05 +0000 (07:59 +0200)
There isn't much difference, but in general we prefer to use the standard
functions. glibc provides reallocarray since version 2.26.

I moved explicit_bzero is configure test to the bottom, so that the two stdlib
functions are at the bottom.

meson.build
src/basic/alloc-util.h
src/basic/strv.c
src/libelogind/sd-bus/bus-error.c

index f2128b9d0a4b2fe559f8529b0788592ace3ae0fa..e2d9b2cf69f021e1d151c85b9be49654d2f8263b 100644 (file)
@@ -585,10 +585,11 @@ foreach ident : [
                                  #include <unistd.h>'''],
         ['bpf',               '''#include <sys/syscall.h>
                                  #include <unistd.h>'''],
-        ['explicit_bzero' ,   '''#include <string.h>'''],
         ['statx',             '''#include <sys/types.h>
                                  #include <sys/stat.h>
 //                                 #include <unistd.h>'''],
+        ['explicit_bzero' ,   '''#include <string.h>'''],
+        ['reallocarray',      '''#include <malloc.h>'''],
 ]
 
         have = cc.has_function(ident[0], prefix : ident[1], args : '-D_GNU_SOURCE')
index 02dee37d36d5dd5807e4ee76c307db9fe7943ce7..ec7808c1f7c2855b2cf84f416795434c0dae488d 100644 (file)
@@ -74,12 +74,14 @@ _malloc_  _alloc_(1, 2) static inline void *malloc_multiply(size_t size, size_t
         return malloc(size * need);
 }
 
-_alloc_(2, 3) static inline void *realloc_multiply(void *p, size_t size, size_t need) {
+#if !HAVE_REALLOCARRAY
+_alloc_(2, 3) static inline void *reallocarray(void *p, size_t need, size_t size) {
         if (size_multiply_overflow(size, need))
                 return NULL;
 
         return realloc(p, size * need);
 }
+#endif
 
 _alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t size, size_t need) {
         if (size_multiply_overflow(size, need))
index 85c55041ed8fd4e585e3487d9e51751a8ec6c66c..7898e8340bd6f01fde5df391020e2f7c3db4b381 100644 (file)
@@ -413,7 +413,7 @@ int strv_push(char ***l, char *value) {
         if (m < n)
                 return -ENOMEM;
 
-        c = realloc_multiply(*l, sizeof(char*), m);
+        c = reallocarray(*l, m, sizeof(char*));
         if (!c)
                 return -ENOMEM;
 
@@ -438,7 +438,7 @@ int strv_push_pair(char ***l, char *a, char *b) {
         if (m < n)
                 return -ENOMEM;
 
-        c = realloc_multiply(*l, sizeof(char*), m);
+        c = reallocarray(*l, m, sizeof(char*));
         if (!c)
                 return -ENOMEM;
 
@@ -554,7 +554,7 @@ int strv_extend_front(char ***l, const char *value) {
         if (!v)
                 return -ENOMEM;
 
-        c = realloc_multiply(*l, sizeof(char*), m);
+        c = reallocarray(*l, m, sizeof(char*));
         if (!c) {
                 free(v);
                 return -ENOMEM;
index c9517499d7bb1f6ea7341a5c807a18ff78f502ca..3939d0a4efb9b905c7ded0e129b59ac5cd71361b 100644 (file)
@@ -595,7 +595,7 @@ _public_ int sd_bus_error_add_map(const sd_bus_error_map *map) {
                         if (additional_error_maps[n] == map)
                                 return 0;
 
-        maps = realloc_multiply(additional_error_maps, sizeof(struct sd_bus_error_map*), n + 2);
+        maps = reallocarray(additional_error_maps, n + 2, sizeof(struct sd_bus_error_map*));
         if (!maps)
                 return -ENOMEM;