diff --git a/coccinelle/reallocarray.cocci b/coccinelle/reallocarray.cocci new file mode 100644 index 0000000000..21fe9dfbfd --- /dev/null +++ b/coccinelle/reallocarray.cocci @@ -0,0 +1,20 @@ +@@ +expression q, p, n, m; +@@ +- q = realloc(p, (n)*(m)) ++ q = reallocarray(p, n, m) +@@ +expression q, p, n, m; +@@ +- q = realloc(p, n*(m)) ++ q = reallocarray(p, n, m) +@@ +expression q, p, n, m; +@@ +- q = realloc(p, (n)*m) ++ q = reallocarray(p, n, m) +@@ +expression q, p, n, m; +@@ +- q = realloc(p, n*m) ++ q = reallocarray(p, n, m) diff --git a/src/basic/env-util.c b/src/basic/env-util.c index ab3fc71cd2..0b1d086394 100644 --- a/src/basic/env-util.c +++ b/src/basic/env-util.c @@ -721,7 +721,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 407b17e9bf..2a1f1af878 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/strbuf.c b/src/basic/strbuf.c index 8befffa66f..bc3e56cf71 100644 --- a/src/basic/strbuf.c +++ b/src/basic/strbuf.c @@ -190,7 +190,7 @@ ssize_t strbuf_add_string(struct strbuf *str, const char *s, size_t len) { node_child->value_len = len; /* extend array, add new entry, sort for bisection */ - child = realloc(node->children, (node->children_count + 1) * sizeof(struct strbuf_child_entry)); + child = reallocarray(node->children, node->children_count + 1, sizeof(struct strbuf_child_entry)); if (!child) { free(node_child); return -ENOMEM; diff --git a/src/basic/strv.c b/src/basic/strv.c index 020fa269d7..e80ff4a62c 100644 --- a/src/basic/strv.c +++ b/src/basic/strv.c @@ -214,7 +214,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; @@ -861,7 +861,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/core/service.c b/src/core/service.c index 1997c82ac3..df36019f62 100644 --- a/src/core/service.c +++ b/src/core/service.c @@ -1254,7 +1254,7 @@ static int service_collect_fds(Service *s, } else { int *t; - t = realloc(rfds, (rn_socket_fds + cn_fds) * sizeof(int)); + t = reallocarray(rfds, rn_socket_fds + cn_fds, sizeof(int)); if (!t) return -ENOMEM; @@ -1276,13 +1276,13 @@ static int service_collect_fds(Service *s, char **nl; int *t; - t = realloc(rfds, (rn_socket_fds + s->n_fd_store) * sizeof(int)); + t = reallocarray(rfds, rn_socket_fds + s->n_fd_store, sizeof(int)); if (!t) return -ENOMEM; rfds = t; - nl = realloc(rfd_names, (rn_socket_fds + s->n_fd_store + 1) * sizeof(char*)); + nl = reallocarray(rfd_names, rn_socket_fds + s->n_fd_store + 1, sizeof(char *)); if (!nl) return -ENOMEM; diff --git a/src/hwdb/hwdb.c b/src/hwdb/hwdb.c index 4540260f9b..f27f60e977 100644 --- a/src/hwdb/hwdb.c +++ b/src/hwdb/hwdb.c @@ -103,7 +103,7 @@ static int node_add_child(struct trie *trie, struct trie_node *node, struct trie struct trie_child_entry *child; /* extend array, add new entry, sort for bisection */ - child = realloc(node->children, (node->children_count + 1) * sizeof(struct trie_child_entry)); + child = reallocarray(node->children, node->children_count + 1, sizeof(struct trie_child_entry)); if (!child) return -ENOMEM; @@ -197,7 +197,7 @@ static int trie_node_add_value(struct trie *trie, struct trie_node *node, } /* extend array, add new entry, sort for bisection */ - val = realloc(node->values, (node->values_count + 1) * sizeof(struct trie_value_entry)); + val = reallocarray(node->values, node->values_count + 1, sizeof(struct trie_value_entry)); if (!val) return -ENOMEM; trie->values_count++; diff --git a/src/libsystemd-network/network-internal.c b/src/libsystemd-network/network-internal.c index 94386e4860..584a1f36ac 100644 --- a/src/libsystemd-network/network-internal.c +++ b/src/libsystemd-network/network-internal.c @@ -424,7 +424,7 @@ int deserialize_in_addrs(struct in_addr **ret, const char *string) { if (r == 0) break; - new_addresses = realloc(addresses, (size + 1) * sizeof(struct in_addr)); + new_addresses = reallocarray(addresses, size + 1, sizeof(struct in_addr)); if (!new_addresses) return -ENOMEM; else @@ -478,7 +478,7 @@ int deserialize_in6_addrs(struct in6_addr **ret, const char *string) { if (r == 0) break; - new_addresses = realloc(addresses, (size + 1) * sizeof(struct in6_addr)); + new_addresses = reallocarray(addresses, size + 1, sizeof(struct in6_addr)); if (!new_addresses) return -ENOMEM; else diff --git a/src/libsystemd/sd-bus/bus-message.c b/src/libsystemd/sd-bus/bus-message.c index 95a87da08b..59b2422b94 100644 --- a/src/libsystemd/sd-bus/bus-message.c +++ b/src/libsystemd/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/libsystemd/sd-bus/bus-socket.c b/src/libsystemd/sd-bus/bus-socket.c index 90132bb87b..44f71fdf86 100644 --- a/src/libsystemd/sd-bus/bus-socket.c +++ b/src/libsystemd/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; diff --git a/src/libudev/libudev-list.c b/src/libudev/libudev-list.c index 29fbdbd450..681e2e445c 100644 --- a/src/libudev/libudev-list.c +++ b/src/libudev/libudev-list.c @@ -140,8 +140,7 @@ static int list_search(struct udev_list *list, const char *name) return -(first+1); } -struct udev_list_entry *udev_list_entry_add(struct udev_list *list, const char *name, const char *value) -{ +struct udev_list_entry *udev_list_entry_add(struct udev_list *list, const char *name, const char *value) { struct udev_list_entry *entry; int i = 0; @@ -152,12 +151,12 @@ struct udev_list_entry *udev_list_entry_add(struct udev_list *list, const char * entry = list->entries[i]; free(entry->value); - if (value == NULL) { + if (!value) { entry->value = NULL; return entry; } entry->value = strdup(value); - if (entry->value == NULL) + if (!entry->value) return NULL; return entry; } @@ -165,16 +164,16 @@ struct udev_list_entry *udev_list_entry_add(struct udev_list *list, const char * /* add new name */ entry = new0(struct udev_list_entry, 1); - if (entry == NULL) + if (!entry) return NULL; entry->name = strdup(name); - if (entry->name == NULL) + if (!entry->name) return mfree(entry); - if (value != NULL) { + if (value) { entry->value = strdup(value); - if (entry->value == NULL) { + if (!entry->value) { free(entry->name); return mfree(entry); } @@ -189,8 +188,8 @@ struct udev_list_entry *udev_list_entry_add(struct udev_list *list, const char * add = list->entries_max; if (add < 1) add = 64; - entries = realloc(list->entries, (list->entries_max + add) * sizeof(struct udev_list_entry *)); - if (entries == NULL) { + entries = reallocarray(list->entries, list->entries_max + add, sizeof(struct udev_list_entry *)); + if (!entries) { free(entry->name); free(entry->value); return mfree(entry); @@ -213,9 +212,8 @@ struct udev_list_entry *udev_list_entry_add(struct udev_list *list, const char * (list->entries_cur - i) * sizeof(struct udev_list_entry *)); list->entries[i] = entry; list->entries_cur++; - } else { + } else udev_list_entry_append(entry, list); - } return entry; } diff --git a/src/network/networkd-network.c b/src/network/networkd-network.c index 48da83c497..709ae2a0cc 100644 --- a/src/network/networkd-network.c +++ b/src/network/networkd-network.c @@ -1072,7 +1072,7 @@ int config_parse_dhcp_server_dns( continue; } - m = realloc(n->dhcp_server_dns, (n->n_dhcp_server_dns + 1) * sizeof(struct in_addr)); + m = reallocarray(n->dhcp_server_dns, n->n_dhcp_server_dns + 1, sizeof(struct in_addr)); if (!m) return log_oom(); @@ -1120,7 +1120,7 @@ int config_parse_radv_dns( if (in_addr_from_string(AF_INET6, w, &a) >= 0) { struct in6_addr *m; - m = realloc(n->router_dns, (n->n_router_dns + 1) * sizeof(struct in6_addr)); + m = reallocarray(n->router_dns, n->n_router_dns + 1, sizeof(struct in6_addr)); if (!m) return log_oom(); @@ -1223,7 +1223,7 @@ int config_parse_dhcp_server_ntp( continue; } - m = realloc(n->dhcp_server_ntp, (n->n_dhcp_server_ntp + 1) * sizeof(struct in_addr)); + m = reallocarray(n->dhcp_server_ntp, n->n_dhcp_server_ntp + 1, sizeof(struct in_addr)); if (!m) return log_oom(); @@ -1273,7 +1273,7 @@ int config_parse_dns( continue; } - m = realloc(n->dns, (n->n_dns + 1) * sizeof(struct in_addr_data)); + m = reallocarray(n->dns, n->n_dns + 1, sizeof(struct in_addr_data)); if (!m) return log_oom(); diff --git a/src/shared/install.c b/src/shared/install.c index fb2231b995..ed5f51cc46 100644 --- a/src/shared/install.c +++ b/src/shared/install.c @@ -311,7 +311,7 @@ int unit_file_changes_add( if (!changes) return 0; - c = realloc(*changes, (*n_changes + 1) * sizeof(UnitFileChange)); + c = reallocarray(*changes, *n_changes + 1, sizeof(UnitFileChange)); if (!c) return -ENOMEM; *changes = c; diff --git a/src/shared/uid-range.c b/src/shared/uid-range.c index c38b7cc984..37d7c981f2 100644 --- a/src/shared/uid-range.c +++ b/src/shared/uid-range.c @@ -22,6 +22,7 @@ #include #include +#include "alloc-util.h" #include "macro.h" #include "uid-range.h" #include "user-util.h" @@ -109,7 +110,7 @@ int uid_range_add(UidRange **p, unsigned *n, uid_t start, uid_t nr) { } else { UidRange *t; - t = realloc(*p, sizeof(UidRange) * (*n + 1)); + t = reallocarray(*p, *n + 1, sizeof(UidRange)); if (!t) return -ENOMEM; diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c index f4708bb066..3b0ddb5118 100644 --- a/src/udev/udev-rules.c +++ b/src/udev/udev-rules.c @@ -465,7 +465,7 @@ static int add_token(struct udev_rules *rules, struct token *token) { if (add < 8) add = 8; - tokens = realloc(rules->tokens, (rules->token_max + add ) * sizeof(struct token)); + tokens = reallocarray(rules->tokens, rules->token_max + add, sizeof(struct token)); if (tokens == NULL) return -1; rules->tokens = tokens; @@ -511,7 +511,7 @@ static uid_t add_uid(struct udev_rules *rules, const char *owner) { if (add < 1) add = 8; - uids = realloc(rules->uids, (rules->uids_max + add ) * sizeof(struct uid_gid)); + uids = reallocarray(rules->uids, rules->uids_max + add, sizeof(struct uid_gid)); if (uids == NULL) return uid; rules->uids = uids; @@ -554,7 +554,7 @@ static gid_t add_gid(struct udev_rules *rules, const char *group) { if (add < 1) add = 8; - gids = realloc(rules->gids, (rules->gids_max + add ) * sizeof(struct uid_gid)); + gids = reallocarray(rules->gids, rules->gids_max + add, sizeof(struct uid_gid)); if (gids == NULL) return gid; rules->gids = gids; diff --git a/src/udev/udevadm-hwdb.c b/src/udev/udevadm-hwdb.c index ab5dc7ab64..dc3ae7484d 100644 --- a/src/udev/udevadm-hwdb.c +++ b/src/udev/udevadm-hwdb.c @@ -94,7 +94,7 @@ static int node_add_child(struct trie *trie, struct trie_node *node, struct trie struct trie_child_entry *child; /* extend array, add new entry, sort for bisection */ - child = realloc(node->children, (node->children_count + 1) * sizeof(struct trie_child_entry)); + child = reallocarray(node->children, node->children_count + 1, sizeof(struct trie_child_entry)); if (!child) return -ENOMEM; @@ -166,7 +166,7 @@ static int trie_node_add_value(struct trie *trie, struct trie_node *node, } /* extend array, add new entry, sort for bisection */ - val = realloc(node->values, (node->values_count + 1) * sizeof(struct trie_value_entry)); + val = reallocarray(node->values, node->values_count + 1, sizeof(struct trie_value_entry)); if (!val) return -ENOMEM; trie->values_count++;