--- /dev/null
+@@
+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)
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);
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;
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;
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;
k = strv_length(*l);
- nl = realloc(*l, sizeof(char*) * (k + n + 1));
+ nl = reallocarray(*l, k + n + 1, sizeof(char *));
if (!nl)
return -ENOMEM;
} 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;
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;
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;
}
/* 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++;
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
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
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);
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;
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;
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;
}
/* 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);
}
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);
(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;
}
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();
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();
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();
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();
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;
#include <stdlib.h>
#include <string.h>
+#include "alloc-util.h"
#include "macro.h"
#include "uid-range.h"
#include "user-util.h"
} else {
UidRange *t;
- t = realloc(*p, sizeof(UidRange) * (*n + 1));
+ t = reallocarray(*p, *n + 1, sizeof(UidRange));
if (!t)
return -ENOMEM;
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;
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;
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;
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;
}
/* 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++;