From 65c2929bed8254e5ea24bb2d3d5a403a36daf69e Mon Sep 17 00:00:00 2001 From: =?utf8?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/libelogind/sd-bus/bus-error.c | 2 +- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/meson.build b/meson.build index f2128b9d0..e2d9b2cf6 100644 --- a/meson.build +++ b/meson.build @@ -585,10 +585,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 02dee37d3..ec7808c1f 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 85c55041e..7898e8340 100644 --- a/src/basic/strv.c +++ b/src/basic/strv.c @@ -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; diff --git a/src/libelogind/sd-bus/bus-error.c b/src/libelogind/sd-bus/bus-error.c index c9517499d..3939d0a4e 100644 --- a/src/libelogind/sd-bus/bus-error.c +++ b/src/libelogind/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; -- 2.30.2