From: Lennart Poettering Date: Tue, 27 Feb 2018 18:09:22 +0000 (+0100) Subject: coccinelle: add reallocarray() coccinelle script X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=ecf21e9295474064b101923f1aec8dce556cdcbd;p=elogind.git coccinelle: add reallocarray() coccinelle script Let's systematically make use of reallocarray() whereever we invoke realloc() with a product of two values. --- diff --git a/src/basic/env-util.c b/src/basic/env-util.c index cb2c54cd8..34f821d57 100644 --- a/src/basic/env-util.c +++ b/src/basic/env-util.c @@ -722,7 +722,7 @@ char **replace_env_argv(char **argv, char **env) { q = strv_length(m); l = l + q - 1; - w = realloc(ret, sizeof(char*) * (l+1)); + w = reallocarray(ret, l + 1, sizeof(char *)); if (!w) { ret[k] = NULL; strv_free(ret); diff --git a/src/basic/prioq.c b/src/basic/prioq.c index 407b17e9b..2a1f1af87 100644 --- a/src/basic/prioq.c +++ b/src/basic/prioq.c @@ -173,7 +173,7 @@ int prioq_put(Prioq *q, void *data, unsigned *idx) { struct prioq_item *j; n = MAX((q->n_items+1) * 2, 16u); - j = realloc(q->items, sizeof(struct prioq_item) * n); + j = reallocarray(q->items, n, sizeof(struct prioq_item)); if (!j) return -ENOMEM; diff --git a/src/basic/strv.c b/src/basic/strv.c index 7898e8340..b6f762702 100644 --- a/src/basic/strv.c +++ b/src/basic/strv.c @@ -215,7 +215,7 @@ int strv_extend_strv(char ***a, char **b, bool filter_duplicates) { p = strv_length(*a); q = strv_length(b); - t = realloc(*a, sizeof(char*) * (p + q + 1)); + t = reallocarray(*a, p + q + 1, sizeof(char *)); if (!t) return -ENOMEM; @@ -875,7 +875,7 @@ int strv_extend_n(char ***l, const char *value, size_t n) { k = strv_length(*l); - nl = realloc(*l, sizeof(char*) * (k + n + 1)); + nl = reallocarray(*l, k + n + 1, sizeof(char *)); if (!nl) return -ENOMEM; diff --git a/src/libelogind/sd-bus/bus-message.c b/src/libelogind/sd-bus/bus-message.c index 414665558..5c91757aa 100644 --- a/src/libelogind/sd-bus/bus-message.c +++ b/src/libelogind/sd-bus/bus-message.c @@ -1400,7 +1400,7 @@ static int message_push_fd(sd_bus_message *m, int fd) { if (copy < 0) return -errno; - f = realloc(m->fds, sizeof(int) * (m->n_fds + 1)); + f = reallocarray(m->fds, sizeof(int), m->n_fds + 1); if (!f) { m->poisoned = true; safe_close(copy); diff --git a/src/libelogind/sd-bus/bus-socket.c b/src/libelogind/sd-bus/bus-socket.c index 4890895bc..a86d092a6 100644 --- a/src/libelogind/sd-bus/bus-socket.c +++ b/src/libelogind/sd-bus/bus-socket.c @@ -1223,7 +1223,7 @@ int bus_socket_read_message(sd_bus *bus) { return -EIO; } - f = realloc(bus->fds, sizeof(int) * (bus->n_fds + n)); + f = reallocarray(bus->fds, bus->n_fds + n, sizeof(int)); if (!f) { close_many((int*) CMSG_DATA(cmsg), n); return -ENOMEM;