1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-22 17:35:35 +03:00

coccinelle: add reallocarray() coccinelle script

Let's systematically make use of reallocarray() whereever we invoke
realloc() with a product of two values.
This commit is contained in:
Lennart Poettering 2018-02-27 19:09:22 +01:00
parent 3209c8e650
commit 62d74c78b5
16 changed files with 56 additions and 37 deletions

View File

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

View File

@ -721,7 +721,7 @@ char **replace_env_argv(char **argv, char **env) {
q = strv_length(m); q = strv_length(m);
l = l + q - 1; l = l + q - 1;
w = realloc(ret, sizeof(char*) * (l+1)); w = reallocarray(ret, l + 1, sizeof(char *));
if (!w) { if (!w) {
ret[k] = NULL; ret[k] = NULL;
strv_free(ret); strv_free(ret);

View File

@ -173,7 +173,7 @@ int prioq_put(Prioq *q, void *data, unsigned *idx) {
struct prioq_item *j; struct prioq_item *j;
n = MAX((q->n_items+1) * 2, 16u); 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) if (!j)
return -ENOMEM; return -ENOMEM;

View File

@ -190,7 +190,7 @@ ssize_t strbuf_add_string(struct strbuf *str, const char *s, size_t len) {
node_child->value_len = len; node_child->value_len = len;
/* extend array, add new entry, sort for bisection */ /* 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) { if (!child) {
free(node_child); free(node_child);
return -ENOMEM; return -ENOMEM;

View File

@ -214,7 +214,7 @@ int strv_extend_strv(char ***a, char **b, bool filter_duplicates) {
p = strv_length(*a); p = strv_length(*a);
q = strv_length(b); q = strv_length(b);
t = realloc(*a, sizeof(char*) * (p + q + 1)); t = reallocarray(*a, p + q + 1, sizeof(char *));
if (!t) if (!t)
return -ENOMEM; return -ENOMEM;
@ -861,7 +861,7 @@ int strv_extend_n(char ***l, const char *value, size_t n) {
k = strv_length(*l); k = strv_length(*l);
nl = realloc(*l, sizeof(char*) * (k + n + 1)); nl = reallocarray(*l, k + n + 1, sizeof(char *));
if (!nl) if (!nl)
return -ENOMEM; return -ENOMEM;

View File

@ -1254,7 +1254,7 @@ static int service_collect_fds(Service *s,
} else { } else {
int *t; int *t;
t = realloc(rfds, (rn_socket_fds + cn_fds) * sizeof(int)); t = reallocarray(rfds, rn_socket_fds + cn_fds, sizeof(int));
if (!t) if (!t)
return -ENOMEM; return -ENOMEM;
@ -1276,13 +1276,13 @@ static int service_collect_fds(Service *s,
char **nl; char **nl;
int *t; 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) if (!t)
return -ENOMEM; return -ENOMEM;
rfds = t; 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) if (!nl)
return -ENOMEM; return -ENOMEM;

View File

@ -103,7 +103,7 @@ static int node_add_child(struct trie *trie, struct trie_node *node, struct trie
struct trie_child_entry *child; struct trie_child_entry *child;
/* extend array, add new entry, sort for bisection */ /* 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) if (!child)
return -ENOMEM; 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 */ /* 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) if (!val)
return -ENOMEM; return -ENOMEM;
trie->values_count++; trie->values_count++;

View File

@ -424,7 +424,7 @@ int deserialize_in_addrs(struct in_addr **ret, const char *string) {
if (r == 0) if (r == 0)
break; break;
new_addresses = realloc(addresses, (size + 1) * sizeof(struct in_addr)); new_addresses = reallocarray(addresses, size + 1, sizeof(struct in_addr));
if (!new_addresses) if (!new_addresses)
return -ENOMEM; return -ENOMEM;
else else
@ -478,7 +478,7 @@ int deserialize_in6_addrs(struct in6_addr **ret, const char *string) {
if (r == 0) if (r == 0)
break; break;
new_addresses = realloc(addresses, (size + 1) * sizeof(struct in6_addr)); new_addresses = reallocarray(addresses, size + 1, sizeof(struct in6_addr));
if (!new_addresses) if (!new_addresses)
return -ENOMEM; return -ENOMEM;
else else

View File

@ -1400,7 +1400,7 @@ static int message_push_fd(sd_bus_message *m, int fd) {
if (copy < 0) if (copy < 0)
return -errno; return -errno;
f = realloc(m->fds, sizeof(int) * (m->n_fds + 1)); f = reallocarray(m->fds, sizeof(int), m->n_fds + 1);
if (!f) { if (!f) {
m->poisoned = true; m->poisoned = true;
safe_close(copy); safe_close(copy);

View File

@ -1223,7 +1223,7 @@ int bus_socket_read_message(sd_bus *bus) {
return -EIO; return -EIO;
} }
f = realloc(bus->fds, sizeof(int) * (bus->n_fds + n)); f = reallocarray(bus->fds, bus->n_fds + n, sizeof(int));
if (!f) { if (!f) {
close_many((int*) CMSG_DATA(cmsg), n); close_many((int*) CMSG_DATA(cmsg), n);
return -ENOMEM; return -ENOMEM;

View File

@ -140,8 +140,7 @@ static int list_search(struct udev_list *list, const char *name)
return -(first+1); 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; struct udev_list_entry *entry;
int i = 0; 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]; entry = list->entries[i];
free(entry->value); free(entry->value);
if (value == NULL) { if (!value) {
entry->value = NULL; entry->value = NULL;
return entry; return entry;
} }
entry->value = strdup(value); entry->value = strdup(value);
if (entry->value == NULL) if (!entry->value)
return NULL; return NULL;
return entry; return entry;
} }
@ -165,16 +164,16 @@ struct udev_list_entry *udev_list_entry_add(struct udev_list *list, const char *
/* add new name */ /* add new name */
entry = new0(struct udev_list_entry, 1); entry = new0(struct udev_list_entry, 1);
if (entry == NULL) if (!entry)
return NULL; return NULL;
entry->name = strdup(name); entry->name = strdup(name);
if (entry->name == NULL) if (!entry->name)
return mfree(entry); return mfree(entry);
if (value != NULL) { if (value) {
entry->value = strdup(value); entry->value = strdup(value);
if (entry->value == NULL) { if (!entry->value) {
free(entry->name); free(entry->name);
return mfree(entry); 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; add = list->entries_max;
if (add < 1) if (add < 1)
add = 64; add = 64;
entries = realloc(list->entries, (list->entries_max + add) * sizeof(struct udev_list_entry *)); entries = reallocarray(list->entries, list->entries_max + add, sizeof(struct udev_list_entry *));
if (entries == NULL) { if (!entries) {
free(entry->name); free(entry->name);
free(entry->value); free(entry->value);
return mfree(entry); 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_cur - i) * sizeof(struct udev_list_entry *));
list->entries[i] = entry; list->entries[i] = entry;
list->entries_cur++; list->entries_cur++;
} else { } else
udev_list_entry_append(entry, list); udev_list_entry_append(entry, list);
}
return entry; return entry;
} }

View File

@ -1072,7 +1072,7 @@ int config_parse_dhcp_server_dns(
continue; 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) if (!m)
return log_oom(); return log_oom();
@ -1120,7 +1120,7 @@ int config_parse_radv_dns(
if (in_addr_from_string(AF_INET6, w, &a) >= 0) { if (in_addr_from_string(AF_INET6, w, &a) >= 0) {
struct in6_addr *m; 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) if (!m)
return log_oom(); return log_oom();
@ -1223,7 +1223,7 @@ int config_parse_dhcp_server_ntp(
continue; 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) if (!m)
return log_oom(); return log_oom();
@ -1273,7 +1273,7 @@ int config_parse_dns(
continue; 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) if (!m)
return log_oom(); return log_oom();

View File

@ -311,7 +311,7 @@ int unit_file_changes_add(
if (!changes) if (!changes)
return 0; return 0;
c = realloc(*changes, (*n_changes + 1) * sizeof(UnitFileChange)); c = reallocarray(*changes, *n_changes + 1, sizeof(UnitFileChange));
if (!c) if (!c)
return -ENOMEM; return -ENOMEM;
*changes = c; *changes = c;

View File

@ -22,6 +22,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "alloc-util.h"
#include "macro.h" #include "macro.h"
#include "uid-range.h" #include "uid-range.h"
#include "user-util.h" #include "user-util.h"
@ -109,7 +110,7 @@ int uid_range_add(UidRange **p, unsigned *n, uid_t start, uid_t nr) {
} else { } else {
UidRange *t; UidRange *t;
t = realloc(*p, sizeof(UidRange) * (*n + 1)); t = reallocarray(*p, *n + 1, sizeof(UidRange));
if (!t) if (!t)
return -ENOMEM; return -ENOMEM;

View File

@ -465,7 +465,7 @@ static int add_token(struct udev_rules *rules, struct token *token) {
if (add < 8) if (add < 8)
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) if (tokens == NULL)
return -1; return -1;
rules->tokens = tokens; rules->tokens = tokens;
@ -511,7 +511,7 @@ static uid_t add_uid(struct udev_rules *rules, const char *owner) {
if (add < 1) if (add < 1)
add = 8; 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) if (uids == NULL)
return uid; return uid;
rules->uids = uids; rules->uids = uids;
@ -554,7 +554,7 @@ static gid_t add_gid(struct udev_rules *rules, const char *group) {
if (add < 1) if (add < 1)
add = 8; 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) if (gids == NULL)
return gid; return gid;
rules->gids = gids; rules->gids = gids;

View File

@ -94,7 +94,7 @@ static int node_add_child(struct trie *trie, struct trie_node *node, struct trie
struct trie_child_entry *child; struct trie_child_entry *child;
/* extend array, add new entry, sort for bisection */ /* 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) if (!child)
return -ENOMEM; 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 */ /* 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) if (!val)
return -ENOMEM; return -ENOMEM;
trie->values_count++; trie->values_count++;