]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
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)
committerLennart Poettering <lennart@poettering.net>
Mon, 26 Feb 2018 20:20:00 +0000 (21:20 +0100)
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/core/dbus-execute.c
src/core/load-fragment.c
src/core/namespace.c
src/libsystemd/sd-bus/bus-error.c
src/nspawn/nspawn-mount.c
src/shared/bus-unit-util.c

index d4af95a44a9b2c0e3448663aa721cec7e543a7ed..96340bb134d7d25dee8c7e859ba9d6731dd4a5f5 100644 (file)
@@ -521,10 +521,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 68e2e874b43b85b9e3cbe87ca94854d4d1125232..020fa269d7adeb72441ee1a33b3ac0301c29e0d9 100644 (file)
@@ -407,7 +407,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;
 
@@ -432,7 +432,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;
 
@@ -546,7 +546,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 f1361ca20b6360d243c9c90d3b51263c282c08a0..7ab40ca6baa663d897fe036c9d16855d3e666adf 100644 (file)
@@ -1518,7 +1518,7 @@ int bus_exec_context_set_transient_property(
                                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Journal field invalid");
 
                         if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
-                                t = realloc_multiply(c->log_extra_fields, sizeof(struct iovec), c->n_log_extra_fields+1);
+                                t = reallocarray(c->log_extra_fields, c->n_log_extra_fields+1, sizeof(struct iovec));
                                 if (!t)
                                         return -ENOMEM;
                                 c->log_extra_fields = t;
index 1b9888c10a479e0b0aebf2dc8a879e85e456e4fd..41da86910ed4871a97189281b473488d3150cd9a 100644 (file)
@@ -2580,7 +2580,7 @@ int config_parse_log_extra_fields(
                         continue;
                 }
 
-                t = realloc_multiply(c->log_extra_fields, sizeof(struct iovec), c->n_log_extra_fields+1);
+                t = reallocarray(c->log_extra_fields, c->n_log_extra_fields+1, sizeof(struct iovec));
                 if (!t)
                         return log_oom();
 
index 705a204bb3f611528c335f518a20da60e17ca964..5d092488bd4e435fa0f26b0ff2586c4ca569c81d 100644 (file)
@@ -1372,7 +1372,7 @@ int bind_mount_add(BindMount **b, unsigned *n, const BindMount *item) {
         if (!d)
                 return -ENOMEM;
 
-        c = realloc_multiply(*b, sizeof(BindMount), *n + 1);
+        c = reallocarray(*b, *n + 1, sizeof(BindMount));
         if (!c)
                 return -ENOMEM;
 
@@ -1426,7 +1426,7 @@ int temporary_filesystem_add(
                         return -ENOMEM;
         }
 
-        c = realloc_multiply(*t, sizeof(TemporaryFileSystem), *n + 1);
+        c = reallocarray(*t, *n + 1, sizeof(TemporaryFileSystem));
         if (!c)
                 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;
 
index 5193f1f69d87782ecd5d0ad6cd0c2fb0b6abb606..0ee69114b25518b764afb03d5d8de27dda092e06 100644 (file)
@@ -48,7 +48,7 @@ CustomMount* custom_mount_add(CustomMount **l, unsigned *n, CustomMountType t) {
         assert(t >= 0);
         assert(t < _CUSTOM_MOUNT_TYPE_MAX);
 
-        c = realloc_multiply(*l, (*n + 1), sizeof(CustomMount));
+        c = reallocarray(*l, *n + 1, sizeof(CustomMount));
         if (!c)
                 return NULL;
 
index 31260b732f1500f4737d1f5027d5e4c978d26ce1..54b2137c9c00998a41dd8de602cbbe1c2407a646 100644 (file)
@@ -1319,13 +1319,13 @@ static int bus_append_service_property(sd_bus_message *m, const char *field, con
                                 if (val < 0)
                                         return log_error_errno(r, "Invalid status or signal %s in %s: %m", word, field);
 
-                                signal = realloc_multiply(signal, sizeof(int), sz_signal + 1);
+                                signal = reallocarray(signal, sz_signal + 1, sizeof(int));
                                 if (!signal)
                                         return log_oom();
 
                                 signal[sz_signal++] = val;
                         } else {
-                                status = realloc_multiply(status, sizeof(int), sz_status + 1);
+                                status = reallocarray(status, sz_status + 1, sizeof(int));
                                 if (!status)
                                         return log_oom();