From aa484f356110d9118c44389fe8f03ee7b25a7746 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Mon, 26 Feb 2018 21:20:00 +0100 Subject: [PATCH] tree-wide: use reallocarray instead of our home-grown realloc_multiply (#8279) 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 | 3 ++- src/basic/alloc-util.h | 4 +++- src/basic/strv.c | 6 +++--- src/core/dbus-execute.c | 2 +- src/core/load-fragment.c | 2 +- src/core/namespace.c | 4 ++-- src/libsystemd/sd-bus/bus-error.c | 2 +- src/nspawn/nspawn-mount.c | 2 +- src/shared/bus-unit-util.c | 4 ++-- 9 files changed, 16 insertions(+), 13 deletions(-) diff --git a/meson.build b/meson.build index d4af95a44a..96340bb134 100644 --- a/meson.build +++ b/meson.build @@ -521,10 +521,11 @@ foreach ident : [ #include '''], ['bpf', '''#include #include '''], - ['explicit_bzero' , '''#include '''], ['statx', '''#include #include #include '''], + ['explicit_bzero' , '''#include '''], + ['reallocarray', '''#include '''], ] have = cc.has_function(ident[0], prefix : ident[1], args : '-D_GNU_SOURCE') diff --git a/src/basic/alloc-util.h b/src/basic/alloc-util.h index 02dee37d36..ec7808c1f7 100644 --- a/src/basic/alloc-util.h +++ b/src/basic/alloc-util.h @@ -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)) diff --git a/src/basic/strv.c b/src/basic/strv.c index 68e2e874b4..020fa269d7 100644 --- a/src/basic/strv.c +++ b/src/basic/strv.c @@ -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; diff --git a/src/core/dbus-execute.c b/src/core/dbus-execute.c index f1361ca20b..7ab40ca6ba 100644 --- a/src/core/dbus-execute.c +++ b/src/core/dbus-execute.c @@ -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; diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c index 1b9888c10a..41da86910e 100644 --- a/src/core/load-fragment.c +++ b/src/core/load-fragment.c @@ -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(); diff --git a/src/core/namespace.c b/src/core/namespace.c index 705a204bb3..5d092488bd 100644 --- a/src/core/namespace.c +++ b/src/core/namespace.c @@ -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; diff --git a/src/libsystemd/sd-bus/bus-error.c b/src/libsystemd/sd-bus/bus-error.c index c9517499d7..3939d0a4ef 100644 --- a/src/libsystemd/sd-bus/bus-error.c +++ b/src/libsystemd/sd-bus/bus-error.c @@ -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; diff --git a/src/nspawn/nspawn-mount.c b/src/nspawn/nspawn-mount.c index 5193f1f69d..0ee69114b2 100644 --- a/src/nspawn/nspawn-mount.c +++ b/src/nspawn/nspawn-mount.c @@ -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; diff --git a/src/shared/bus-unit-util.c b/src/shared/bus-unit-util.c index 31260b732f..54b2137c9c 100644 --- a/src/shared/bus-unit-util.c +++ b/src/shared/bus-unit-util.c @@ -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();