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.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2018-02-26 21:20:00 +01:00 committed by Lennart Poettering
parent f2e3f36950
commit aa484f3561
9 changed files with 16 additions and 13 deletions

View 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')

View 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))

View 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;

View 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;

View 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();

View 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;

View 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;

View 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;

View 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();