Skip to content

Commit 650f401

Browse files
authored
Merge pull request systemd#8336 from poettering/coccinelle-reallocarray
reallocarray() coccinellization
2 parents 774a928 + 8419d45 commit 650f401

File tree

19 files changed

+79
-40
lines changed

19 files changed

+79
-40
lines changed

coccinelle/malloc_multiply.cocci

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
@@
2+
expression q, n, m;
3+
@@
4+
- q = malloc((n)*(m))
5+
+ q = malloc_multiply(n, m)
6+
@@
7+
expression q, n, m;
8+
@@
9+
- q = malloc(n*(m))
10+
+ q = malloc_multiply(n, m)
11+
@@
12+
expression q, n, m;
13+
@@
14+
- q = malloc((n)*m)
15+
+ q = malloc_multiply(n, m)
16+
@@
17+
expression q, n, m;
18+
@@
19+
- q = malloc(n*m)
20+
+ q = malloc_multiply(n, m)

coccinelle/reallocarray.cocci

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
@@
2+
expression q, p, n, m;
3+
@@
4+
- q = realloc(p, (n)*(m))
5+
+ q = reallocarray(p, n, m)
6+
@@
7+
expression q, p, n, m;
8+
@@
9+
- q = realloc(p, n*(m))
10+
+ q = reallocarray(p, n, m)
11+
@@
12+
expression q, p, n, m;
13+
@@
14+
- q = realloc(p, (n)*m)
15+
+ q = reallocarray(p, n, m)
16+
@@
17+
expression q, p, n, m;
18+
@@
19+
- q = realloc(p, n*m)
20+
+ q = reallocarray(p, n, m)

src/basic/env-util.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ char **replace_env_argv(char **argv, char **env) {
721721
q = strv_length(m);
722722
l = l + q - 1;
723723

724-
w = realloc(ret, sizeof(char*) * (l+1));
724+
w = reallocarray(ret, l + 1, sizeof(char *));
725725
if (!w) {
726726
ret[k] = NULL;
727727
strv_free(ret);

src/basic/prioq.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ int prioq_put(Prioq *q, void *data, unsigned *idx) {
173173
struct prioq_item *j;
174174

175175
n = MAX((q->n_items+1) * 2, 16u);
176-
j = realloc(q->items, sizeof(struct prioq_item) * n);
176+
j = reallocarray(q->items, n, sizeof(struct prioq_item));
177177
if (!j)
178178
return -ENOMEM;
179179

src/basic/strbuf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ ssize_t strbuf_add_string(struct strbuf *str, const char *s, size_t len) {
190190
node_child->value_len = len;
191191

192192
/* extend array, add new entry, sort for bisection */
193-
child = realloc(node->children, (node->children_count + 1) * sizeof(struct strbuf_child_entry));
193+
child = reallocarray(node->children, node->children_count + 1, sizeof(struct strbuf_child_entry));
194194
if (!child) {
195195
free(node_child);
196196
return -ENOMEM;

src/basic/strv.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ int strv_extend_strv(char ***a, char **b, bool filter_duplicates) {
214214
p = strv_length(*a);
215215
q = strv_length(b);
216216

217-
t = realloc(*a, sizeof(char*) * (p + q + 1));
217+
t = reallocarray(*a, p + q + 1, sizeof(char *));
218218
if (!t)
219219
return -ENOMEM;
220220

@@ -861,7 +861,7 @@ int strv_extend_n(char ***l, const char *value, size_t n) {
861861

862862
k = strv_length(*l);
863863

864-
nl = realloc(*l, sizeof(char*) * (k + n + 1));
864+
nl = reallocarray(*l, k + n + 1, sizeof(char *));
865865
if (!nl)
866866
return -ENOMEM;
867867

src/core/service.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,7 +1254,7 @@ static int service_collect_fds(Service *s,
12541254
} else {
12551255
int *t;
12561256

1257-
t = realloc(rfds, (rn_socket_fds + cn_fds) * sizeof(int));
1257+
t = reallocarray(rfds, rn_socket_fds + cn_fds, sizeof(int));
12581258
if (!t)
12591259
return -ENOMEM;
12601260

@@ -1276,13 +1276,13 @@ static int service_collect_fds(Service *s,
12761276
char **nl;
12771277
int *t;
12781278

1279-
t = realloc(rfds, (rn_socket_fds + s->n_fd_store) * sizeof(int));
1279+
t = reallocarray(rfds, rn_socket_fds + s->n_fd_store, sizeof(int));
12801280
if (!t)
12811281
return -ENOMEM;
12821282

12831283
rfds = t;
12841284

1285-
nl = realloc(rfd_names, (rn_socket_fds + s->n_fd_store + 1) * sizeof(char*));
1285+
nl = reallocarray(rfd_names, rn_socket_fds + s->n_fd_store + 1, sizeof(char *));
12861286
if (!nl)
12871287
return -ENOMEM;
12881288

src/hwdb/hwdb.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ static int node_add_child(struct trie *trie, struct trie_node *node, struct trie
103103
struct trie_child_entry *child;
104104

105105
/* extend array, add new entry, sort for bisection */
106-
child = realloc(node->children, (node->children_count + 1) * sizeof(struct trie_child_entry));
106+
child = reallocarray(node->children, node->children_count + 1, sizeof(struct trie_child_entry));
107107
if (!child)
108108
return -ENOMEM;
109109

@@ -197,7 +197,7 @@ static int trie_node_add_value(struct trie *trie, struct trie_node *node,
197197
}
198198

199199
/* extend array, add new entry, sort for bisection */
200-
val = realloc(node->values, (node->values_count + 1) * sizeof(struct trie_value_entry));
200+
val = reallocarray(node->values, node->values_count + 1, sizeof(struct trie_value_entry));
201201
if (!val)
202202
return -ENOMEM;
203203
trie->values_count++;

src/libsystemd-network/network-internal.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ int deserialize_in_addrs(struct in_addr **ret, const char *string) {
424424
if (r == 0)
425425
break;
426426

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

481-
new_addresses = realloc(addresses, (size + 1) * sizeof(struct in6_addr));
481+
new_addresses = reallocarray(addresses, size + 1, sizeof(struct in6_addr));
482482
if (!new_addresses)
483483
return -ENOMEM;
484484
else

src/libsystemd/sd-bus/bus-message.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1400,7 +1400,7 @@ static int message_push_fd(sd_bus_message *m, int fd) {
14001400
if (copy < 0)
14011401
return -errno;
14021402

1403-
f = realloc(m->fds, sizeof(int) * (m->n_fds + 1));
1403+
f = reallocarray(m->fds, sizeof(int), m->n_fds + 1);
14041404
if (!f) {
14051405
m->poisoned = true;
14061406
safe_close(copy);

0 commit comments

Comments
 (0)