coccinelle: add reallocarray() coccinelle script
authorLennart Poettering <lennart@poettering.net>
Tue, 27 Feb 2018 18:09:22 +0000 (19:09 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 2 Mar 2018 11:39:07 +0000 (12:39 +0100)
Let's systematically make use of reallocarray() whereever we invoke
realloc() with a product of two values.

16 files changed:
coccinelle/reallocarray.cocci [new file with mode: 0644]
src/basic/env-util.c
src/basic/prioq.c
src/basic/strbuf.c
src/basic/strv.c
src/core/service.c
src/hwdb/hwdb.c
src/libsystemd-network/network-internal.c
src/libsystemd/sd-bus/bus-message.c
src/libsystemd/sd-bus/bus-socket.c
src/libudev/libudev-list.c
src/network/networkd-network.c
src/shared/install.c
src/shared/uid-range.c
src/udev/udev-rules.c
src/udev/udevadm-hwdb.c

diff --git a/coccinelle/reallocarray.cocci b/coccinelle/reallocarray.cocci
new file mode 100644 (file)
index 0000000..21fe9df
--- /dev/null
@@ -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)
index ab3fc71..0b1d086 100644 (file)
@@ -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);
index 407b17e..2a1f1af 100644 (file)
@@ -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;
 
index 8befffa..bc3e56c 100644 (file)
@@ -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;
index 020fa26..e80ff4a 100644 (file)
@@ -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;
 
index 1997c82..df36019 100644 (file)
@@ -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;
 
index 4540260..f27f60e 100644 (file)
@@ -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++;
index 94386e4..584a1f3 100644 (file)
@@ -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
index 95a87da..59b2422 100644 (file)
@@ -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);
index 90132bb..44f71fd 100644 (file)
@@ -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;
index 29fbdbd..681e2e4 100644 (file)
@@ -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;
 }
index 48da83c..709ae2a 100644 (file)
@@ -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();
 
index fb2231b..ed5f51c 100644 (file)
@@ -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;
index c38b7cc..37d7c98 100644 (file)
@@ -22,6 +22,7 @@
 #include <stdlib.h>
 #include <string.h>
 
+#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;
 
index f4708bb..3b0ddb5 100644 (file)
@@ -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;
index ab5dc7a..dc3ae74 100644 (file)
@@ -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++;