]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: replace reallocarray() with GREEDY_REALLOC() 34674/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 8 Oct 2024 06:26:02 +0000 (15:26 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 8 Oct 2024 10:56:09 +0000 (19:56 +0900)
18 files changed:
src/core/dbus-execute.c
src/core/load-fragment.c
src/core/namespace.c
src/cryptenroll/cryptenroll.c
src/libsystemd-network/network-internal.c
src/libsystemd/sd-bus/bus-error.c
src/libsystemd/sd-bus/bus-message.c
src/libsystemd/sd-login/sd-login.c
src/network/networkd-dns.c
src/network/networkd-radv.c
src/nspawn/nspawn-bind-user.c
src/nspawn/nspawn-mount.c
src/nspawn/nspawn-oci.c
src/portable/portable.c
src/shared/bus-unit-util.c
src/shared/hwdb-util.c
src/shared/install.c
src/shared/user-record.c

index abb5f1f81ea91f63e4441efa5e58558a625080b6..d42d785f1941c4dfa5444eb847acaec5a23a9d8b 100644 (file)
@@ -2377,7 +2377,6 @@ int bus_exec_context_set_transient_property(
 
                 for (;;) {
                         _cleanup_free_ void *copy = NULL;
-                        struct iovec *t;
                         const char *eq;
                         const void *p;
                         size_t sz;
@@ -2404,13 +2403,6 @@ int bus_exec_context_set_transient_property(
                         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;
@@ -2419,10 +2411,12 @@ int bus_exec_context_set_transient_property(
                                 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++;
index 107b55d8ecc099b6cb7945d375893fd9f3a18cbf..ba6aad2f2b416a778f2b93e2cf99a0943e95b8e1 100644 (file)
@@ -2935,7 +2935,6 @@ int config_parse_log_extra_fields(
 
         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);
@@ -2965,14 +2964,11 @@ int config_parse_log_extra_fields(
                         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);
         }
 }
 
index c04ddedf5d6b8875da428218f23a33851ee8cbdb..b7fe4ffbcce00ebc3c2484b1b59c58df077c5d47 100644 (file)
@@ -2837,7 +2837,6 @@ MountImage* mount_image_free_many(MountImage *m, size_t *n) {
 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);
@@ -2870,13 +2869,10 @@ int mount_image_add(MountImage **m, size_t *n, const MountImage *item) {
                 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),
@@ -2905,7 +2901,6 @@ int temporary_filesystem_add(
                 const char *options) {
 
         _cleanup_free_ char *p = NULL, *o = NULL;
-        TemporaryFileSystem *c;
 
         assert(t);
         assert(n);
@@ -2921,13 +2916,10 @@ int temporary_filesystem_add(
                         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),
         };
index a205f59dcc3602b7d9ef793eb85efe19c9ca3c07..e1f72a352980082a20a24e0d46d0806e4cb6847f 100644 (file)
@@ -606,19 +606,15 @@ static int parse_argv(int argc, char *argv[]) {
                                 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;
                                 }
                         }
index c8aa021ee064d6ea090871cea1afa9d570da0751..a2b88963dac25985338a1cda8234fe35743613b4 100644 (file)
@@ -51,7 +51,7 @@ int deserialize_in_addrs(struct in_addr **ret, const char *string) {
 
         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);
@@ -60,17 +60,13 @@ int deserialize_in_addrs(struct in_addr **ret, const char *string) {
                 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;
@@ -104,7 +100,7 @@ int deserialize_in6_addrs(struct in6_addr **ret, const char *string) {
 
         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);
@@ -113,17 +109,13 @@ int deserialize_in6_addrs(struct in6_addr **ret, const char *string) {
                 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);
index a5146fd6e6c944a25b6bc99de8f43be92fe45f27..58c24d25c067f7be4e7a892785cf7883619b27b1 100644 (file)
@@ -602,7 +602,6 @@ static bool map_ok(const sd_bus_error_map *map) {
 }
 
 _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);
@@ -613,13 +612,11 @@ _public_ int sd_bus_error_add_map(const sd_bus_error_map *map) {
                         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;
 }
index a3a872778871f2a9920ac68595954689f38c63fd..c8962e4cc0661aba5312106f6367ba9178d0a3d8 100644 (file)
@@ -1276,7 +1276,7 @@ static void *message_extend_body(
 }
 
 static int message_push_fd(sd_bus_message *m, int fd) {
-        int *f, copy;
+        int copy;
 
         assert(m);
 
@@ -1290,15 +1290,13 @@ static int message_push_fd(sd_bus_message *m, int fd) {
         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;
index 4d91ba96f6c348dbe900966ea2399d5a7afce74e..3aca593622995a83742ee75f7dd31a79aacae726 100644 (file)
@@ -1048,9 +1048,8 @@ _public_ int sd_get_sessions(char ***sessions) {
 
 _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) {
@@ -1063,38 +1062,31 @@ _public_ int sd_get_uids(uid_t **users) {
         }
 
         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) {
index 74a371b6d7decb899bc1c05b96ee1c11af053d64..d3da34da83f08a8d6aee3e7ca52ab46f1bbebccc 100644 (file)
@@ -201,7 +201,6 @@ int config_parse_dns(
         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)
@@ -224,12 +223,10 @@ int config_parse_dns(
                 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);
         }
 }
 
index 347788eca72b7b8179714c796a99e1428d180cf5..5115646e7d038d31374ae2eb41fbbe94689529d6 100644 (file)
@@ -1331,13 +1331,10 @@ int config_parse_radv_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;
         }
 }
 
index 0960a6dcea64adf6f4ba11856afa42f1b3bc1d7e..d64a89f161c3f1346b0b361bb236c82da650c200 100644 (file)
@@ -230,7 +230,6 @@ int bind_user_prepare(
                 _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)
@@ -290,12 +289,9 @@ int bind_user_prepare(
                 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),
index 874d54e73462cc4a79f55ee95c64eb6f4b669fc2..3e579480fd7565ac344b21f54c7162306c3f5601 100644 (file)
 #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)++;
 
index 11ea7445622f77cea2af2a59c3cd951321d870ef..3228b19bbae098626555a881ff0f6e1591da9664 100644 (file)
@@ -341,7 +341,7 @@ static int oci_supplementary_gids(const char *name, sd_json_variant *v, sd_json_
         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),
@@ -351,11 +351,9 @@ static int oci_supplementary_gids(const char *name, sd_json_variant *v, sd_json_
                 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;
         }
 
@@ -805,15 +803,12 @@ static int oci_devices(const char *name, sd_json_variant *v, sd_json_dispatch_fl
                         {}
                 };
 
-                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,
@@ -960,7 +955,7 @@ static int oci_cgroup_devices(const char *name, sd_json_variant *v, sd_json_disp
                 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 },
@@ -1012,11 +1007,9 @@ static int oci_cgroup_devices(const char *name, sd_json_variant *v, sd_json_disp
                                                 "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;
         }
 
@@ -1750,14 +1743,12 @@ static int oci_seccomp_args(const char *name, sd_json_variant *v, sd_json_dispat
                         {},
                 };
 
-                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) {
@@ -2014,7 +2005,7 @@ static int oci_hooks_array(const char *name, sd_json_variant *v, sd_json_dispatc
                         {}
                 };
 
-                OciHook *a, **array, *new_item;
+                OciHook **array, *new_item;
                 size_t *n_array;
 
                 if (streq(name, "prestart")) {
@@ -2029,12 +2020,10 @@ static int oci_hooks_array(const char *name, sd_json_variant *v, sd_json_dispatc
                         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,
index b1055aa8c365ef4fcf17ca04003d40db8d751f33..5476a866142323a3a6459f76eff9785dadfa1e13 100644 (file)
@@ -904,7 +904,6 @@ static int portable_changes_add(
                 const char *source) {
 
         _cleanup_free_ char *p = NULL, *s = NULL;
-        PortableChange *c;
         int r;
 
         assert(path);
@@ -918,10 +917,8 @@ static int portable_changes_add(
         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)
@@ -931,7 +928,7 @@ static int portable_changes_add(
         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),
index fc923314364a61377a772dcad2bf5138d205d51f..9fdf56620522b54fc1742e4a0a911b83824780ef 100644 (file)
@@ -2423,15 +2423,13 @@ static int bus_append_service_property(sd_bus_message *m, const char *field, con
                         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;
index afc1f54da3df4769aebe11eaef84f2a56dec4bee..bc4da846f1bcb604c956b91b3e19284016f895fb 100644 (file)
@@ -76,18 +76,15 @@ static int trie_children_cmp(const struct trie_child_entry *a, const struct trie
 }
 
 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++;
 
@@ -136,7 +133,6 @@ static int trie_node_add_value(struct trie *trie, struct trie_node *node,
                                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)
@@ -155,7 +151,7 @@ static int trie_node_add_value(struct trie *trie, struct trie_node *node,
                 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) {
@@ -170,19 +166,17 @@ static int trie_node_add_value(struct trie *trie, struct trie_node *node,
         }
 
         /* 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;
 }
index 08c2915fb5326e76b11974c828a2410a153d7313..6d87858a3ce960fda58833dc2570a29bf3a113c2 100644 (file)
@@ -295,7 +295,6 @@ InstallChangeType install_changes_add(
                 const char *source) {
 
         _cleanup_free_ char *p = NULL, *s = NULL;
-        InstallChange *c;
         int r;
 
         assert(!changes == !n_changes);
@@ -310,10 +309,8 @@ InstallChangeType install_changes_add(
         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)
@@ -323,7 +320,7 @@ InstallChangeType install_changes_add(
         if (r < 0)
                 return r;
 
-        c[(*n_changes)++] = (InstallChange) {
+        (*changes)[(*n_changes)++] = (InstallChange) {
                 .type = type,
                 .path = TAKE_PTR(p),
                 .source = TAKE_PTR(s),
index 0de768cc59f3d164ed1a02b882796ead5c7b6d4e..f14a38e03bf2558e30d1813990f6d53d285c7352 100644 (file)
@@ -771,8 +771,6 @@ static int dispatch_pkcs11_key(const char *name, sd_json_variant *variant, sd_js
                 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 },
@@ -783,12 +781,10 @@ static int dispatch_pkcs11_key(const char *name, sd_json_variant *variant, sd_js
                 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);
@@ -834,20 +830,16 @@ static int dispatch_fido2_hmac_credential_array(const char *name, sd_json_varian
                 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,
@@ -889,8 +881,6 @@ static int dispatch_fido2_hmac_salt(const char *name, sd_json_variant *variant,
                 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 },
@@ -904,12 +894,10 @@ static int dispatch_fido2_hmac_salt(const char *name, sd_json_variant *variant,
                 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,
@@ -937,8 +925,6 @@ static int dispatch_recovery_key(const char *name, sd_json_variant *variant, sd_
                 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 },
@@ -948,12 +934,10 @@ static int dispatch_recovery_key(const char *name, sd_json_variant *variant, sd_
                 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);