for (;;) {
_cleanup_free_ void *copy = NULL;
- struct iovec *t;
const char *eq;
const void *p;
size_t sz;
if (!journal_field_valid(p, eq - (const char*) p, false))
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Journal field invalid");
- if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
- t = reallocarray(c->log_extra_fields, c->n_log_extra_fields+1, sizeof(struct iovec));
- if (!t)
- return -ENOMEM;
- c->log_extra_fields = t;
- }
-
copy = memdup_suffix0(p, sz);
if (!copy)
return -ENOMEM;
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Journal field is not valid UTF-8");
if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
+ if (!GREEDY_REALLOC(c->log_extra_fields, c->n_log_extra_fields + 1))
+ return -ENOMEM;
+
c->log_extra_fields[c->n_log_extra_fields++] = IOVEC_MAKE(copy, sz);
unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS|UNIT_ESCAPE_C, name, "LogExtraFields=%s", (char*) copy);
-
- copy = NULL;
+ TAKE_PTR(copy);
}
n++;
for (const char *p = rvalue;;) {
_cleanup_free_ char *word = NULL, *k = NULL;
- struct iovec *t;
const char *eq;
r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE);
continue;
}
- t = reallocarray(c->log_extra_fields, c->n_log_extra_fields+1, sizeof(struct iovec));
- if (!t)
+ if (!GREEDY_REALLOC(c->log_extra_fields, c->n_log_extra_fields + 1))
return log_oom();
- c->log_extra_fields = t;
c->log_extra_fields[c->n_log_extra_fields++] = IOVEC_MAKE_STRING(k);
-
- k = NULL;
+ TAKE_PTR(k);
}
}
int mount_image_add(MountImage **m, size_t *n, const MountImage *item) {
_cleanup_free_ char *s = NULL, *d = NULL;
_cleanup_(mount_options_free_allp) MountOptions *options = NULL;
- MountImage *c;
assert(m);
assert(n);
LIST_APPEND(mount_options, options, TAKE_PTR(o));
}
- c = reallocarray(*m, *n + 1, sizeof(MountImage));
- if (!c)
+ if (!GREEDY_REALLOC(*m, *n + 1))
return -ENOMEM;
- *m = c;
-
- c[(*n)++] = (MountImage) {
+ (*m)[(*n)++] = (MountImage) {
.source = TAKE_PTR(s),
.destination = TAKE_PTR(d),
.mount_options = TAKE_PTR(options),
const char *options) {
_cleanup_free_ char *p = NULL, *o = NULL;
- TemporaryFileSystem *c;
assert(t);
assert(n);
return -ENOMEM;
}
- c = reallocarray(*t, *n + 1, sizeof(TemporaryFileSystem));
- if (!c)
+ if (!GREEDY_REALLOC(*t, *n + 1))
return -ENOMEM;
- *t = c;
-
- c[(*n)++] = (TemporaryFileSystem) {
+ (*t)[(*n)++] = (TemporaryFileSystem) {
.path = TAKE_PTR(p),
.options = TAKE_PTR(o),
};
else if (streq(slot, "tpm2"))
arg_wipe_slots_mask |= 1U << ENROLL_TPM2;
else {
- int *a;
-
r = safe_atou(slot, &n);
if (r < 0)
return log_error_errno(r, "Failed to parse slot index: %s", slot);
if (n > INT_MAX)
return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Slot index out of range: %u", n);
- a = reallocarray(arg_wipe_slots, arg_n_wipe_slots + 1, sizeof(int));
- if (!a)
+ if (!GREEDY_REALLOC(arg_wipe_slots, arg_n_wipe_slots + 1))
return log_oom();
- arg_wipe_slots = a;
arg_wipe_slots[arg_n_wipe_slots++] = (int) n;
}
}
for (;;) {
_cleanup_free_ char *word = NULL;
- struct in_addr *new_addresses;
+ union in_addr_union a;
int r;
r = extract_first_word(&string, &word, NULL, 0);
if (r == 0)
break;
- new_addresses = reallocarray(addresses, size + 1, sizeof(struct in_addr));
- if (!new_addresses)
- return -ENOMEM;
- else
- addresses = new_addresses;
-
- r = inet_pton(AF_INET, word, &(addresses[size]));
- if (r <= 0)
+ if (in_addr_from_string(AF_INET, word, &a) < 0)
continue;
- size++;
+ if (!GREEDY_REALLOC(addresses, size + 1))
+ return -ENOMEM;
+
+ addresses[size++] = a.in;
}
*ret = size > 0 ? TAKE_PTR(addresses) : NULL;
for (;;) {
_cleanup_free_ char *word = NULL;
- struct in6_addr *new_addresses;
+ union in_addr_union a;
int r;
r = extract_first_word(&string, &word, NULL, 0);
if (r == 0)
break;
- new_addresses = reallocarray(addresses, size + 1, sizeof(struct in6_addr));
- if (!new_addresses)
- return -ENOMEM;
- else
- addresses = new_addresses;
-
- r = inet_pton(AF_INET6, word, &(addresses[size]));
- if (r <= 0)
+ if (in_addr_from_string(AF_INET6, word, &a) < 0)
continue;
- size++;
+ if (!GREEDY_REALLOC(addresses, size + 1))
+ return -ENOMEM;
+
+ addresses[size++] = a.in6;
}
*ret = TAKE_PTR(addresses);
}
_public_ int sd_bus_error_add_map(const sd_bus_error_map *map) {
- const sd_bus_error_map **maps = NULL;
unsigned n = 0;
assert_return(map, -EINVAL);
if (additional_error_maps[n] == map)
return 0;
- maps = reallocarray(additional_error_maps, n + 2, sizeof(struct sd_bus_error_map*));
- if (!maps)
+ if (!GREEDY_REALLOC(additional_error_maps, n + 2))
return -ENOMEM;
- maps[n] = map;
- maps[n+1] = NULL;
+ additional_error_maps[n] = map;
+ additional_error_maps[n+1] = NULL;
- additional_error_maps = maps;
return 1;
}
}
static int message_push_fd(sd_bus_message *m, int fd) {
- int *f, copy;
+ int copy;
assert(m);
if (copy < 0)
return -errno;
- f = reallocarray(m->fds, m->n_fds + 1, sizeof(int));
- if (!f) {
+ if (!GREEDY_REALLOC(m->fds, m->n_fds + 1)) {
m->poisoned = true;
safe_close(copy);
return -ENOMEM;
}
- m->fds = f;
- m->fds[m->n_fds] = copy;
+ m->fds[m->n_fds] = copy; /* m->n_fds will be incremented by the caller later */
m->free_fds = true;
return copy;
_public_ int sd_get_uids(uid_t **users) {
_cleanup_closedir_ DIR *d = NULL;
- int r = 0;
- unsigned n = 0;
_cleanup_free_ uid_t *l = NULL;
+ size_t n = 0;
d = opendir("/run/systemd/users/");
if (!d) {
}
FOREACH_DIRENT_ALL(de, d, return -errno) {
- int k;
uid_t uid;
if (!dirent_is_file(de))
continue;
- k = parse_uid(de->d_name, &uid);
- if (k < 0)
+ if (parse_uid(de->d_name, &uid) < 0)
continue;
if (users) {
- if ((unsigned) r >= n) {
- uid_t *t;
+ if (!GREEDY_REALLOC(l, n + 1))
+ return -ENOMEM;
- n = MAX(16, 2*r);
- t = reallocarray(l, n, sizeof(uid_t));
- if (!t)
- return -ENOMEM;
-
- l = t;
- }
+ l[n] = uid;
+ }
- assert((unsigned) r < n);
- l[r++] = uid;
- } else
- r++;
+ n++;
}
+ if (n > INT_MAX)
+ return -EOVERFLOW;
+
if (users)
*users = TAKE_PTR(l);
- return r;
+ return (int) n;
}
_public_ int sd_get_machine_names(char ***machines) {
for (const char *p = rvalue;;) {
_cleanup_(in_addr_full_freep) struct in_addr_full *dns = NULL;
_cleanup_free_ char *w = NULL;
- struct in_addr_full **m;
r = extract_first_word(&p, &w, NULL, 0);
if (r == -ENOMEM)
if (IN_SET(dns->port, 53, 853))
dns->port = 0;
- m = reallocarray(n->dns, n->n_dns + 1, sizeof(struct in_addr_full*));
- if (!m)
+ if (!GREEDY_REALLOC(n->dns, n->n_dns + 1))
return log_oom();
- m[n->n_dns++] = TAKE_PTR(dns);
- n->dns = m;
+ n->dns[n->n_dns++] = TAKE_PTR(dns);
}
}
}
}
- struct in6_addr *m;
- m = reallocarray(n->router_dns, n->n_router_dns + 1, sizeof(struct in6_addr));
- if (!m)
+ if (!GREEDY_REALLOC(n->router_dns, n->n_router_dns + 1))
return log_oom();
- m[n->n_router_dns++] = a.in6;
- n->router_dns = m;
+ n->router_dns[n->n_router_dns++] = a.in6;
}
}
_cleanup_(user_record_unrefp) UserRecord *u = NULL, *cu = NULL;
_cleanup_(group_record_unrefp) GroupRecord *g = NULL, *cg = NULL;
_cleanup_free_ char *sm = NULL, *sd = NULL;
- CustomMount *cm;
r = userdb_by_name(*n, USERDB_DONT_SYNTHESIZE, &u);
if (r < 0)
if (!sd)
return log_oom();
- cm = reallocarray(*custom_mounts, *n_custom_mounts + 1, sizeof(CustomMount));
- if (!cm)
+ if (!GREEDY_REALLOC(*custom_mounts, *n_custom_mounts + 1))
return log_oom();
- *custom_mounts = cm;
-
(*custom_mounts)[(*n_custom_mounts)++] = (CustomMount) {
.type = CUSTOM_MOUNT_BIND,
.source = TAKE_PTR(sm),
#include "user-util.h"
CustomMount* custom_mount_add(CustomMount **l, size_t *n, CustomMountType t) {
- CustomMount *c, *ret;
+ CustomMount *ret;
assert(l);
assert(n);
assert(t >= 0);
assert(t < _CUSTOM_MOUNT_TYPE_MAX);
- c = reallocarray(*l, *n + 1, sizeof(CustomMount));
- if (!c)
+ if (!GREEDY_REALLOC(*l, *n + 1))
return NULL;
- *l = c;
ret = *l + *n;
(*n)++;
int r;
JSON_VARIANT_ARRAY_FOREACH(e, v) {
- gid_t gid, *a;
+ gid_t gid;
if (!sd_json_variant_is_unsigned(e))
return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
if (r < 0)
return r;
- a = reallocarray(s->supplementary_gids, s->n_supplementary_gids + 1, sizeof(gid_t));
- if (!a)
+ if (!GREEDY_REALLOC(s->supplementary_gids, s->n_supplementary_gids + 1))
return log_oom();
- s->supplementary_gids = a;
s->supplementary_gids[s->n_supplementary_gids++] = gid;
}
{}
};
- DeviceNode *node, *nodes;
+ DeviceNode *node;
- nodes = reallocarray(s->extra_nodes, s->n_extra_nodes + 1, sizeof(DeviceNode));
- if (!nodes)
+ if (!GREEDY_REALLOC(s->extra_nodes, s->n_extra_nodes + 1))
return log_oom();
- s->extra_nodes = nodes;
-
- node = nodes + s->n_extra_nodes;
+ node = s->extra_nodes + s->n_extra_nodes;
*node = (DeviceNode) {
.uid = UID_INVALID,
.gid = GID_INVALID,
struct device_data data = {
.major = UINT_MAX,
.minor = UINT_MAX,
- }, *a;
+ };
static const sd_json_dispatch_field table[] = {
{ "allow", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(struct device_data, allow), SD_JSON_MANDATORY },
"Device cgroup allow list entries with no type not supported.");
}
- a = reallocarray(list, n_list + 1, sizeof(struct device_data));
- if (!a)
+ if (!GREEDY_REALLOC(list, n_list + 1))
return log_oom();
- list = a;
list[n_list++] = data;
}
{},
};
- struct scmp_arg_cmp *a, *p;
+ struct scmp_arg_cmp *p;
int expected;
- a = reallocarray(rule->arguments, rule->n_arguments + 1, sizeof(struct syscall_rule));
- if (!a)
+ if (!GREEDY_REALLOC(rule->arguments, rule->n_arguments + 1))
return log_oom();
- rule->arguments = a;
p = rule->arguments + rule->n_arguments;
*p = (struct scmp_arg_cmp) {
{}
};
- OciHook *a, **array, *new_item;
+ OciHook **array, *new_item;
size_t *n_array;
if (streq(name, "prestart")) {
n_array = &s->n_oci_hooks_poststop;
}
- a = reallocarray(*array, *n_array + 1, sizeof(OciHook));
- if (!a)
+ if (!GREEDY_REALLOC(*array, *n_array + 1))
return log_oom();
- *array = a;
- new_item = a + *n_array;
+ new_item = *array + *n_array;
*new_item = (OciHook) {
.timeout = USEC_INFINITY,
const char *source) {
_cleanup_free_ char *p = NULL, *s = NULL;
- PortableChange *c;
int r;
assert(path);
if (!changes)
return 0;
- c = reallocarray(*changes, *n_changes + 1, sizeof(PortableChange));
- if (!c)
+ if (!GREEDY_REALLOC(*changes, *n_changes + 1))
return -ENOMEM;
- *changes = c;
r = path_simplify_alloc(path, &p);
if (r < 0)
if (r < 0)
return r;
- c[(*n_changes)++] = (PortableChange) {
+ (*changes)[(*n_changes)++] = (PortableChange) {
.type_or_errno = type_or_errno,
.path = TAKE_PTR(p),
.source = TAKE_PTR(s),
if (r >= 0) {
assert(r >= 0 && r < 256);
- status = reallocarray(status, n_status + 1, sizeof(int));
- if (!status)
+ if (!GREEDY_REALLOC(status, n_status + 1))
return log_oom();
status[n_status++] = r;
} else if ((r = signal_from_string(word)) >= 0) {
- signal = reallocarray(signal, n_signal + 1, sizeof(int));
- if (!signal)
+ if (!GREEDY_REALLOC(signal, n_signal + 1))
return log_oom();
signal[n_signal++] = r;
}
static int node_add_child(struct trie *trie, struct trie_node *node, struct trie_node *node_child, uint8_t c) {
- struct trie_child_entry *child;
-
/* extend array, add new entry, sort for bisection */
- child = reallocarray(node->children, node->children_count + 1, sizeof(struct trie_child_entry));
- if (!child)
+ if (!GREEDY_REALLOC(node->children, node->children_count + 1))
return -ENOMEM;
- node->children = child;
trie->children_count++;
- node->children[node->children_count].c = c;
- node->children[node->children_count].child = node_child;
- node->children_count++;
+ node->children[node->children_count++] = (struct trie_child_entry) {
+ .c = c,
+ .child = node_child,
+ };
typesafe_qsort(node->children, node->children_count, trie_children_cmp);
trie->nodes_count++;
const char *key, const char *value,
const char *filename, uint16_t file_priority, uint32_t line_number, bool compat) {
ssize_t k, v, fn = 0;
- struct trie_value_entry *val;
k = strbuf_add_string(trie->strings, key);
if (k < 0)
struct trie_value_entry search = {
.key_off = k,
.value_off = v,
- };
+ }, *val;
val = typesafe_bsearch_r(&search, node->values, node->values_count, trie_values_cmp, trie);
if (val) {
}
/* extend array, add new entry, sort for bisection */
- val = reallocarray(node->values, node->values_count + 1, sizeof(struct trie_value_entry));
- if (!val)
+ if (!GREEDY_REALLOC(node->values, node->values_count + 1))
return -ENOMEM;
+
trie->values_count++;
- node->values = val;
- node->values[node->values_count] = (struct trie_value_entry) {
+ node->values[node->values_count++] = (struct trie_value_entry) {
.key_off = k,
.value_off = v,
.filename_off = fn,
.file_priority = file_priority,
.line_number = line_number,
};
- node->values_count++;
typesafe_qsort_r(node->values, node->values_count, trie_values_cmp, trie);
return 0;
}
const char *source) {
_cleanup_free_ char *p = NULL, *s = NULL;
- InstallChange *c;
int r;
assert(!changes == !n_changes);
if (!changes)
return type;
- c = reallocarray(*changes, *n_changes + 1, sizeof(InstallChange));
- if (!c)
+ if (!GREEDY_REALLOC(*changes, *n_changes + 1))
return -ENOMEM;
- *changes = c;
r = path_simplify_alloc(path, &p);
if (r < 0)
if (r < 0)
return r;
- c[(*n_changes)++] = (InstallChange) {
+ (*changes)[(*n_changes)++] = (InstallChange) {
.type = type,
.path = TAKE_PTR(p),
.source = TAKE_PTR(s),
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of objects.", strna(name));
JSON_VARIANT_ARRAY_FOREACH(e, variant) {
- Pkcs11EncryptedKey *array, *k;
-
static const sd_json_dispatch_field pkcs11_key_dispatch_table[] = {
{ "uri", SD_JSON_VARIANT_STRING, dispatch_pkcs11_uri, offsetof(Pkcs11EncryptedKey, uri), SD_JSON_MANDATORY },
{ "data", SD_JSON_VARIANT_STRING, dispatch_pkcs11_key_data, 0, SD_JSON_MANDATORY },
if (!sd_json_variant_is_object(e))
return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "JSON array element is not an object.");
- array = reallocarray(h->pkcs11_encrypted_key, h->n_pkcs11_encrypted_key + 1, sizeof(Pkcs11EncryptedKey));
- if (!array)
+ if (!GREEDY_REALLOC(h->pkcs11_encrypted_key, h->n_pkcs11_encrypted_key + 1))
return log_oom();
- h->pkcs11_encrypted_key = array;
- k = h->pkcs11_encrypted_key + h->n_pkcs11_encrypted_key;
+ Pkcs11EncryptedKey *k = h->pkcs11_encrypted_key + h->n_pkcs11_encrypted_key;
*k = (Pkcs11EncryptedKey) {};
r = sd_json_dispatch(e, pkcs11_key_dispatch_table, flags, k);
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of strings.", strna(name));
JSON_VARIANT_ARRAY_FOREACH(e, variant) {
- Fido2HmacCredential *array;
size_t l;
void *b;
- array = reallocarray(h->fido2_hmac_credential, h->n_fido2_hmac_credential + 1, sizeof(Fido2HmacCredential));
- if (!array)
+ if (!GREEDY_REALLOC(h->fido2_hmac_credential, h->n_fido2_hmac_credential + 1))
return log_oom();
r = sd_json_variant_unbase64(e, &b, &l);
if (r < 0)
return json_log(variant, flags, r, "Failed to decode FIDO2 credential ID: %m");
- h->fido2_hmac_credential = array;
-
h->fido2_hmac_credential[h->n_fido2_hmac_credential++] = (Fido2HmacCredential) {
.id = b,
.size = l,
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of objects.", strna(name));
JSON_VARIANT_ARRAY_FOREACH(e, variant) {
- Fido2HmacSalt *array, *k;
-
static const sd_json_dispatch_field fido2_hmac_salt_dispatch_table[] = {
{ "credential", SD_JSON_VARIANT_STRING, dispatch_fido2_hmac_credential, offsetof(Fido2HmacSalt, credential), SD_JSON_MANDATORY },
{ "salt", SD_JSON_VARIANT_STRING, dispatch_fido2_hmac_salt_value, 0, SD_JSON_MANDATORY },
if (!sd_json_variant_is_object(e))
return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "JSON array element is not an object.");
- array = reallocarray(h->fido2_hmac_salt, h->n_fido2_hmac_salt + 1, sizeof(Fido2HmacSalt));
- if (!array)
+ if (!GREEDY_REALLOC(h->fido2_hmac_salt, h->n_fido2_hmac_salt + 1))
return log_oom();
- h->fido2_hmac_salt = array;
- k = h->fido2_hmac_salt + h->n_fido2_hmac_salt;
+ Fido2HmacSalt *k = h->fido2_hmac_salt + h->n_fido2_hmac_salt;
*k = (Fido2HmacSalt) {
.uv = -1,
.up = -1,
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of objects.", strna(name));
JSON_VARIANT_ARRAY_FOREACH(e, variant) {
- RecoveryKey *array, *k;
-
static const sd_json_dispatch_field recovery_key_dispatch_table[] = {
{ "type", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, 0, SD_JSON_MANDATORY },
{ "hashedPassword", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(RecoveryKey, hashed_password), SD_JSON_MANDATORY },
if (!sd_json_variant_is_object(e))
return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "JSON array element is not an object.");
- array = reallocarray(h->recovery_key, h->n_recovery_key + 1, sizeof(RecoveryKey));
- if (!array)
+ if (!GREEDY_REALLOC(h->recovery_key, h->n_recovery_key + 1))
return log_oom();
- h->recovery_key = array;
- k = h->recovery_key + h->n_recovery_key;
+ RecoveryKey *k = h->recovery_key + h->n_recovery_key;
*k = (RecoveryKey) {};
r = sd_json_dispatch(e, recovery_key_dispatch_table, flags, k);