]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
alloc-util: simplify GREEDY_REALLOC() logic by relying on malloc_usable_size() 19653/head
authorLennart Poettering <lennart@poettering.net>
Tue, 18 May 2021 21:01:32 +0000 (23:01 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 19 May 2021 14:42:37 +0000 (16:42 +0200)
We recently started making more use of malloc_usable_size() and rely on
it (see the string_erase() story). Given that we don't really support
sytems where malloc_usable_size() cannot be trusted beyond statistics
anyway, let's go fully in and rework GREEDY_REALLOC() on top of it:
instead of passing around and maintaining the currenly allocated size
everywhere, let's just derive it automatically from
malloc_usable_size().

I am mostly after this for the simplicity this brings. It also brings
minor efficiency improvements I guess, but things become so much nicer
to look at if we can avoid these allocation size variables everywhere.

Note that the malloc_usable_size() man page says relying on it wasn't
"good programming practice", but I think it does this for reasons that
don't apply here: the greedy realloc logic specifically doesn't rely on
the returned extra size, beyond the fact that it is equal or larger than
what was requested.

(This commit was supposed to be a quick patch btw, but apparently we use
the greedy realloc stuff quite a bit across the codebase, so this ends
up touching *a*lot* of code.)

127 files changed:
src/analyze/analyze-security.c
src/analyze/analyze.c
src/basic/alloc-util.c
src/basic/alloc-util.h
src/basic/btrfs-util.c
src/basic/cap-list.c
src/basic/cgroup-util.c
src/basic/env-file.c
src/basic/extract-word.c
src/basic/fileio.c
src/basic/fs-util.c
src/basic/hashmap.c
src/basic/io-util.c
src/basic/io-util.h
src/basic/process-util.c
src/basic/string-util.c
src/basic/strv.c
src/basic/time-util.c
src/busctl/busctl.c
src/core/execute.c
src/core/job.c
src/core/load-fragment.c
src/core/service.c
src/core/smack-setup.c
src/core/transaction.c
src/core/unit.c
src/cryptenroll/cryptenroll-list.c
src/delta/delta.c
src/fuzz/fuzz-compress.c
src/import/pull-job.c
src/import/pull-job.h
src/journal-remote/journal-remote.c
src/journal-remote/journal-remote.h
src/journal/fuzz-journald.c
src/journal/journald-audit.c
src/journal/journald-context.c
src/journal/journald-native.c
src/journal/journald-server.c
src/journal/journald-server.h
src/journal/journald-stream.c
src/libsystemd-network/dhcp-lease-internal.h
src/libsystemd-network/dhcp6-internal.h
src/libsystemd-network/dhcp6-lease-internal.h
src/libsystemd-network/dhcp6-option.c
src/libsystemd-network/ndisc-router.c
src/libsystemd-network/network-internal.c
src/libsystemd-network/network-internal.h
src/libsystemd-network/sd-dhcp-lease.c
src/libsystemd-network/sd-dhcp6-client.c
src/libsystemd-network/sd-dhcp6-lease.c
src/libsystemd/sd-bus/bus-creds.c
src/libsystemd/sd-bus/bus-internal.h
src/libsystemd/sd-bus/bus-match.c
src/libsystemd/sd-bus/bus-message.c
src/libsystemd/sd-bus/bus-message.h
src/libsystemd/sd-bus/bus-socket.c
src/libsystemd/sd-bus/sd-bus.c
src/libsystemd/sd-device/device-enumerator.c
src/libsystemd/sd-device/device-private.c
src/libsystemd/sd-event/sd-event.c
src/libsystemd/sd-journal/catalog.c
src/libsystemd/sd-journal/compress.c
src/libsystemd/sd-journal/compress.h
src/libsystemd/sd-journal/journal-file.c
src/libsystemd/sd-journal/journal-file.h
src/libsystemd/sd-journal/journal-internal.h
src/libsystemd/sd-journal/journal-vacuum.c
src/libsystemd/sd-journal/journal-verify.c
src/libsystemd/sd-journal/sd-journal.c
src/libsystemd/sd-journal/test-compress-benchmark.c
src/libsystemd/sd-journal/test-compress.c
src/libsystemd/sd-netlink/netlink-internal.h
src/libsystemd/sd-netlink/netlink-message.c
src/libsystemd/sd-netlink/netlink-socket.c
src/libsystemd/sd-netlink/sd-netlink.c
src/libsystemd/sd-network/sd-network.c
src/login/logind-dbus.c
src/login/logind-seat.c
src/login/logind-seat.h
src/machine/machine.c
src/machine/machinectl.c
src/mount/mount-tool.c
src/network/networkctl.c
src/network/networkd-dhcp-server.c
src/network/networkd-radv.c
src/network/test-network.c
src/nspawn/nspawn-setuid.c
src/partition/repart.c
src/portable/portabled-image-bus.c
src/pstore/pstore.c
src/resolve/resolvectl.c
src/resolve/resolved-dns-packet.c
src/resolve/resolved-etc-hosts.c
src/resolve/resolved-etc-hosts.h
src/resolve/test-resolved-etc-hosts.c
src/shared/bitmap.c
src/shared/bitmap.h
src/shared/bootspec.c
src/shared/bpf-program.c
src/shared/bpf-program.h
src/shared/bus-message-util.c
src/shared/bus-unit-procs.c
src/shared/cgroup-show.c
src/shared/cpu-set-util.c
src/shared/dissect-image.c
src/shared/dns-domain.c
src/shared/efi-loader.c
src/shared/format-table.c
src/shared/install.c
src/shared/journal-importer.c
src/shared/journal-importer.h
src/shared/json.c
src/shared/local-addresses.c
src/shared/sleep-config.c
src/shared/specifier.c
src/shared/varlink.c
src/systemctl/systemctl-list-jobs.c
src/systemctl/systemctl-list-machines.c
src/systemctl/systemctl-list-unit-files.c
src/systemctl/systemctl-list-units.c
src/systemctl/systemctl-util.c
src/test/test-alloc-util.c
src/test/test-nss-hosts.c
src/tmpfiles/tmpfiles.c
src/udev/cdrom_id/cdrom_id.c
src/udev/udevadm-info.c
src/xdg-autostart-generator/xdg-autostart-service.c

index fc5af11f55705f3b04d7d96ed97f94e9318e1b09..f20606c17ca4fc01110ba9a14d4b3d463cd77b80 100644 (file)
@@ -2116,7 +2116,7 @@ int analyze_security(sd_bus *bus, char **units, AnalyzeSecurityFlags flags) {
                 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
                 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
                 _cleanup_strv_free_ char **list = NULL;
-                size_t allocated = 0, n = 0;
+                size_t n = 0;
                 char **i;
 
                 r = sd_bus_call_method(
@@ -2148,7 +2148,7 @@ int analyze_security(sd_bus *bus, char **units, AnalyzeSecurityFlags flags) {
                         if (!endswith(info.id, ".service"))
                                 continue;
 
-                        if (!GREEDY_REALLOC(list, allocated, n + 2))
+                        if (!GREEDY_REALLOC(list, n + 2))
                                 return log_oom();
 
                         copy = strdup(info.id);
index 1ad373185263af47ac99f1861c9b03911fddd759..62c0ccbdfe3a732287b5d65b1d75eb571dcf4a45 100644 (file)
@@ -342,7 +342,7 @@ static int acquire_time_data(sd_bus *bus, UnitTimes **out) {
         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
         _cleanup_(unit_times_free_arrayp) UnitTimes *unit_times = NULL;
         BootTimes *boot_times = NULL;
-        size_t allocated = 0, c = 0;
+        size_t c = 0;
         UnitInfo u;
         int r;
 
@@ -361,7 +361,7 @@ static int acquire_time_data(sd_bus *bus, UnitTimes **out) {
         while ((r = bus_parse_unit_info(reply, &u)) > 0) {
                 UnitTimes *t;
 
-                if (!GREEDY_REALLOC(unit_times, allocated, c + 2))
+                if (!GREEDY_REALLOC(unit_times, c + 2))
                         return log_oom();
 
                 unit_times[c + 1].has_data = false;
index bad15cc204c953ffa5e3ebb2477d1caa25efe27d..5545af5497b38be7c162ddb00b5a5020098a1e4e 100644 (file)
@@ -39,24 +39,32 @@ void* memdup_suffix0(const void *p, size_t l) {
         return ret;
 }
 
-void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size) {
+void* greedy_realloc(
+                void **p,
+                size_t need,
+                size_t size) {
+
         size_t a, newalloc;
         void *q;
 
         assert(p);
-        assert(allocated);
 
-        if (*allocated >= need)
+        /* We use malloc_usable_size() for determining the current allocated size. On all systems we care
+         * about this should be safe to rely on. Should there ever arise the need to avoid relying on this we
+         * can instead locally fall back to realloc() on every call, rounded up to the next exponent of 2 or
+         * so. */
+
+        if (*p && (size == 0 || (MALLOC_SIZEOF_SAFE(*p) / size >= need)))
                 return *p;
 
         if (_unlikely_(need > SIZE_MAX/2)) /* Overflow check */
                 return NULL;
-
         newalloc = need * 2;
+
         if (size_multiply_overflow(newalloc, size))
                 return NULL;
-
         a = newalloc * size;
+
         if (a < 64) /* Allocate at least 64 bytes */
                 a = 64;
 
@@ -64,49 +72,34 @@ void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size) {
         if (!q)
                 return NULL;
 
-        if (size > 0) {
-                size_t bn;
-
-                /* Adjust for the 64 byte minimum */
-                newalloc = a / size;
-
-                bn = malloc_usable_size(q) / size;
-                if (bn > newalloc) {
-                        void *qq;
-
-                        /* The actual size allocated is larger than what we asked for. Let's call realloc() again to
-                         * take possession of the extra space. This should be cheap, since libc doesn't have to move
-                         * the memory for this. */
-
-                        qq = reallocarray(q, bn, size);
-                        if (_likely_(qq)) {
-                                *p = qq;
-                                *allocated = bn;
-                                return qq;
-                        }
-                }
-        }
-
-        *p = q;
-        *allocated = newalloc;
-        return q;
+        return *p = q;
 }
 
-void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size) {
-        size_t prev;
+void* greedy_realloc0(
+                void **p,
+                size_t need,
+                size_t size) {
+
+        size_t before, after;
         uint8_t *q;
 
         assert(p);
-        assert(allocated);
 
-        prev = *allocated;
+        before = MALLOC_SIZEOF_SAFE(*p); /* malloc_usable_size() will return 0 on NULL input, as per docs */
 
-        q = greedy_realloc(p, allocated, need, size);
+        q = greedy_realloc(p, need, size);
         if (!q)
                 return NULL;
 
-        if (*allocated > prev)
-                memzero(q + prev * size, (*allocated - prev) * size);
+        after = MALLOC_SIZEOF_SAFE(q);
+
+        if (size == 0) /* avoid division by zero */
+                before = 0;
+        else
+                before = (before / size) * size; /* Round down */
+
+        if (after > before)
+                memzero(q + before, after - before);
 
         return q;
 }
index 195795285bb6a2bb5afb52aeced9af11e9869fa7..3ef126955bba732cde48613e1e6c2e4e8faf0f51 100644 (file)
@@ -121,14 +121,14 @@ static inline void *memdup_suffix0_multiply(const void *p, size_t size, size_t n
         return memdup_suffix0(p, size * need);
 }
 
-void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size);
-void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size);
+void* greedy_realloc(void **p, size_t need, size_t size);
+void* greedy_realloc0(void **p, size_t need, size_t size);
 
-#define GREEDY_REALLOC(array, allocated, need)                          \
-        greedy_realloc((void**) &(array), &(allocated), (need), sizeof((array)[0]))
+#define GREEDY_REALLOC(array, need)                                     \
+        greedy_realloc((void**) &(array), (need), sizeof((array)[0]))
 
-#define GREEDY_REALLOC0(array, allocated, need)                         \
-        greedy_realloc0((void**) &(array), &(allocated), (need), sizeof((array)[0]))
+#define GREEDY_REALLOC0(array, need)                                    \
+        greedy_realloc0((void**) &(array), (need), sizeof((array)[0]))
 
 #define alloca0(n)                                      \
         ({                                              \
index 51aaee71fd7c10a57909902d79edd3915ef18706..bc1fb8d29d044270efdecc72784aed44683fcfc9 100644 (file)
@@ -1728,7 +1728,7 @@ int btrfs_qgroup_find_parents(int fd, uint64_t qgroupid, uint64_t **ret) {
         };
 
         _cleanup_free_ uint64_t *items = NULL;
-        size_t n_items = 0, n_allocated = 0;
+        size_t n_items = 0;
         int r;
 
         assert(fd >= 0);
@@ -1775,7 +1775,7 @@ int btrfs_qgroup_find_parents(int fd, uint64_t qgroupid, uint64_t **ret) {
                         if (sh->objectid != qgroupid)
                                 continue;
 
-                        if (!GREEDY_REALLOC(items, n_allocated, n_items+1))
+                        if (!GREEDY_REALLOC(items, n_items+1))
                                 return -ENOMEM;
 
                         items[n_items++] = sh->offset;
index d295a635ca9bbc38c099aad0b4aa06fdf5796a6f..fdc7948a23f70f74c2901e959884b28ff93879dc 100644 (file)
@@ -59,7 +59,7 @@ int capability_list_length(void) {
 
 int capability_set_to_string_alloc(uint64_t set, char **s) {
         _cleanup_free_ char *str = NULL;
-        size_t allocated = 0, n = 0;
+        size_t n = 0;
 
         assert(s);
 
@@ -77,14 +77,14 @@ int capability_set_to_string_alloc(uint64_t set, char **s) {
 
                         add = strlen(p);
 
-                        if (!GREEDY_REALLOC(str, allocated, n + add + 2))
+                        if (!GREEDY_REALLOC(str, n + add + 2))
                                 return -ENOMEM;
 
                         strcpy(mempcpy(str + n, p, add), " ");
                         n += add + 1;
                 }
 
-        if (!GREEDY_REALLOC(str, allocated, n + 1))
+        if (!GREEDY_REALLOC(str, n + 1))
                 return -ENOMEM;
 
         str[n > 0 ? n - 1 : 0] = '\0'; /* truncate the last space, if it's there */
index b790df6dbeb17d07e4ea311cffbfa796666659ea..e39bfd9bc747ac16024be261abcf28b9f41a406b 100644 (file)
@@ -1817,9 +1817,9 @@ done:
 
 int cg_mask_to_string(CGroupMask mask, char **ret) {
         _cleanup_free_ char *s = NULL;
-        size_t n = 0, allocated = 0;
         bool space = false;
         CGroupController c;
+        size_t n = 0;
 
         assert(ret);
 
@@ -1838,7 +1838,7 @@ int cg_mask_to_string(CGroupMask mask, char **ret) {
                 k = cgroup_controller_to_string(c);
                 l = strlen(k);
 
-                if (!GREEDY_REALLOC(s, allocated, n + space + l + 1))
+                if (!GREEDY_REALLOC(s, n + space + l + 1))
                         return -ENOMEM;
 
                 if (space)
index a3009093607523b9cd22a78c835d0efb9e677030..599b73bc22a68387d1a8298aecb5954113e4c2b3 100644 (file)
@@ -20,7 +20,7 @@ static int parse_env_file_internal(
                 void *userdata,
                 int *n_pushed) {
 
-        size_t key_alloc = 0, n_key = 0, value_alloc = 0, n_value = 0, last_value_whitespace = SIZE_MAX, last_key_whitespace = SIZE_MAX;
+        size_t n_key = 0, n_value = 0, last_value_whitespace = SIZE_MAX, last_key_whitespace = SIZE_MAX;
         _cleanup_free_ char *contents = NULL, *key = NULL, *value = NULL;
         unsigned line = 1;
         char *p;
@@ -58,7 +58,7 @@ static int parse_env_file_internal(
                                 state = KEY;
                                 last_key_whitespace = SIZE_MAX;
 
-                                if (!GREEDY_REALLOC(key, key_alloc, n_key+2))
+                                if (!GREEDY_REALLOC(key, n_key+2))
                                         return -ENOMEM;
 
                                 key[n_key++] = c;
@@ -79,7 +79,7 @@ static int parse_env_file_internal(
                                 else if (last_key_whitespace == SIZE_MAX)
                                          last_key_whitespace = n_key;
 
-                                if (!GREEDY_REALLOC(key, key_alloc, n_key+2))
+                                if (!GREEDY_REALLOC(key, n_key+2))
                                         return -ENOMEM;
 
                                 key[n_key++] = c;
@@ -106,7 +106,7 @@ static int parse_env_file_internal(
 
                                 n_key = 0;
                                 value = NULL;
-                                value_alloc = n_value = 0;
+                                n_value = 0;
 
                         } else if (c == '\'')
                                 state = SINGLE_QUOTE_VALUE;
@@ -117,7 +117,7 @@ static int parse_env_file_internal(
                         else if (!strchr(WHITESPACE, c)) {
                                 state = VALUE;
 
-                                if (!GREEDY_REALLOC(value, value_alloc, n_value+2))
+                                if (!GREEDY_REALLOC(value, n_value+2))
                                         return  -ENOMEM;
 
                                 value[n_value++] = c;
@@ -149,7 +149,7 @@ static int parse_env_file_internal(
 
                                 n_key = 0;
                                 value = NULL;
-                                value_alloc = n_value = 0;
+                                n_value = 0;
 
                         } else if (c == '\\') {
                                 state = VALUE_ESCAPE;
@@ -160,7 +160,7 @@ static int parse_env_file_internal(
                                 else if (last_value_whitespace == SIZE_MAX)
                                         last_value_whitespace = n_value;
 
-                                if (!GREEDY_REALLOC(value, value_alloc, n_value+2))
+                                if (!GREEDY_REALLOC(value, n_value+2))
                                         return -ENOMEM;
 
                                 value[n_value++] = c;
@@ -173,7 +173,7 @@ static int parse_env_file_internal(
 
                         if (!strchr(NEWLINE, c)) {
                                 /* Escaped newlines we eat up entirely */
-                                if (!GREEDY_REALLOC(value, value_alloc, n_value+2))
+                                if (!GREEDY_REALLOC(value, n_value+2))
                                         return -ENOMEM;
 
                                 value[n_value++] = c;
@@ -184,7 +184,7 @@ static int parse_env_file_internal(
                         if (c == '\'')
                                 state = PRE_VALUE;
                         else {
-                                if (!GREEDY_REALLOC(value, value_alloc, n_value+2))
+                                if (!GREEDY_REALLOC(value, n_value+2))
                                         return -ENOMEM;
 
                                 value[n_value++] = c;
@@ -198,7 +198,7 @@ static int parse_env_file_internal(
                         else if (c == '\\')
                                 state = DOUBLE_QUOTE_VALUE_ESCAPE;
                         else {
-                                if (!GREEDY_REALLOC(value, value_alloc, n_value+2))
+                                if (!GREEDY_REALLOC(value, n_value+2))
                                         return -ENOMEM;
 
                                 value[n_value++] = c;
@@ -211,13 +211,13 @@ static int parse_env_file_internal(
 
                         if (strchr(SHELL_NEED_ESCAPE, c)) {
                                 /* If this is a char that needs escaping, just unescape it. */
-                                if (!GREEDY_REALLOC(value, value_alloc, n_value+2))
+                                if (!GREEDY_REALLOC(value, n_value+2))
                                         return -ENOMEM;
                                 value[n_value++] = c;
                         } else if (c != '\n') {
                                 /* If other char than what needs escaping, keep the "\" in place, like the
                                  * real shell does. */
-                                if (!GREEDY_REALLOC(value, value_alloc, n_value+3))
+                                if (!GREEDY_REALLOC(value, n_value+3))
                                         return -ENOMEM;
                                 value[n_value++] = '\\';
                                 value[n_value++] = c;
index d1af11318a8704c80036860637541d4302767f33..2c14b6f0cf1eed930a79cc99b7a6a027f3d46e50 100644 (file)
@@ -19,7 +19,7 @@
 
 int extract_first_word(const char **p, char **ret, const char *separators, ExtractFlags flags) {
         _cleanup_free_ char *s = NULL;
-        size_t allocated = 0, sz = 0;
+        size_t sz = 0;
         char quote = 0;                 /* 0 or ' or " */
         bool backslash = false;         /* whether we've just seen a backslash */
         char c;
@@ -42,7 +42,7 @@ int extract_first_word(const char **p, char **ret, const char *separators, Extra
          * the pointer *p at the first invalid character. */
 
         if (flags & EXTRACT_DONT_COALESCE_SEPARATORS)
-                if (!GREEDY_REALLOC(s, allocated, sz+1))
+                if (!GREEDY_REALLOC(s, sz+1))
                         return -ENOMEM;
 
         for (;; (*p)++, c = **p) {
@@ -57,7 +57,7 @@ int extract_first_word(const char **p, char **ret, const char *separators, Extra
                         /* We found a non-blank character, so we will always
                          * want to return a string (even if it is empty),
                          * allocate it here. */
-                        if (!GREEDY_REALLOC(s, allocated, sz+1))
+                        if (!GREEDY_REALLOC(s, sz+1))
                                 return -ENOMEM;
                         break;
                 }
@@ -65,7 +65,7 @@ int extract_first_word(const char **p, char **ret, const char *separators, Extra
 
         for (;; (*p)++, c = **p) {
                 if (backslash) {
-                        if (!GREEDY_REALLOC(s, allocated, sz+7))
+                        if (!GREEDY_REALLOC(s, sz+7))
                                 return -ENOMEM;
 
                         if (c == 0) {
@@ -128,7 +128,7 @@ int extract_first_word(const char **p, char **ret, const char *separators, Extra
                                         backslash = true;
                                         break;
                                 } else {
-                                        if (!GREEDY_REALLOC(s, allocated, sz+2))
+                                        if (!GREEDY_REALLOC(s, sz+2))
                                                 return -ENOMEM;
 
                                         s[sz++] = c;
@@ -160,7 +160,7 @@ int extract_first_word(const char **p, char **ret, const char *separators, Extra
                                         goto finish;
 
                                 } else {
-                                        if (!GREEDY_REALLOC(s, allocated, sz+2))
+                                        if (!GREEDY_REALLOC(s, sz+2))
                                                 return -ENOMEM;
 
                                         s[sz++] = c;
index 4afab84ed3e22e5c4c109402d05c4b51817e4f44..0df03077d3c594f3fbc131f39d060a51b8831b46 100644 (file)
@@ -1212,8 +1212,8 @@ static EndOfLineMarker categorize_eol(char c, ReadLineFlags flags) {
 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(FILE*, funlockfile, NULL);
 
 int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret) {
-        size_t n = 0, allocated = 0, count = 0;
         _cleanup_free_ char *buffer = NULL;
+        size_t n = 0, count = 0;
         int r;
 
         assert(f);
@@ -1243,7 +1243,7 @@ int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret) {
          * If a line shall be skipped ret may be initialized as NULL. */
 
         if (ret) {
-                if (!GREEDY_REALLOC(buffer, allocated, 1))
+                if (!GREEDY_REALLOC(buffer, 1))
                         return -ENOMEM;
         }
 
@@ -1315,7 +1315,7 @@ int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret) {
                         }
 
                         if (ret) {
-                                if (!GREEDY_REALLOC(buffer, allocated, n + 2))
+                                if (!GREEDY_REALLOC(buffer, n + 2))
                                         return -ENOMEM;
 
                                 buffer[n] = c;
index 46d6f7780e18941b08487dfdd252d580afcb2e47..e8435cab2ccd26332d11d987df4f0ffa36b1d9f1 100644 (file)
@@ -549,10 +549,10 @@ int mkfifoat_atomic(int dirfd, const char *path, mode_t mode) {
 }
 
 int get_files_in_directory(const char *path, char ***list) {
+        _cleanup_strv_free_ char **l = NULL;
         _cleanup_closedir_ DIR *d = NULL;
         struct dirent *de;
-        size_t bufsize = 0, n = 0;
-        _cleanup_strv_free_ char **l = NULL;
+        size_t n = 0;
 
         assert(path);
 
@@ -572,7 +572,7 @@ int get_files_in_directory(const char *path, char ***list) {
 
                 if (list) {
                         /* one extra slot is needed for the terminating NULL */
-                        if (!GREEDY_REALLOC(l, bufsize, n + 2))
+                        if (!GREEDY_REALLOC(l, n + 2))
                                 return -ENOMEM;
 
                         l[n] = strdup(de->d_name);
index e354c67adc0ef7a1ac7961ac79ffb08f802695c2..34fed69d997e65b8f2d6e6c445d09505f45d4205 100644 (file)
@@ -230,7 +230,7 @@ struct Set {
 
 typedef struct CacheMem {
         const void **ptr;
-        size_t n_populated, n_allocated;
+        size_t n_populated;
         bool active:1;
 } CacheMem;
 
@@ -1897,10 +1897,10 @@ int set_put_strsplit(Set *s, const char *v, const char *separators, ExtractFlags
 }
 
 /* expand the cachemem if needed, return true if newly (re)activated. */
-static int cachemem_maintain(CacheMem *mem, unsigned size) {
+static int cachemem_maintain(CacheMem *mem, size_t size) {
         assert(mem);
 
-        if (!GREEDY_REALLOC(mem->ptr, mem->n_allocated, size)) {
+        if (!GREEDY_REALLOC(mem->ptr, size)) {
                 if (size > 0)
                         return -ENOMEM;
         }
@@ -1915,7 +1915,7 @@ static int cachemem_maintain(CacheMem *mem, unsigned size) {
 
 int iterated_cache_get(IteratedCache *cache, const void ***res_keys, const void ***res_values, unsigned *res_n_entries) {
         bool sync_keys = false, sync_values = false;
-        unsigned size;
+        size_t size;
         int r;
 
         assert(cache);
@@ -1988,8 +1988,8 @@ IteratedCache* iterated_cache_free(IteratedCache *cache) {
 }
 
 int set_strjoin(Set *s, const char *separator, bool wrap_with_separator, char **ret) {
-        size_t separator_len, allocated = 0, len = 0;
         _cleanup_free_ char *str = NULL;
+        size_t separator_len, len = 0;
         const char *value;
         bool first;
 
@@ -2013,7 +2013,7 @@ int set_strjoin(Set *s, const char *separator, bool wrap_with_separator, char **
                 if (l == 0)
                         continue;
 
-                if (!GREEDY_REALLOC(str, allocated, len + l + (first ? 0 : separator_len) + (wrap_with_separator ? separator_len : 0) + 1))
+                if (!GREEDY_REALLOC(str, len + l + (first ? 0 : separator_len) + (wrap_with_separator ? separator_len : 0) + 1))
                         return -ENOMEM;
 
                 if (separator_len > 0 && !first) {
index f0a66da9bfc983c24373a072157d8a64d2dbbc8f..783ca1309dede27c8f2245cc34c542f50ceeb1a4 100644 (file)
@@ -288,7 +288,6 @@ void iovw_free_contents(struct iovec_wrapper *iovw, bool free_vectors) {
 
         iovw->iovec = mfree(iovw->iovec);
         iovw->count = 0;
-        iovw->size_bytes = 0;
 }
 
 struct iovec_wrapper *iovw_free_free(struct iovec_wrapper *iovw) {
@@ -307,7 +306,7 @@ int iovw_put(struct iovec_wrapper *iovw, void *data, size_t len) {
         if (iovw->count >= IOV_MAX)
                 return -E2BIG;
 
-        if (!GREEDY_REALLOC(iovw->iovec, iovw->size_bytes, iovw->count + 1))
+        if (!GREEDY_REALLOC(iovw->iovec, iovw->count + 1))
                 return -ENOMEM;
 
         iovw->iovec[iovw->count++] = IOVEC_MAKE(data, len);
index ce54ca228bf8558e0e0b6a26ac296f0ad26d2cc4..39728e06bcfa7200fa162e4d675083dda8124881 100644 (file)
@@ -85,7 +85,6 @@ char* set_iovec_string_field_free(struct iovec *iovec, size_t *n_iovec, const ch
 struct iovec_wrapper {
         struct iovec *iovec;
         size_t count;
-        size_t size_bytes;
 };
 
 struct iovec_wrapper *iovw_new(void);
index de28daf2b17eb3bc90d0895a8d5059f53ea04071..22903cd677e2aeacb9ae32ed5c033337a68e30eb 100644 (file)
@@ -601,7 +601,7 @@ int get_process_root(pid_t pid, char **root) {
 int get_process_environ(pid_t pid, char **env) {
         _cleanup_fclose_ FILE *f = NULL;
         _cleanup_free_ char *outcome = NULL;
-        size_t allocated = 0, sz = 0;
+        size_t sz = 0;
         const char *p;
         int r;
 
@@ -622,7 +622,7 @@ int get_process_environ(pid_t pid, char **env) {
                 if (sz >= ENVIRONMENT_BLOCK_MAX)
                         return -ENOBUFS;
 
-                if (!GREEDY_REALLOC(outcome, allocated, sz + 5))
+                if (!GREEDY_REALLOC(outcome, sz + 5))
                         return -ENOMEM;
 
                 r = safe_fgetc(f, &c);
index f4b6515a4377f54f82dd446523b2624123ce7c67..fd922b117babc27ea72d29b8d186f1401bb8693b 100644 (file)
@@ -522,7 +522,7 @@ char* strshorten(char *s, size_t l) {
 }
 
 char *strreplace(const char *text, const char *old_string, const char *new_string) {
-        size_t l, old_len, new_len, allocated = 0;
+        size_t l, old_len, new_len;
         char *t, *ret = NULL;
         const char *f;
 
@@ -536,7 +536,7 @@ char *strreplace(const char *text, const char *old_string, const char *new_strin
         new_len = strlen(new_string);
 
         l = strlen(text);
-        if (!GREEDY_REALLOC(ret, allocated, l+1))
+        if (!GREEDY_REALLOC(ret, l+1))
                 return NULL;
 
         f = text;
@@ -552,7 +552,7 @@ char *strreplace(const char *text, const char *old_string, const char *new_strin
                 d = t - ret;
                 nl = l - old_len + new_len;
 
-                if (!GREEDY_REALLOC(ret, allocated, nl + 1))
+                if (!GREEDY_REALLOC(ret, nl + 1))
                         return mfree(ret);
 
                 l = nl;
index 765da04a7bd972cc040779c27c07c25c16e71181..3adf3c5c7e0043d8f7b4610c5c2f917e450c14e9 100644 (file)
@@ -266,7 +266,7 @@ int strv_split_newlines_full(char ***ret, const char *s, ExtractFlags flags) {
 
 int strv_split_full(char ***t, const char *s, const char *separators, ExtractFlags flags) {
         _cleanup_strv_free_ char **l = NULL;
-        size_t n = 0, allocated = 0;
+        size_t n = 0;
         int r;
 
         assert(t);
@@ -281,7 +281,7 @@ int strv_split_full(char ***t, const char *s, const char *separators, ExtractFla
                 if (r == 0)
                         break;
 
-                if (!GREEDY_REALLOC(l, allocated, n + 2))
+                if (!GREEDY_REALLOC(l, n + 2))
                         return -ENOMEM;
 
                 l[n++] = TAKE_PTR(word);
@@ -302,7 +302,7 @@ int strv_split_full(char ***t, const char *s, const char *separators, ExtractFla
 
 int strv_split_colon_pairs(char ***t, const char *s) {
         _cleanup_strv_free_ char **l = NULL;
-        size_t n = 0, allocated = 0;
+        size_t n = 0;
         int r;
 
         assert(t);
@@ -332,7 +332,7 @@ int strv_split_colon_pairs(char ***t, const char *s) {
                 if (!second_or_empty)
                         return -ENOMEM;
 
-                if (!GREEDY_REALLOC(l, allocated, n + 3))
+                if (!GREEDY_REALLOC(l, n + 3))
                         return -ENOMEM;
 
                 l[n++] = TAKE_PTR(first);
@@ -708,9 +708,9 @@ int strv_make_nulstr(char * const *l, char **ret, size_t *ret_size) {
          * is provided separately.
          */
 
-        size_t n_allocated = 0, n = 0;
         _cleanup_free_ char *m = NULL;
         char * const *i;
+        size_t n = 0;
 
         assert(ret);
         assert(ret_size);
@@ -720,7 +720,7 @@ int strv_make_nulstr(char * const *l, char **ret, size_t *ret_size) {
 
                 z = strlen(*i);
 
-                if (!GREEDY_REALLOC(m, n_allocated, n + z + 2))
+                if (!GREEDY_REALLOC(m, n + z + 2))
                         return -ENOMEM;
 
                 memcpy(m + n, *i, z + 1);
index b6087c455030c51e30dd66e20a70469f94d08d6f..da3f0255831aaaeb1a7cd23e563df8199fb1e8d5 100644 (file)
@@ -1247,7 +1247,7 @@ int parse_nsec(const char *t, nsec_t *nsec) {
 int get_timezones(char ***ret) {
         _cleanup_fclose_ FILE *f = NULL;
         _cleanup_strv_free_ char **zones = NULL;
-        size_t n_zones = 0, n_allocated = 0;
+        size_t n_zones = 0;
         int r;
 
         assert(ret);
@@ -1256,7 +1256,6 @@ int get_timezones(char ***ret) {
         if (!zones)
                 return -ENOMEM;
 
-        n_allocated = 2;
         n_zones = 1;
 
         f = fopen("/usr/share/zoneinfo/zone1970.tab", "re");
@@ -1294,7 +1293,7 @@ int get_timezones(char ***ret) {
                         if (!w)
                                 return -ENOMEM;
 
-                        if (!GREEDY_REALLOC(zones, n_allocated, n_zones + 2))
+                        if (!GREEDY_REALLOC(zones, n_zones + 2))
                                 return -ENOMEM;
 
                         zones[n_zones++] = TAKE_PTR(w);
index f081e98ae02e5589df817c3f4f8fdbddaf10a188..8aa769e0f9232ca1b0bf0449b9169648d76feeaa 100644 (file)
@@ -1644,8 +1644,8 @@ static int message_append_cmdline(sd_bus_message *m, const char *signature, char
 static int json_transform_one(sd_bus_message *m, JsonVariant **ret);
 
 static int json_transform_array_or_struct(sd_bus_message *m, JsonVariant **ret) {
-        size_t n_elements = 0, n_allocated = 0;
         JsonVariant **elements = NULL;
+        size_t n_elements = 0;
         int r;
 
         assert(m);
@@ -1660,7 +1660,7 @@ static int json_transform_array_or_struct(sd_bus_message *m, JsonVariant **ret)
                 if (r > 0)
                         break;
 
-                if (!GREEDY_REALLOC(elements, n_allocated, n_elements + 1)) {
+                if (!GREEDY_REALLOC(elements, n_elements + 1)) {
                         r = log_oom();
                         goto finish;
                 }
@@ -1702,8 +1702,8 @@ static int json_transform_variant(sd_bus_message *m, const char *contents, JsonV
 }
 
 static int json_transform_dict_array(sd_bus_message *m, JsonVariant **ret) {
-        size_t n_elements = 0, n_allocated = 0;
         JsonVariant **elements = NULL;
+        size_t n_elements = 0;
         int r;
 
         assert(m);
@@ -1727,7 +1727,7 @@ static int json_transform_dict_array(sd_bus_message *m, JsonVariant **ret) {
 
                 assert(type == 'e');
 
-                if (!GREEDY_REALLOC(elements, n_allocated, n_elements + 2)) {
+                if (!GREEDY_REALLOC(elements, n_elements + 2)) {
                         r = log_oom();
                         goto finish;
                 }
index 6a1e6c842974b2b8fc7414d7d007c518e002bae5..ebd1418c8c973946b13375e25f1b607eb6af3667 100644 (file)
@@ -1967,7 +1967,7 @@ static int build_environment(
 
 static int build_pass_environment(const ExecContext *c, char ***ret) {
         _cleanup_strv_free_ char **pass_env = NULL;
-        size_t n_env = 0, n_bufsize = 0;
+        size_t n_env = 0;
         char **i;
 
         STRV_FOREACH(i, c->pass_environment) {
@@ -1981,7 +1981,7 @@ static int build_pass_environment(const ExecContext *c, char ***ret) {
                 if (!x)
                         return -ENOMEM;
 
-                if (!GREEDY_REALLOC(pass_env, n_bufsize, n_env + 2))
+                if (!GREEDY_REALLOC(pass_env, n_env + 2))
                         return -ENOMEM;
 
                 pass_env[n_env++] = TAKE_PTR(x);
index 57829d185a059012f18e947caf86f3302aac12c2..c626ddca91d5568422666fde7386844ad972695c 100644 (file)
@@ -1498,8 +1498,8 @@ static size_t sort_job_list(Job **list, size_t n) {
 
 int job_get_before(Job *j, Job*** ret) {
         _cleanup_free_ Job** list = NULL;
-        size_t n = 0, n_allocated = 0;
         Unit *other = NULL;
+        size_t n = 0;
         void *v;
 
         /* Returns a list of all pending jobs that need to finish before this job may be started. */
@@ -1518,7 +1518,7 @@ int job_get_before(Job *j, Job*** ret) {
                 if (job_compare(j, other->job, UNIT_AFTER) <= 0)
                         continue;
 
-                if (!GREEDY_REALLOC(list, n_allocated, n+1))
+                if (!GREEDY_REALLOC(list, n+1))
                         return -ENOMEM;
                 list[n++] = other->job;
         }
@@ -1529,7 +1529,7 @@ int job_get_before(Job *j, Job*** ret) {
                 if (job_compare(j, other->job, UNIT_BEFORE) <= 0)
                         continue;
 
-                if (!GREEDY_REALLOC(list, n_allocated, n+1))
+                if (!GREEDY_REALLOC(list, n+1))
                         return -ENOMEM;
                 list[n++] = other->job;
         }
@@ -1543,8 +1543,8 @@ int job_get_before(Job *j, Job*** ret) {
 
 int job_get_after(Job *j, Job*** ret) {
         _cleanup_free_ Job** list = NULL;
-        size_t n = 0, n_allocated = 0;
         Unit *other = NULL;
+        size_t n = 0;
         void *v;
 
         assert(j);
@@ -1562,7 +1562,7 @@ int job_get_after(Job *j, Job*** ret) {
                 if (job_compare(j, other->job, UNIT_BEFORE) >= 0)
                         continue;
 
-                if (!GREEDY_REALLOC(list, n_allocated, n+1))
+                if (!GREEDY_REALLOC(list, n+1))
                         return -ENOMEM;
                 list[n++] = other->job;
         }
@@ -1577,7 +1577,7 @@ int job_get_after(Job *j, Job*** ret) {
                 if (job_compare(j, other->job, UNIT_AFTER) >= 0)
                         continue;
 
-                if (!GREEDY_REALLOC(list, n_allocated, n+1))
+                if (!GREEDY_REALLOC(list, n+1))
                         return -ENOMEM;
                 list[n++] = other->job;
         }
index 3dd1e869eb6f519f953480ce35bb3666f2097914..cd373145426726027e1e79c25637c3494b316280 100644 (file)
@@ -712,7 +712,7 @@ int config_parse_exec(
                 bool ignore = false, separate_argv0 = false;
                 _cleanup_free_ ExecCommand *nce = NULL;
                 _cleanup_strv_free_ char **n = NULL;
-                size_t nlen = 0, nbufsize = 0;
+                size_t nlen = 0;
                 const char *f;
 
                 semicolon = false;
@@ -799,7 +799,7 @@ int config_parse_exec(
                 if (!separate_argv0) {
                         char *w = NULL;
 
-                        if (!GREEDY_REALLOC(n, nbufsize, nlen + 2))
+                        if (!GREEDY_REALLOC(n, nlen + 2))
                                 return log_oom();
 
                         w = strdup(path);
@@ -831,7 +831,7 @@ int config_parse_exec(
                                 p += 2;
                                 p += strspn(p, WHITESPACE);
 
-                                if (!GREEDY_REALLOC(n, nbufsize, nlen + 2))
+                                if (!GREEDY_REALLOC(n, nlen + 2))
                                         return log_oom();
 
                                 w = strdup(";");
@@ -856,7 +856,7 @@ int config_parse_exec(
                                 return ignore ? 0 : -ENOEXEC;
                         }
 
-                        if (!GREEDY_REALLOC(n, nbufsize, nlen + 2))
+                        if (!GREEDY_REALLOC(n, nlen + 2))
                                 return log_oom();
 
                         n[nlen++] = TAKE_PTR(resolved);
@@ -2691,9 +2691,9 @@ int config_parse_pass_environ(
                 void *userdata) {
 
         _cleanup_strv_free_ char **n = NULL;
-        size_t nlen = 0, nbufsize = 0;
-        char*** passenv = data;
         const Unit *u = userdata;
+        char*** passenv = data;
+        size_t nlen = 0;
         int r;
 
         assert(filename);
@@ -2737,7 +2737,7 @@ int config_parse_pass_environ(
                         continue;
                 }
 
-                if (!GREEDY_REALLOC(n, nbufsize, nlen + 2))
+                if (!GREEDY_REALLOC(n, nlen + 2))
                         return log_oom();
 
                 n[nlen++] = TAKE_PTR(k);
@@ -2766,9 +2766,9 @@ int config_parse_unset_environ(
                 void *userdata) {
 
         _cleanup_strv_free_ char **n = NULL;
-        size_t nlen = 0, nbufsize = 0;
         char*** unsetenv = data;
         const Unit *u = userdata;
+        size_t nlen = 0;
         int r;
 
         assert(filename);
@@ -2812,7 +2812,7 @@ int config_parse_unset_environ(
                         continue;
                 }
 
-                if (!GREEDY_REALLOC(n, nbufsize, nlen + 2))
+                if (!GREEDY_REALLOC(n, nlen + 2))
                         return log_oom();
 
                 n[nlen++] = TAKE_PTR(k);
index 085715b65a601b575cbc0a57164542209bac06c4..c34030644af731f20aab8ed704af07d9f85727f1 100644 (file)
@@ -2566,10 +2566,10 @@ static unsigned service_exec_command_index(Unit *u, ServiceExecCommand id, ExecC
 
 static int service_serialize_exec_command(Unit *u, FILE *f, ExecCommand *command) {
         _cleanup_free_ char *args = NULL, *p = NULL;
-        size_t allocated = 0, length = 0;
         Service *s = SERVICE(u);
         const char *type, *key;
         ServiceExecCommand id;
+        size_t length = 0;
         unsigned idx;
         char **arg;
 
@@ -2598,7 +2598,7 @@ static int service_serialize_exec_command(Unit *u, FILE *f, ExecCommand *command
                         return log_oom();
 
                 n = strlen(e);
-                if (!GREEDY_REALLOC(args, allocated, length + 2 + n + 2))
+                if (!GREEDY_REALLOC(args, length + 2 + n + 2))
                         return log_oom();
 
                 if (length > 0)
@@ -2610,7 +2610,7 @@ static int service_serialize_exec_command(Unit *u, FILE *f, ExecCommand *command
                 args[length++] = '"';
         }
 
-        if (!GREEDY_REALLOC(args, allocated, length + 1))
+        if (!GREEDY_REALLOC(args, length + 1))
                 return log_oom();
 
         args[length++] = 0;
index 8cc1696a4fa47b754f83e207bcdf3251a79fca03..58ad085dcd6f0cc624c6831bd4507552d0f102e4 100644 (file)
@@ -247,7 +247,7 @@ static int write_onlycap_list(void) {
         _cleanup_close_ int onlycap_fd = -1;
         _cleanup_free_ char *list = NULL;
         _cleanup_fclose_ FILE *f = NULL;
-        size_t len = 0, allocated = 0;
+        size_t len = 0;
         int r;
 
         f = fopen("/etc/smack/onlycap", "re");
@@ -272,7 +272,7 @@ static int write_onlycap_list(void) {
                         continue;
 
                 l = strlen(buf);
-                if (!GREEDY_REALLOC(list, allocated, len + l + 1))
+                if (!GREEDY_REALLOC(list, len + l + 1))
                         return log_oom();
 
                 stpcpy(list + len, buf)[0] = ' ';
index 1d3cf8f1fc61c4eafb5bb2bb7045b95c181c2715..4a55c075b4f1644101571ea131a8c7d321088faa 100644 (file)
@@ -330,11 +330,11 @@ _pure_ static bool unit_matters_to_anchor(Unit *u, Job *j) {
 
 static char* merge_unit_ids(const char* unit_log_field, char **pairs) {
         char **unit_id, **job_type, *ans = NULL;
-        size_t alloc = 0, size = 0, next;
+        size_t size = 0, next;
 
         STRV_FOREACH_PAIR(unit_id, job_type, pairs) {
                 next = strlen(unit_log_field) + strlen(*unit_id);
-                if (!GREEDY_REALLOC(ans, alloc, size + next + 1))
+                if (!GREEDY_REALLOC(ans, size + next + 1))
                         return mfree(ans);
 
                 sprintf(ans + size, "%s%s", unit_log_field, *unit_id);
index 67d811d7dd9d8dbaf610b96fcf8703e8d24966ef..fc63f3bee5fa36c743fd10f2bb5b63a998e2d3dc 100644 (file)
@@ -4004,7 +4004,7 @@ char* unit_escape_setting(const char *s, UnitWriteFlags flags, char **buf) {
 
 char* unit_concat_strv(char **l, UnitWriteFlags flags) {
         _cleanup_free_ char *result = NULL;
-        size_t n = 0, allocated = 0;
+        size_t n = 0;
         char **i;
 
         /* Takes a list of strings, escapes them, and concatenates them. This may be used to format command lines in a
@@ -4021,7 +4021,7 @@ char* unit_concat_strv(char **l, UnitWriteFlags flags) {
                         return NULL;
 
                 a = (n > 0) + 1 + strlen(p) + 1; /* separating space + " + entry + " */
-                if (!GREEDY_REALLOC(result, allocated, n + a + 1))
+                if (!GREEDY_REALLOC(result, n + a + 1))
                         return NULL;
 
                 q = result + n;
@@ -4035,7 +4035,7 @@ char* unit_concat_strv(char **l, UnitWriteFlags flags) {
                 n += a;
         }
 
-        if (!GREEDY_REALLOC(result, allocated, n + 1))
+        if (!GREEDY_REALLOC(result, n + 1))
                 return NULL;
 
         result[n] = 0;
index d56deaa6b10b0559d282a2d13ce58c6629a7ba2b..c3ad81943030c52fdcbc3b41eb15e0aa8f9a741f 100644 (file)
@@ -11,8 +11,8 @@ int list_enrolled(struct crypt_device *cd) {
                 int slot;
                 const char *type;
         } *keyslot_metadata = NULL;
-        size_t n_keyslot_metadata = 0, n_keyslot_metadata_allocated = 0;
         _cleanup_(table_unrefp) Table *t = NULL;
+        size_t n_keyslot_metadata = 0;
         int slot_max, r;
         TableCell *cell;
 
@@ -27,7 +27,7 @@ int list_enrolled(struct crypt_device *cd) {
                 if (!IN_SET(status, CRYPT_SLOT_ACTIVE, CRYPT_SLOT_ACTIVE_LAST))
                         continue;
 
-                if (!GREEDY_REALLOC(keyslot_metadata, n_keyslot_metadata_allocated, n_keyslot_metadata+1))
+                if (!GREEDY_REALLOC(keyslot_metadata, n_keyslot_metadata+1))
                         return log_oom();
 
                 keyslot_metadata[n_keyslot_metadata++] = (struct keyslot_metadata) {
index bb38db54a4ec25bf09f1cc5f7108a53b3a2b6ab7..8c702459c1ea5583f91cf42883dc1fa52ecb41b7 100644 (file)
@@ -291,7 +291,7 @@ static int enumerate_dir(
         _cleanup_closedir_ DIR *d = NULL;
         struct dirent *de;
         _cleanup_strv_free_ char **files = NULL, **dirs = NULL;
-        size_t n_files = 0, allocated_files = 0, n_dirs = 0, allocated_dirs = 0;
+        size_t n_files = 0, n_dirs = 0;
         char **t;
         int r;
 
@@ -314,7 +314,7 @@ static int enumerate_dir(
                 dirent_ensure_type(d, de);
 
                 if (dropins && de->d_type == DT_DIR && endswith(de->d_name, ".d")) {
-                        if (!GREEDY_REALLOC0(dirs, allocated_dirs, n_dirs + 2))
+                        if (!GREEDY_REALLOC0(dirs, n_dirs + 2))
                                 return -ENOMEM;
 
                         dirs[n_dirs] = strdup(de->d_name);
@@ -326,7 +326,7 @@ static int enumerate_dir(
                 if (!dirent_is_file(de))
                         continue;
 
-                if (!GREEDY_REALLOC0(files, allocated_files, n_files + 2))
+                if (!GREEDY_REALLOC0(files, n_files + 2))
                         return -ENOMEM;
 
                 files[n_files] = strdup(de->d_name);
index f94fd0673e4f4300d6360be464ba654be276032e..189bd210b56a5e312a183f28591a3515f718dcd7 100644 (file)
@@ -73,7 +73,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
 
         size_t sw_len = MIN(data_len - 1, h->sw_len);
 
-        r = decompress_startswith(alg, buf, csize, &buf2, &sw_alloc, h->data, sw_len, h->data[sw_len]);
+        r = decompress_startswith(alg, buf, csize, &buf2, h->data, sw_len, h->data[sw_len]);
         assert_se(r > 0);
 
         return 0;
index 33512cd77a8adfb1d1fef0a9622a53e27208e666..9ab3776e506a1d9383593d31a8ae721937b961d9 100644 (file)
@@ -75,7 +75,6 @@ static int pull_job_restart(PullJob *j, const char *new_url) {
         j->error = 0;
         j->payload = mfree(j->payload);
         j->payload_size = 0;
-        j->payload_allocated = 0;
         j->written_compressed = 0;
         j->written_uncompressed = 0;
         j->content_length = UINT64_MAX;
@@ -266,7 +265,7 @@ static int pull_job_write_uncompressed(const void *p, size_t sz, void *userdata)
                         return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short write");
         } else {
 
-                if (!GREEDY_REALLOC(j->payload, j->payload_allocated, j->payload_size + sz))
+                if (!GREEDY_REALLOC(j->payload, j->payload_size + sz))
                         return log_oom();
 
                 memcpy(j->payload + j->payload_size, p, sz);
@@ -371,7 +370,6 @@ static int pull_job_detect_compression(PullJob *j) {
 
         j->payload = NULL;
         j->payload_size = 0;
-        j->payload_allocated = 0;
 
         j->state = PULL_JOB_RUNNING;
 
@@ -395,7 +393,7 @@ static size_t pull_job_write_callback(void *contents, size_t size, size_t nmemb,
         case PULL_JOB_ANALYZING:
                 /* Let's first check what it actually is */
 
-                if (!GREEDY_REALLOC(j->payload, j->payload_allocated, j->payload_size + sz)) {
+                if (!GREEDY_REALLOC(j->payload, j->payload_size + sz)) {
                         r = log_oom();
                         goto fail;
                 }
index 8e416d51badc4bf8ec0a863c45f03c55494f612b..023ccc8302fd13c97e62d651affe76a85094697b 100644 (file)
@@ -57,7 +57,6 @@ struct PullJob {
 
         uint8_t *payload;
         size_t payload_size;
-        size_t payload_allocated;
 
         int disk_fd;
 
index 13461dbe41d7e783994104c629cbc7711ed38866..6e5aebdc485704f110b64058055f0779dfa7f496 100644 (file)
@@ -171,7 +171,7 @@ static int get_source_for_fd(RemoteServer *s,
         assert(fd >= 0);
         assert(source);
 
-        if (!GREEDY_REALLOC0(s->sources, s->sources_size, fd + 1))
+        if (!GREEDY_REALLOC0(s->sources, fd + 1))
                 return log_oom();
 
         r = journal_remote_get_writer(s, name, &writer);
@@ -197,7 +197,7 @@ static int remove_source(RemoteServer *s, int fd) {
         RemoteSource *source;
 
         assert(s);
-        assert(fd >= 0 && fd < (ssize_t) s->sources_size);
+        assert(fd >= 0 && fd < (ssize_t) MALLOC_ELEMENTSOF(s->sources));
 
         source = s->sources[fd];
         if (source) {
@@ -352,8 +352,7 @@ void journal_remote_server_destroy(RemoteServer *s) {
         hashmap_free_with_destructor(s->daemons, MHDDaemonWrapper_free);
 #endif
 
-        assert(s->sources_size == 0 || s->sources);
-        for (i = 0; i < s->sources_size; i++)
+        for (i = 0; i < MALLOC_ELEMENTSOF(s->sources); i++)
                 remove_source(s, i);
         free(s->sources);
 
@@ -388,7 +387,7 @@ int journal_remote_handle_raw_source(
          * 0 if data is currently exhausted, negative on error.
          */
 
-        assert(fd >= 0 && fd < (ssize_t) s->sources_size);
+        assert(fd >= 0 && fd < (ssize_t) MALLOC_ELEMENTSOF(s->sources));
         source = s->sources[fd];
         assert(source->importer.fd == fd);
 
index 247ffa9765cb0b5ae80b14f7138c187b4414742a..45176e964da6337e1134f5993ee365f8e1e59a4a 100644 (file)
@@ -23,7 +23,6 @@ struct MHDDaemonWrapper {
 
 struct RemoteServer {
         RemoteSource **sources;
-        size_t sources_size;
         size_t active;
 
         sd_event *events;
index e2f73ffa7e564ea4f1f9c750d2d0f6a810062c3a..ff1746e1221ca45c0f58e30f152434fc9a53f952 100644 (file)
@@ -22,7 +22,6 @@ void dummy_server_init(Server *s, const uint8_t *buffer, size_t size) {
         if (buffer) {
                 s->buffer = memdup_suffix0(buffer, size);
                 assert_se(s->buffer);
-                s->buffer_size = size + 1;
         }
 }
 
index 744f750ca34ed9bf70fb83afc2e11185f93688c3..507b12137db70001d8d95502911d6e80697157e6 100644 (file)
@@ -1,5 +1,7 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
+#include <malloc.h>
+
 #include "alloc-util.h"
 #include "audit-type.h"
 #include "errno-util.h"
 typedef struct MapField {
         const char *audit_field;
         const char *journal_field;
-        int (*map)(const char *field, const char **p, struct iovec **iov, size_t *n_iov_allocated, size_t *n_iov);
+        int (*map)(const char *field, const char **p, struct iovec **iov, size_t *n_iov);
 } MapField;
 
-static int map_simple_field(const char *field, const char **p, struct iovec **iov, size_t *n_iov_allocated, size_t *n_iov) {
+static int map_simple_field(
+                const char *field,
+                const char **p,
+                struct iovec **iov,
+                size_t *n_iov) {
+
         _cleanup_free_ char *c = NULL;
-        size_t l = 0, allocated = 0;
+        size_t l = 0;
         const char *e;
 
         assert(field);
@@ -27,14 +34,13 @@ static int map_simple_field(const char *field, const char **p, struct iovec **io
         assert(n_iov);
 
         l = strlen(field);
-        allocated = l + 1;
-        c = malloc(allocated);
+        c = malloc(l + 1);
         if (!c)
                 return -ENOMEM;
 
         memcpy(c, field, l);
         for (e = *p; !IN_SET(*e, 0, ' '); e++) {
-                if (!GREEDY_REALLOC(c, allocated, l+2))
+                if (!GREEDY_REALLOC(c, l+2))
                         return -ENOMEM;
 
                 c[l++] = *e;
@@ -42,7 +48,7 @@ static int map_simple_field(const char *field, const char **p, struct iovec **io
 
         c[l] = 0;
 
-        if (!GREEDY_REALLOC(*iov, *n_iov_allocated, *n_iov + 1))
+        if (!GREEDY_REALLOC(*iov, *n_iov + 1))
                 return -ENOMEM;
 
         (*iov)[(*n_iov)++] = IOVEC_MAKE(c, l);
@@ -53,7 +59,13 @@ static int map_simple_field(const char *field, const char **p, struct iovec **io
         return 1;
 }
 
-static int map_string_field_internal(const char *field, const char **p, struct iovec **iov, size_t *n_iov_allocated, size_t *n_iov, bool filter_printable) {
+static int map_string_field_internal(
+                const char *field,
+                const char **p,
+                struct iovec **iov,
+                size_t *n_iov,
+                bool filter_printable) {
+
         _cleanup_free_ char *c = NULL;
         const char *s, *e;
         size_t l;
@@ -83,11 +95,8 @@ static int map_string_field_internal(const char *field, const char **p, struct i
 
         } else if (unhexchar(**p) >= 0) {
                 /* Hexadecimal escaping */
-                size_t allocated = 0;
-
                 l = strlen(field);
-                allocated = l + 2;
-                c = malloc(allocated);
+                c = malloc(l + 2);
                 if (!c)
                         return -ENOMEM;
 
@@ -109,7 +118,7 @@ static int map_string_field_internal(const char *field, const char **p, struct i
                         if (filter_printable && x < (uint8_t) ' ')
                                 x = (uint8_t) ' ';
 
-                        if (!GREEDY_REALLOC(c, allocated, l+2))
+                        if (!GREEDY_REALLOC(c, l+2))
                                 return -ENOMEM;
 
                         c[l++] = (char) x;
@@ -119,7 +128,7 @@ static int map_string_field_internal(const char *field, const char **p, struct i
         } else
                 return 0;
 
-        if (!GREEDY_REALLOC(*iov, *n_iov_allocated, *n_iov + 1))
+        if (!GREEDY_REALLOC(*iov, *n_iov + 1))
                 return -ENOMEM;
 
         (*iov)[(*n_iov)++] = IOVEC_MAKE(c, l);
@@ -130,15 +139,20 @@ static int map_string_field_internal(const char *field, const char **p, struct i
         return 1;
 }
 
-static int map_string_field(const char *field, const char **p, struct iovec **iov, size_t *n_iov_allocated, size_t *n_iov) {
-        return map_string_field_internal(field, p, iov, n_iov_allocated, n_iov, false);
+static int map_string_field(const char *field, const char **p, struct iovec **iov, size_t *n_iov) {
+        return map_string_field_internal(field, p, iov, n_iov, false);
 }
 
-static int map_string_field_printable(const char *field, const char **p, struct iovec **iov, size_t *n_iov_allocated, size_t *n_iov) {
-        return map_string_field_internal(field, p, iov, n_iov_allocated, n_iov, true);
+static int map_string_field_printable(const char *field, const char **p, struct iovec **iov, size_t *n_iov) {
+        return map_string_field_internal(field, p, iov, n_iov, true);
 }
 
-static int map_generic_field(const char *prefix, const char **p, struct iovec **iov, size_t *n_iov_allocated, size_t *n_iov) {
+static int map_generic_field(
+                const char *prefix,
+                const char **p,
+                struct iovec **iov,
+                size_t *n_iov) {
+
         const char *e, *f;
         char *c, *t;
         int r;
@@ -182,7 +196,7 @@ static int map_generic_field(const char *prefix, const char **p, struct iovec **
 
         e++;
 
-        r = map_simple_field(c, &e, iov, n_iov_allocated, n_iov);
+        r = map_simple_field(c, &e, iov, n_iov);
         if (r < 0)
                 return r;
 
@@ -242,14 +256,12 @@ static int map_all_fields(
                 const char *prefix,
                 bool handle_msg,
                 struct iovec **iov,
-                size_t *n_iov_allocated,
                 size_t *n_iov) {
 
         int r;
 
         assert(p);
         assert(iov);
-        assert(n_iov_allocated);
         assert(n_iov);
 
         for (;;) {
@@ -284,7 +296,7 @@ static int map_all_fields(
                                 if (!c)
                                         return -ENOMEM;
 
-                                return map_all_fields(c, map_fields_userspace, "AUDIT_FIELD_", false, iov, n_iov_allocated, n_iov);
+                                return map_all_fields(c, map_fields_userspace, "AUDIT_FIELD_", false, iov, n_iov);
                         }
                 }
 
@@ -294,7 +306,7 @@ static int map_all_fields(
                         if (!v)
                                 continue;
 
-                        r = m->map(m->journal_field, &v, iov, n_iov_allocated, n_iov);
+                        r = m->map(m->journal_field, &v, iov, n_iov);
                         if (r < 0)
                                 return log_debug_errno(r, "Failed to parse audit array: %m");
 
@@ -306,7 +318,7 @@ static int map_all_fields(
                 }
 
                 if (!mapped) {
-                        r = map_generic_field(prefix, &p, iov, n_iov_allocated, n_iov);
+                        r = map_generic_field(prefix, &p, iov, n_iov);
                         if (r < 0)
                                 return log_debug_errno(r, "Failed to parse audit array: %m");
 
@@ -318,7 +330,7 @@ static int map_all_fields(
 }
 
 void process_audit_string(Server *s, int type, const char *data, size_t size) {
-        size_t n_iov_allocated = 0, n_iov = 0, z;
+        size_t n_iov = 0, z;
         _cleanup_free_ struct iovec *iov = NULL;
         uint64_t seconds, msec, id;
         const char *p, *type_name;
@@ -359,8 +371,7 @@ void process_audit_string(Server *s, int type, const char *data, size_t size) {
         if (isempty(p))
                 return;
 
-        n_iov_allocated = N_IOVEC_META_FIELDS + 8;
-        iov = new(struct iovec, n_iov_allocated);
+        iov = new(struct iovec, N_IOVEC_META_FIELDS + 8);
         if (!iov) {
                 log_oom();
                 return;
@@ -392,14 +403,14 @@ void process_audit_string(Server *s, int type, const char *data, size_t size) {
 
         z = n_iov;
 
-        map_all_fields(p, map_fields_kernel, "_AUDIT_FIELD_", true, &iov, &n_iov_allocated, &n_iov);
+        map_all_fields(p, map_fields_kernel, "_AUDIT_FIELD_", true, &iov, &n_iov);
 
-        if (!GREEDY_REALLOC(iov, n_iov_allocated, n_iov + N_IOVEC_META_FIELDS)) {
+        if (!GREEDY_REALLOC(iov, n_iov + N_IOVEC_META_FIELDS)) {
                 log_oom();
                 goto finish;
         }
 
-        server_dispatch_message(s, iov, n_iov, n_iov_allocated, NULL, NULL, LOG_NOTICE, 0);
+        server_dispatch_message(s, iov, n_iov, MALLOC_ELEMENTSOF(iov), NULL, NULL, LOG_NOTICE, 0);
 
 finish:
         /* free() all entries that map_all_fields() added. All others
index 694056d5589f4a43c0df3566d62a8b051c88f3be..27608ff089888357c03e118f6463efee158fce6f 100644 (file)
@@ -378,8 +378,8 @@ static int client_context_read_extra_fields(
                 Server *s,
                 ClientContext *c) {
 
-        size_t size = 0, n_iovec = 0, n_allocated = 0, left;
         _cleanup_free_ struct iovec *iovec = NULL;
+        size_t size = 0, n_iovec = 0, left;
         _cleanup_free_ void *data = NULL;
         _cleanup_fclose_ FILE *f = NULL;
         struct stat st;
@@ -445,7 +445,7 @@ static int client_context_read_extra_fields(
                 if (!journal_field_valid((const char *) field, eq - field, false))
                         return -EBADMSG;
 
-                if (!GREEDY_REALLOC(iovec, n_allocated, n_iovec+1))
+                if (!GREEDY_REALLOC(iovec, n_iovec+1))
                         return -ENOMEM;
 
                 iovec[n_iovec++] = IOVEC_MAKE(field, v);
index 102814a403da8057654207b46a6f4038d0fc7ab1..032578822d13cb9c799fd875ec82cddc1d0b7a1e 100644 (file)
@@ -105,7 +105,7 @@ static int server_process_entry(
          *
          * Note that *remaining is altered on both success and failure. */
 
-        size_t n = 0, j, tn = SIZE_MAX, m = 0, entry_size = 0;
+        size_t n = 0, j, tn = SIZE_MAX, entry_size = 0;
         char *identifier = NULL, *message = NULL;
         struct iovec *iovec = NULL;
         int priority = LOG_INFO;
@@ -146,7 +146,7 @@ static int server_process_entry(
                 }
 
                 /* n existing properties, 1 new, +1 for _TRANSPORT */
-                if (!GREEDY_REALLOC(iovec, m,
+                if (!GREEDY_REALLOC(iovec,
                                     n + 2 +
                                     N_IOVEC_META_FIELDS + N_IOVEC_OBJECT_FIELDS +
                                     client_context_extra_fields_n_iovec(context))) {
@@ -273,7 +273,7 @@ static int server_process_entry(
                         server_forward_wall(s, priority, identifier, message, ucred);
         }
 
-        server_dispatch_message(s, iovec, n, m, context, tv, priority, object_pid);
+        server_dispatch_message(s, iovec, n, MALLOC_ELEMENTSOF(iovec), context, tv, priority, object_pid);
 
 finish:
         for (j = 0; j < n; j++)  {
index 6d87fa0f844bf16b8fe4b9cc3b40a35854259465..2907e26b77d63fefc7552e296640b1a4cce4cd45 100644 (file)
@@ -1254,12 +1254,12 @@ int server_process_datagram(
                 uint32_t revents,
                 void *userdata) {
 
+        size_t label_len = 0, m;
         Server *s = userdata;
         struct ucred *ucred = NULL;
         struct timeval *tv = NULL;
         struct cmsghdr *cmsg;
         char *label = NULL;
-        size_t label_len = 0, m;
         struct iovec iovec;
         ssize_t n;
         int *fds = NULL, v = 0;
@@ -1302,10 +1302,10 @@ int server_process_datagram(
                             (size_t) LINE_MAX,
                             ALIGN(sizeof(struct nlmsghdr)) + ALIGN((size_t) MAX_AUDIT_MESSAGE_LENGTH)) + 1);
 
-        if (!GREEDY_REALLOC(s->buffer, s->buffer_size, m))
+        if (!GREEDY_REALLOC(s->buffer, m))
                 return log_oom();
 
-        iovec = IOVEC_MAKE(s->buffer, s->buffer_size - 1); /* Leave room for trailing NUL we add later */
+        iovec = IOVEC_MAKE(s->buffer, MALLOC_ELEMENTSOF(s->buffer) - 1); /* Leave room for trailing NUL we add later */
 
         n = recvmsg_safe(fd, &msghdr, MSG_DONTWAIT|MSG_CMSG_CLOEXEC);
         if (IN_SET(n, -EINTR, -EAGAIN))
index 09f80475a7cebb0819208df48b3ddfe42f09aa96..53e75ff3b0b3f5ec8f54295bfb8006b470494068 100644 (file)
@@ -95,7 +95,6 @@ struct Server {
         uint64_t seqnum;
 
         char *buffer;
-        size_t buffer_size;
 
         JournalRateLimit *ratelimit;
         usec_t sync_interval_usec;
index 85723f5b30fcc0de9fbb2e4e073e3eaf55c3a871..c6720b6b13950813d1a46a7011ded4a2d5e4ffba 100644 (file)
@@ -90,7 +90,6 @@ struct StdoutStream {
 
         char *buffer;
         size_t length;
-        size_t allocated;
 
         sd_event_source *event_source;
 
@@ -552,8 +551,8 @@ static int stdout_stream_scan(
 
 static int stdout_stream_process(sd_event_source *es, int fd, uint32_t revents, void *userdata) {
         CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred))) control;
+        size_t limit, consumed, allocated;
         StdoutStream *s = userdata;
-        size_t limit, consumed;
         struct ucred *ucred;
         struct iovec iovec;
         ssize_t l;
@@ -575,16 +574,19 @@ static int stdout_stream_process(sd_event_source *es, int fd, uint32_t revents,
         }
 
         /* If the buffer is almost full, add room for another 1K */
-        if (s->length + 512 >= s->allocated) {
-                if (!GREEDY_REALLOC(s->buffer, s->allocated, s->length + 1 + 1024)) {
+        allocated = MALLOC_ELEMENTSOF(s->buffer);
+        if (s->length + 512 >= allocated) {
+                if (!GREEDY_REALLOC(s->buffer, s->length + 1 + 1024)) {
                         log_oom();
                         goto terminate;
                 }
+
+                allocated = MALLOC_ELEMENTSOF(s->buffer);
         }
 
         /* Try to make use of the allocated buffer in full, but never read more than the configured line size. Also,
          * always leave room for a terminating NUL we might need to add. */
-        limit = MIN(s->allocated - 1, MAX(s->server->line_max, STDOUT_STREAM_SETUP_PROTOCOL_LINE_MAX));
+        limit = MIN(allocated - 1, MAX(s->server->line_max, STDOUT_STREAM_SETUP_PROTOCOL_LINE_MAX));
         assert(s->length <= limit);
         iovec = IOVEC_MAKE(s->buffer + s->length, limit - s->length);
 
index 49392d1beac483de7a9c7efffdb1a6160680dddf..35a74dd7c27874ac6ab752773e3819a52b04daaa 100644 (file)
@@ -53,7 +53,7 @@ struct sd_dhcp_lease {
         DHCPServerData servers[_SD_DHCP_LEASE_SERVER_TYPE_MAX];
 
         struct sd_dhcp_route *static_route;
-        size_t static_route_size, static_route_allocated;
+        size_t static_route_size;
 
         uint16_t mtu; /* 0 if unset */
 
index 1e5a6b0e3779be0cf250bfc8f00c0c80e6c19d85..34e1f61cb08c1e52e1f6726f22fec0a01fadb6c1 100644 (file)
@@ -104,8 +104,7 @@ int dhcp6_option_parse(uint8_t **buf, size_t *buflen, uint16_t *optcode,
 int dhcp6_option_parse_status(DHCP6Option *option, size_t len);
 int dhcp6_option_parse_ia(sd_dhcp6_client *client, DHCP6Option *iaoption, DHCP6IA *ia, uint16_t *ret_status_code);
 int dhcp6_option_parse_ip6addrs(uint8_t *optval, uint16_t optlen,
-                                struct in6_addr **addrs, size_t count,
-                                size_t *allocated);
+                                struct in6_addr **addrs, size_t count);
 int dhcp6_option_parse_domainname_list(const uint8_t *optval, uint16_t optlen,
                                        char ***str_arr);
 int dhcp6_option_parse_domainname(const uint8_t *optval, uint16_t optlen, char **str);
index e9e2362d6f1c5a0c875408cd85b6a81e064a443a..391b4f1fa9b833a1a991b5c99d5f3e2dfe4179a7 100644 (file)
@@ -27,12 +27,10 @@ struct sd_dhcp6_lease {
 
         struct in6_addr *dns;
         size_t dns_count;
-        size_t dns_allocated;
         char **domains;
         size_t domains_count;
         struct in6_addr *ntp;
         size_t ntp_count;
-        size_t ntp_allocated;
         char **ntp_fqdn;
         size_t ntp_fqdn_count;
         char *fqdn;
index 6aca898d336756fc69784830bd499c719be7e802..97ef03a2d2469f02a0cab49161df97faae699f15 100644 (file)
@@ -678,14 +678,12 @@ int dhcp6_option_parse_ia(sd_dhcp6_client *client, DHCP6Option *iaoption, DHCP6I
 }
 
 int dhcp6_option_parse_ip6addrs(uint8_t *optval, uint16_t optlen,
-                                struct in6_addr **addrs, size_t count,
-                                size_t *allocated) {
+                                struct in6_addr **addrs, size_t count) {
 
         if (optlen == 0 || optlen % sizeof(struct in6_addr) != 0)
                 return -EINVAL;
 
-        if (!GREEDY_REALLOC(*addrs, *allocated,
-                            count * sizeof(struct in6_addr) + optlen))
+        if (!GREEDY_REALLOC(*addrs, count * sizeof(struct in6_addr) + optlen))
                 return -ENOMEM;
 
         memcpy(*addrs + count, optval, optlen);
@@ -697,10 +695,10 @@ int dhcp6_option_parse_ip6addrs(uint8_t *optval, uint16_t optlen,
 
 static int parse_domain(const uint8_t **data, uint16_t *len, char **out_domain) {
         _cleanup_free_ char *ret = NULL;
-        size_t n = 0, allocated = 0;
         const uint8_t *optval = *data;
         uint16_t optlen = *len;
         bool first = true;
+        size_t n = 0;
         int r;
 
         if (optlen <= 1)
@@ -730,7 +728,7 @@ static int parse_domain(const uint8_t **data, uint16_t *len, char **out_domain)
                 optval += c;
                 optlen -= c;
 
-                if (!GREEDY_REALLOC(ret, allocated, n + !first + DNS_LABEL_ESCAPED_MAX))
+                if (!GREEDY_REALLOC(ret, n + !first + DNS_LABEL_ESCAPED_MAX))
                         return -ENOMEM;
 
                 if (first)
@@ -746,7 +744,7 @@ static int parse_domain(const uint8_t **data, uint16_t *len, char **out_domain)
         }
 
         if (n) {
-                if (!GREEDY_REALLOC(ret, allocated, n + 1))
+                if (!GREEDY_REALLOC(ret, n + 1))
                         return -ENOMEM;
                 ret[n] = 0;
         }
index 46f30332bbf8ffbeb82b2e2d62760bfb39047e6e..6db07b0dbc0cca78859a0911fc63d2e50efd32ba 100644 (file)
@@ -630,7 +630,7 @@ static int get_dnssl_info(sd_ndisc_router *rt, uint8_t **ret) {
 _public_ int sd_ndisc_router_dnssl_get_domains(sd_ndisc_router *rt, char ***ret) {
         _cleanup_strv_free_ char **l = NULL;
         _cleanup_free_ char *e = NULL;
-        size_t allocated = 0, n = 0, left;
+        size_t n = 0, left;
         uint8_t *ri, *p;
         bool first = true;
         int r;
@@ -691,7 +691,7 @@ _public_ int sd_ndisc_router_dnssl_get_domains(sd_ndisc_router *rt, char ***ret)
                 if (1U + *p + 1U > left)
                         return -EBADMSG;
 
-                if (!GREEDY_REALLOC(e, allocated, n + !first + DNS_LABEL_ESCAPED_MAX + 1U))
+                if (!GREEDY_REALLOC(e, n + !first + DNS_LABEL_ESCAPED_MAX + 1U))
                         return -ENOMEM;
 
                 if (first)
index 12b73cd50d8175eeeb6597dca258925becc1f4cc..bc7aec2f9d4f870a4e4d4c7dd10c06cf724919b9 100644 (file)
@@ -158,13 +158,12 @@ void serialize_dhcp_routes(FILE *f, const char *key, sd_dhcp_route **routes, siz
         fputs("\n", f);
 }
 
-int deserialize_dhcp_routes(struct sd_dhcp_route **ret, size_t *ret_size, size_t *ret_allocated, const char *string) {
+int deserialize_dhcp_routes(struct sd_dhcp_route **ret, size_t *ret_size, const char *string) {
         _cleanup_free_ struct sd_dhcp_route *routes = NULL;
-        size_t size = 0, allocated = 0;
+        size_t size = 0;
 
         assert(ret);
         assert(ret_size);
-        assert(ret_allocated);
         assert(string);
 
          /* WORD FORMAT: dst_ip/dst_prefixlen,gw_ip */
@@ -180,7 +179,7 @@ int deserialize_dhcp_routes(struct sd_dhcp_route **ret, size_t *ret_size, size_t
                 if (r == 0)
                         break;
 
-                if (!GREEDY_REALLOC(routes, allocated, size + 1))
+                if (!GREEDY_REALLOC(routes, size + 1))
                         return -ENOMEM;
 
                 tok = word;
@@ -220,7 +219,6 @@ int deserialize_dhcp_routes(struct sd_dhcp_route **ret, size_t *ret_size, size_t
         }
 
         *ret_size = size;
-        *ret_allocated = allocated;
         *ret = TAKE_PTR(routes);
 
         return 0;
index e5b853c0cd7acec6a8924e79dedc9353b75ed34b..895a00d01b403186202dfc499f65b51b1504f695 100644 (file)
@@ -22,7 +22,7 @@ struct sd_dhcp_route;
 struct sd_dhcp_lease;
 
 void serialize_dhcp_routes(FILE *f, const char *key, sd_dhcp_route **routes, size_t size);
-int deserialize_dhcp_routes(struct sd_dhcp_route **ret, size_t *ret_size, size_t *ret_allocated, const char *string);
+int deserialize_dhcp_routes(struct sd_dhcp_route **ret, size_t *ret_size, const char *string);
 
 /* It is not necessary to add deserialize_dhcp_option(). Use unhexmem() instead. */
 int serialize_dhcp_option(FILE *f, const char *key, const void *data, size_t size);
index 6d88c88e6bb794c6ac3e9360c80f4b0bdec65653..095a4ee68374928b0f25fa0618521ff146b41c6b 100644 (file)
@@ -440,14 +440,13 @@ static int lease_parse_sip_server(const uint8_t *option, size_t len, struct in_a
 
 static int lease_parse_routes(
                 const uint8_t *option, size_t len,
-                struct sd_dhcp_route **routes, size_t *routes_size, size_t *routes_allocated) {
+                struct sd_dhcp_route **routes, size_t *routes_size) {
 
         struct in_addr addr;
 
         assert(option || len <= 0);
         assert(routes);
         assert(routes_size);
-        assert(routes_allocated);
 
         if (len <= 0)
                 return 0;
@@ -455,7 +454,7 @@ static int lease_parse_routes(
         if (len % 8 != 0)
                 return -EINVAL;
 
-        if (!GREEDY_REALLOC(*routes, *routes_allocated, *routes_size + (len / 8)))
+        if (!GREEDY_REALLOC(*routes, *routes_size + (len / 8)))
                 return -ENOMEM;
 
         while (len >= 8) {
@@ -486,12 +485,11 @@ static int lease_parse_routes(
 /* parses RFC3442 Classless Static Route Option */
 static int lease_parse_classless_routes(
                 const uint8_t *option, size_t len,
-                struct sd_dhcp_route **routes, size_t *routes_size, size_t *routes_allocated) {
+                struct sd_dhcp_route **routes, size_t *routes_size) {
 
         assert(option || len <= 0);
         assert(routes);
         assert(routes_size);
-        assert(routes_allocated);
 
         if (len <= 0)
                 return 0;
@@ -502,7 +500,7 @@ static int lease_parse_classless_routes(
                 uint8_t dst_octets;
                 struct sd_dhcp_route *route;
 
-                if (!GREEDY_REALLOC(*routes, *routes_allocated, *routes_size + 1))
+                if (!GREEDY_REALLOC(*routes, *routes_size + 1))
                         return -ENOMEM;
 
                 route = *routes + *routes_size;
@@ -616,7 +614,7 @@ int dhcp_lease_parse_options(uint8_t code, uint8_t len, const void *option, void
                 break;
 
         case SD_DHCP_OPTION_STATIC_ROUTE:
-                r = lease_parse_routes(option, len, &lease->static_route, &lease->static_route_size, &lease->static_route_allocated);
+                r = lease_parse_routes(option, len, &lease->static_route, &lease->static_route_size);
                 if (r < 0)
                         log_debug_errno(r, "Failed to parse static routes, ignoring: %m");
                 break;
@@ -678,8 +676,7 @@ int dhcp_lease_parse_options(uint8_t code, uint8_t len, const void *option, void
                 r = lease_parse_classless_routes(
                                 option, len,
                                 &lease->static_route,
-                                &lease->static_route_size,
-                                &lease->static_route_allocated);
+                                &lease->static_route_size);
                 if (r < 0)
                         log_debug_errno(r, "Failed to parse classless routes, ignoring: %m");
                 break;
@@ -747,7 +744,7 @@ int dhcp_lease_parse_search_domains(const uint8_t *option, size_t len, char ***d
 
         while (pos < len) {
                 _cleanup_free_ char *name = NULL;
-                size_t n = 0, allocated = 0;
+                size_t n = 0;
                 size_t jump_barrier = pos, next_chunk = 0;
                 bool first = true;
 
@@ -767,7 +764,7 @@ int dhcp_lease_parse_search_domains(const uint8_t *option, size_t len, char ***d
                                 if (pos >= len)
                                         return -EBADMSG;
 
-                                if (!GREEDY_REALLOC(name, allocated, n + !first + DNS_LABEL_ESCAPED_MAX))
+                                if (!GREEDY_REALLOC(name, n + !first + DNS_LABEL_ESCAPED_MAX))
                                         return -ENOMEM;
 
                                 if (first)
@@ -806,7 +803,7 @@ int dhcp_lease_parse_search_domains(const uint8_t *option, size_t len, char ***d
                                 return -EBADMSG;
                 }
 
-                if (!GREEDY_REALLOC(name, allocated, n + 1))
+                if (!GREEDY_REALLOC(name, n + 1))
                         return -ENOMEM;
                 name[n] = 0;
 
@@ -1232,7 +1229,6 @@ int dhcp_lease_load(sd_dhcp_lease **ret, const char *lease_file) {
                 r = deserialize_dhcp_routes(
                                 &lease->static_route,
                                 &lease->static_route_size,
-                                &lease->static_route_allocated,
                                 routes);
                 if (r < 0)
                         log_debug_errno(r, "Failed to parse DHCP routes %s, ignoring: %m", routes);
index 10f82996c8c260f3633113a1158b86f1b34e9c0c..855a61bb216c0a5e7cfa2681c87fc9df366272d1 100644 (file)
@@ -66,7 +66,6 @@ struct sd_dhcp6_client {
         bool information_request;
         bool iaid_set;
         be16_t *req_opts;
-        size_t req_opts_allocated;
         size_t req_opts_len;
         char *fqdn;
         char *mudurl;
@@ -456,8 +455,7 @@ int sd_dhcp6_client_set_request_option(sd_dhcp6_client *client, uint16_t option)
                 if (client->req_opts[t] == htobe16(option))
                         return -EEXIST;
 
-        if (!GREEDY_REALLOC(client->req_opts, client->req_opts_allocated,
-                            client->req_opts_len + 1))
+        if (!GREEDY_REALLOC(client->req_opts, client->req_opts_len + 1))
                 return -ENOMEM;
 
         client->req_opts[client->req_opts_len++] = htobe16(option);
index aca2c1f7e072546929bf5a2d7beb3965bb0cab85..9082185bcaf1a9b1ad18c660b420f0d6ddb907fe 100644 (file)
@@ -203,8 +203,7 @@ int dhcp6_lease_set_dns(sd_dhcp6_lease *lease, uint8_t *optval, size_t optlen) {
                 return 0;
 
         r = dhcp6_option_parse_ip6addrs(optval, optlen, &lease->dns,
-                                        lease->dns_count,
-                                        &lease->dns_allocated);
+                                        lease->dns_count);
         if (r < 0)
                 return r;
 
@@ -269,7 +268,6 @@ int dhcp6_lease_set_ntp(sd_dhcp6_lease *lease, uint8_t *optval, size_t optlen) {
 
         lease->ntp = mfree(lease->ntp);
         lease->ntp_count = 0;
-        lease->ntp_allocated = 0;
 
         while ((r = dhcp6_option_parse(&optval, &optlen, &subopt, &sublen,
                                        &subval)) >= 0) {
@@ -284,8 +282,7 @@ int dhcp6_lease_set_ntp(sd_dhcp6_lease *lease, uint8_t *optval, size_t optlen) {
 
                         s = dhcp6_option_parse_ip6addrs(subval, sublen,
                                                         &lease->ntp,
-                                                        lease->ntp_count,
-                                                        &lease->ntp_allocated);
+                                                        lease->ntp_count);
                         if (s < 0)
                                 return s;
 
@@ -327,8 +324,7 @@ int dhcp6_lease_set_sntp(sd_dhcp6_lease *lease, uint8_t *optval, size_t optlen)
         /* Using deprecated SNTP information */
 
         r = dhcp6_option_parse_ip6addrs(optval, optlen, &lease->ntp,
-                                        lease->ntp_count,
-                                        &lease->ntp_allocated);
+                                        lease->ntp_count);
         if (r < 0)
                 return r;
 
index 5e748fdb462b986bf4877327e0ca51886e3c6e5c..4468b45dd1edb82a4cdcd31d678f1a8a67943e26 100644 (file)
@@ -882,8 +882,6 @@ int bus_creds_add_more(sd_bus_creds *c, uint64_t mask, pid_t pid, pid_t tid) {
                                 if (missing & SD_BUS_CREDS_SUPPLEMENTARY_GIDS) {
                                         p = startswith(line, "Groups:");
                                         if (p) {
-                                                size_t allocated = 0;
-
                                                 for (;;) {
                                                         unsigned long g;
                                                         int n = 0;
@@ -895,7 +893,7 @@ int bus_creds_add_more(sd_bus_creds *c, uint64_t mask, pid_t pid, pid_t tid) {
                                                         if (sscanf(p, "%lu%n", &g, &n) != 1)
                                                                 return -EIO;
 
-                                                        if (!GREEDY_REALLOC(c->supplementary_gids, allocated, c->n_supplementary_gids+1))
+                                                        if (!GREEDY_REALLOC(c->supplementary_gids, c->n_supplementary_gids+1))
                                                                 return -ENOMEM;
 
                                                         c->supplementary_gids[c->n_supplementary_gids++] = (gid_t) g;
index 4a45507da502c5916de22a6accd5baabb97c71d6..51673ad1c5c81f8b1dd557a828d050f570eb2296 100644 (file)
@@ -222,12 +222,10 @@ struct sd_bus {
 
         sd_bus_message **rqueue;
         size_t rqueue_size;
-        size_t rqueue_allocated;
 
         sd_bus_message **wqueue;
         size_t wqueue_size;
         size_t windex;
-        size_t wqueue_allocated;
 
         uint64_t cookie;
         uint64_t read_counter; /* A counter for each incoming msg */
index 96b50aeaed6360d4e7275f546cee37b8b10c00dd..648f1ca62fe3b2d1ebae75743f4432d74a4cfea5 100644 (file)
@@ -711,7 +711,6 @@ int bus_match_parse(
                 unsigned *ret_n_components) {
 
         struct bus_match_component *components = NULL;
-        size_t components_allocated = 0;
         unsigned n_components = 0;
         int r;
 
@@ -724,7 +723,6 @@ int bus_match_parse(
                 enum bus_match_node_type t;
                 unsigned j = 0;
                 _cleanup_free_ char *value = NULL;
-                size_t value_allocated = 0;
                 bool escaped = false, quoted;
                 uint8_t u;
 
@@ -780,7 +778,7 @@ int bus_match_parse(
                                 }
                         }
 
-                        if (!GREEDY_REALLOC(value, value_allocated, j + 2)) {
+                        if (!GREEDY_REALLOC(value, j + 2)) {
                                 r = -ENOMEM;
                                 goto fail;
                         }
@@ -806,7 +804,7 @@ int bus_match_parse(
                 } else
                         u = 0;
 
-                if (!GREEDY_REALLOC(components, components_allocated, n_components + 1)) {
+                if (!GREEDY_REALLOC(components, n_components + 1)) {
                         r = -ENOMEM;
                         goto fail;
                 }
index b25064b672147dee6c1d2b9ec1fe30cd48360148..20f7396c74ef1ebf80734e2014c823cdf52036a7 100644 (file)
@@ -117,7 +117,6 @@ static void message_reset_containers(sd_bus_message *m) {
                 message_free_last_container(m);
 
         m->containers = mfree(m->containers);
-        m->containers_allocated = 0;
         m->root_container.index = 0;
 }
 
@@ -1289,7 +1288,7 @@ static int message_add_offset(sd_bus_message *m, size_t offset) {
         if (!c->need_offsets)
                 return 0;
 
-        if (!GREEDY_REALLOC(c->offsets, c->offsets_allocated, c->n_offsets + 1))
+        if (!GREEDY_REALLOC(c->offsets, c->n_offsets + 1))
                 return -ENOMEM;
 
         c->offsets[c->n_offsets++] = offset;
@@ -2031,7 +2030,7 @@ _public_ int sd_bus_message_open_container(
         assert_return(!m->poisoned, -ESTALE);
 
         /* Make sure we have space for one more container */
-        if (!GREEDY_REALLOC(m->containers, m->containers_allocated, m->n_containers + 1)) {
+        if (!GREEDY_REALLOC(m->containers, m->n_containers + 1)) {
                 m->poisoned = true;
                 return -ENOMEM;
         }
@@ -4111,7 +4110,7 @@ _public_ int sd_bus_message_enter_container(sd_bus_message *m,
         if (m->n_containers >= BUS_CONTAINER_DEPTH)
                 return -EBADMSG;
 
-        if (!GREEDY_REALLOC(m->containers, m->containers_allocated, m->n_containers + 1))
+        if (!GREEDY_REALLOC(m->containers, m->n_containers + 1))
                 return -ENOMEM;
 
         if (message_end_of_signature(m))
index 3561737e2f73c2b1cf379ea0ff4a922a4eaac72a..e5bf5df160b61169b8cb93f7d93918ba40d93e1d 100644 (file)
@@ -26,7 +26,7 @@ struct bus_container {
         uint32_t *array_size;
 
         /* gvariant: list of offsets to end of children if this is struct/dict entry/array */
-        size_t *offsets, n_offsets, offsets_allocated, offset_index;
+        size_t *offsets, n_offsets, offset_index;
         size_t item_size;
 
         char *peeked_signature;
@@ -111,7 +111,6 @@ struct sd_bus_message {
 
         struct bus_container root_container, *containers;
         size_t n_containers;
-        size_t containers_allocated;
 
         struct iovec *iovec;
         struct iovec iovec_fixed[2];
index 832526cc1b7124969640ad827359e0b80b8cb58a..16e61e1e89ce08452b309de198d7ff5927b0e2df 100644 (file)
@@ -700,7 +700,7 @@ int bus_socket_start_auth(sd_bus *b) {
 static int bus_socket_inotify_setup(sd_bus *b) {
         _cleanup_free_ int *new_watches = NULL;
         _cleanup_free_ char *absolute = NULL;
-        size_t n_allocated = 0, n = 0, done = 0, i;
+        size_t n = 0, done = 0, i;
         unsigned max_follow = 32;
         const char *p;
         int wd, r;
@@ -737,7 +737,7 @@ static int bus_socket_inotify_setup(sd_bus *b) {
          * that exists we want to know when files are created or moved into it. For all parents of it we just care if
          * they are removed or renamed. */
 
-        if (!GREEDY_REALLOC(new_watches, n_allocated, n + 1)) {
+        if (!GREEDY_REALLOC(new_watches, n + 1)) {
                 r = -ENOMEM;
                 goto fail;
         }
@@ -786,7 +786,7 @@ static int bus_socket_inotify_setup(sd_bus *b) {
                         goto fail;
                 }
 
-                if (!GREEDY_REALLOC(new_watches, n_allocated, n + 1)) {
+                if (!GREEDY_REALLOC(new_watches, n + 1)) {
                         r = -ENOMEM;
                         goto fail;
                 }
index 31f527c571d84264c3b9657fde5c5f7f6fc64b95..0ec0d8d26ea1c9a1a415bdaf31888f444d0d21e4 100644 (file)
@@ -153,13 +153,11 @@ static void bus_reset_queues(sd_bus *b) {
                 bus_message_unref_queued(b->rqueue[--b->rqueue_size], b);
 
         b->rqueue = mfree(b->rqueue);
-        b->rqueue_allocated = 0;
 
         while (b->wqueue_size > 0)
                 bus_message_unref_queued(b->wqueue[--b->wqueue_size], b);
 
         b->wqueue = mfree(b->wqueue);
-        b->wqueue_allocated = 0;
 }
 
 static sd_bus* bus_free(sd_bus *b) {
@@ -253,7 +251,7 @@ _public_ int sd_bus_new(sd_bus **ret) {
         };
 
         /* We guarantee that wqueue always has space for at least one entry */
-        if (!GREEDY_REALLOC(b->wqueue, b->wqueue_allocated, 1))
+        if (!GREEDY_REALLOC(b->wqueue, 1))
                 return -ENOMEM;
 
         assert_se(pthread_mutex_init(&b->memfd_cache_mutex, NULL) == 0);
@@ -631,8 +629,8 @@ int bus_start_running(sd_bus *bus) {
 }
 
 static int parse_address_key(const char **p, const char *key, char **value) {
-        size_t l, n = 0, allocated = 0;
         _cleanup_free_ char *r = NULL;
+        size_t l, n = 0;
         const char *a;
 
         assert(p);
@@ -675,7 +673,7 @@ static int parse_address_key(const char **p, const char *key, char **value) {
                         a++;
                 }
 
-                if (!GREEDY_REALLOC(r, allocated, n + 2))
+                if (!GREEDY_REALLOC(r, n + 2))
                         return -ENOMEM;
 
                 r[n++] = c;
@@ -847,7 +845,6 @@ static int parse_exec_address(sd_bus *b, const char **p, char **guid) {
         char *path = NULL;
         unsigned n_argv = 0, j;
         char **argv = NULL;
-        size_t allocated = 0;
         int r;
 
         assert(b);
@@ -881,7 +878,7 @@ static int parse_exec_address(sd_bus *b, const char **p, char **guid) {
                         (*p)++;
 
                         if (ul >= n_argv) {
-                                if (!GREEDY_REALLOC0(argv, allocated, ul + 2)) {
+                                if (!GREEDY_REALLOC0(argv, ul + 2)) {
                                         r = -ENOMEM;
                                         goto fail;
                                 }
@@ -2048,7 +2045,7 @@ int bus_rqueue_make_room(sd_bus *bus) {
         if (bus->rqueue_size >= BUS_RQUEUE_MAX)
                 return -ENOBUFS;
 
-        if (!GREEDY_REALLOC(bus->rqueue, bus->rqueue_allocated, bus->rqueue_size + 1))
+        if (!GREEDY_REALLOC(bus->rqueue, bus->rqueue_size + 1))
                 return -ENOMEM;
 
         return 0;
@@ -2164,7 +2161,7 @@ _public_ int sd_bus_send(sd_bus *bus, sd_bus_message *_m, uint64_t *cookie) {
                 if (bus->wqueue_size >= BUS_WQUEUE_MAX)
                         return -ENOBUFS;
 
-                if (!GREEDY_REALLOC(bus->wqueue, bus->wqueue_allocated, bus->wqueue_size + 1))
+                if (!GREEDY_REALLOC(bus->wqueue, bus->wqueue_size + 1))
                         return -ENOMEM;
 
                 bus->wqueue[bus->wqueue_size++] = bus_message_ref_queued(m, bus);
index a290dbcc9ce5b17ced396878ad95360cc36a69ce..3ae80ab94281ce84b195ff65ab78307fd8b04e84 100644 (file)
@@ -29,7 +29,7 @@ struct sd_device_enumerator {
 
         DeviceEnumerationType type;
         sd_device **devices;
-        size_t n_devices, n_allocated, current_device_index;
+        size_t n_devices, current_device_index;
         bool scan_uptodate;
 
         Set *match_subsystem;
@@ -279,7 +279,7 @@ int device_enumerator_add_device(sd_device_enumerator *enumerator, sd_device *de
         assert_return(enumerator, -EINVAL);
         assert_return(device, -EINVAL);
 
-        if (!GREEDY_REALLOC(enumerator->devices, enumerator->n_allocated, enumerator->n_devices + 1))
+        if (!GREEDY_REALLOC(enumerator->devices, enumerator->n_devices + 1))
                 return -ENOMEM;
 
         enumerator->devices[enumerator->n_devices++] = sd_device_ref(device);
index e612dbe0a072ef5d8597487c529d3bd9ea63a650..09ef87f84ccd5b97440cc176721972904e814862 100644 (file)
@@ -480,7 +480,6 @@ static int device_update_properties_bufs(sd_device *device) {
         const char *val, *prop;
         _cleanup_free_ char **buf_strv = NULL;
         _cleanup_free_ uint8_t *buf_nulstr = NULL;
-        size_t allocated_nulstr = 0;
         size_t nulstr_len = 0, num = 0, i = 0;
 
         assert(device);
@@ -493,7 +492,7 @@ static int device_update_properties_bufs(sd_device *device) {
 
                 len = strlen(prop) + 1 + strlen(val);
 
-                buf_nulstr = GREEDY_REALLOC0(buf_nulstr, allocated_nulstr, nulstr_len + len + 2);
+                buf_nulstr = GREEDY_REALLOC0(buf_nulstr, nulstr_len + len + 2);
                 if (!buf_nulstr)
                         return -ENOMEM;
 
index 9fba09ba280a58981685bfa6ccaf3b7d783a7b86..611af45293ac3d1b084c943d874b271cf8128c30 100644 (file)
@@ -144,7 +144,6 @@ struct sd_event {
         unsigned n_sources;
 
         struct epoll_event *event_queue;
-        size_t event_queue_allocated;
 
         LIST_HEAD(sd_event_source, sources);
 
@@ -3858,38 +3857,45 @@ static int epoll_wait_usec(
 }
 
 static int process_epoll(sd_event *e, usec_t timeout, int64_t threshold, int64_t *ret_min_priority) {
+        size_t n_event_queue, m, n_event_max;
         int64_t min_priority = threshold;
         bool something_new = false;
-        size_t n_event_queue, m;
         int r;
 
         assert(e);
         assert(ret_min_priority);
 
         n_event_queue = MAX(e->n_sources, 1u);
-        if (!GREEDY_REALLOC(e->event_queue, e->event_queue_allocated, n_event_queue))
+        if (!GREEDY_REALLOC(e->event_queue, n_event_queue))
                 return -ENOMEM;
 
+        n_event_max = MALLOC_ELEMENTSOF(e->event_queue);
+
         /* If we still have inotify data buffered, then query the other fds, but don't wait on it */
         if (e->inotify_data_buffered)
                 timeout = 0;
 
         for (;;) {
-                r = epoll_wait_usec(e->epoll_fd, e->event_queue, e->event_queue_allocated, timeout);
+                r = epoll_wait_usec(
+                                e->epoll_fd,
+                                e->event_queue,
+                                n_event_max,
+                                timeout);
                 if (r < 0)
                         return r;
 
                 m = (size_t) r;
 
-                if (m < e->event_queue_allocated)
+                if (m < n_event_max)
                         break;
 
-                if (e->event_queue_allocated >= n_event_queue * 10)
+                if (n_event_max >= n_event_queue * 10)
                         break;
 
-                if (!GREEDY_REALLOC(e->event_queue, e->event_queue_allocated, e->event_queue_allocated + n_event_queue))
+                if (!GREEDY_REALLOC(e->event_queue, n_event_max + n_event_queue))
                         return -ENOMEM;
 
+                n_event_max = MALLOC_ELEMENTSOF(e->event_queue);
                 timeout = 0;
         }
 
index aea3241d1853be26319d1ea518672972b08b9a2b..ce8d47ccc3d2564111af675dc6e3f220a7736873 100644 (file)
@@ -249,7 +249,7 @@ static int catalog_entry_lang(
 int catalog_import_file(OrderedHashmap *h, const char *path) {
         _cleanup_fclose_ FILE *f = NULL;
         _cleanup_free_ char *payload = NULL;
-        size_t payload_size = 0, payload_allocated = 0;
+        size_t payload_size = 0;
         unsigned n = 0;
         sd_id128_t id;
         _cleanup_free_ char *deflang = NULL, *lang = NULL;
@@ -345,8 +345,7 @@ int catalog_import_file(OrderedHashmap *h, const char *path) {
                                                path, n);
 
                 line_len = strlen(line);
-                if (!GREEDY_REALLOC(payload, payload_allocated,
-                                    payload_size + (empty_line ? 1 : 0) + line_len + 1 + 1))
+                if (!GREEDY_REALLOC(payload, payload_size + (empty_line ? 1 : 0) + line_len + 1 + 1))
                         return log_oom();
 
                 if (empty_line)
index f366a597b5a5b2693a965a8c633ddfe5a9c78228..c788dd8caf9e9e00b7ced2434cac1e0a94c45005 100644 (file)
@@ -1,10 +1,11 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
 #include <inttypes.h>
+#include <malloc.h>
 #include <stdlib.h>
 #include <sys/mman.h>
-#include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/types.h>
 #include <unistd.h>
 
 #if HAVE_XZ
@@ -157,8 +158,12 @@ int compress_blob_zstd(
 #endif
 }
 
-int decompress_blob_xz(const void *src, uint64_t src_size,
-                       void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max) {
+int decompress_blob_xz(
+                const void *src,
+                uint64_t src_size,
+                void **dst,
+                size_t* dst_size,
+                size_t dst_max) {
 
 #if HAVE_XZ
         _cleanup_(lzma_end) lzma_stream s = LZMA_STREAM_INIT;
@@ -168,16 +173,14 @@ int decompress_blob_xz(const void *src, uint64_t src_size,
         assert(src);
         assert(src_size > 0);
         assert(dst);
-        assert(dst_alloc_size);
         assert(dst_size);
-        assert(*dst_alloc_size == 0 || *dst);
 
         ret = lzma_stream_decoder(&s, UINT64_MAX, 0);
         if (ret != LZMA_OK)
                 return -ENOMEM;
 
         space = MIN(src_size * 2, dst_max ?: SIZE_MAX);
-        if (!greedy_realloc(dst, dst_alloc_size, space, 1))
+        if (!greedy_realloc(dst, space, 1))
                 return -ENOMEM;
 
         s.next_in = src;
@@ -203,7 +206,7 @@ int decompress_blob_xz(const void *src, uint64_t src_size,
 
                 used = space - s.avail_out;
                 space = MIN(2 * space, dst_max ?: SIZE_MAX);
-                if (!greedy_realloc(dst, dst_alloc_size, space, 1))
+                if (!greedy_realloc(dst, space, 1))
                         return -ENOMEM;
 
                 s.avail_out = space - used;
@@ -217,8 +220,12 @@ int decompress_blob_xz(const void *src, uint64_t src_size,
 #endif
 }
 
-int decompress_blob_lz4(const void *src, uint64_t src_size,
-                        void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max) {
+int decompress_blob_lz4(
+                const void *src,
+                uint64_t src_size,
+                void **dst,
+                size_t* dst_size,
+                size_t dst_max) {
 
 #if HAVE_LZ4
         char* out;
@@ -227,9 +234,7 @@ int decompress_blob_lz4(const void *src, uint64_t src_size,
         assert(src);
         assert(src_size > 0);
         assert(dst);
-        assert(dst_alloc_size);
         assert(dst_size);
-        assert(*dst_alloc_size == 0 || *dst);
 
         if (src_size <= 8)
                 return -EBADMSG;
@@ -237,14 +242,9 @@ int decompress_blob_lz4(const void *src, uint64_t src_size,
         size = unaligned_read_le64(src);
         if (size < 0 || (unsigned) size != unaligned_read_le64(src))
                 return -EFBIG;
-        if ((size_t) size > *dst_alloc_size) {
-                out = realloc(*dst, size);
-                if (!out)
-                        return -ENOMEM;
-                *dst = out;
-                *dst_alloc_size = size;
-        } else
-                out = *dst;
+        out = greedy_realloc(dst, size, 1);
+        if (!out)
+                return -ENOMEM;
 
         r = LZ4_decompress_safe((char*)src + 8, out, src_size - 8, size);
         if (r < 0 || r != size)
@@ -258,8 +258,11 @@ int decompress_blob_lz4(const void *src, uint64_t src_size,
 }
 
 int decompress_blob_zstd(
-                const void *src, uint64_t src_size,
-                void **dst, size_t *dst_alloc_size, size_t *dst_size, size_t dst_max) {
+                const void *src,
+                uint64_t src_size,
+                void **dst,
+                size_t *dst_size,
+                size_t dst_max) {
 
 #if HAVE_ZSTD
         uint64_t size;
@@ -267,9 +270,7 @@ int decompress_blob_zstd(
         assert(src);
         assert(src_size > 0);
         assert(dst);
-        assert(dst_alloc_size);
         assert(dst_size);
-        assert(*dst_alloc_size == 0 || *dst);
 
         size = ZSTD_getFrameContentSize(src, src_size);
         if (IN_SET(size, ZSTD_CONTENTSIZE_ERROR, ZSTD_CONTENTSIZE_UNKNOWN))
@@ -280,7 +281,7 @@ int decompress_blob_zstd(
         if (size > SIZE_MAX)
                 return -E2BIG;
 
-        if (!(greedy_realloc(dst, dst_alloc_size, MAX(ZSTD_DStreamOutSize(), size), 1)))
+        if (!(greedy_realloc(dst, MAX(ZSTD_DStreamOutSize(), size), 1)))
                 return -ENOMEM;
 
         _cleanup_(ZSTD_freeDCtxp) ZSTD_DCtx *dctx = ZSTD_createDCtx();
@@ -293,7 +294,7 @@ int decompress_blob_zstd(
         };
         ZSTD_outBuffer output = {
                 .dst = *dst,
-                .size = *dst_alloc_size,
+                .size = MALLOC_SIZEOF_SAFE(*dst),
         };
 
         size_t k = ZSTD_decompressStream(dctx, &output, &input);
@@ -312,57 +313,63 @@ int decompress_blob_zstd(
 
 int decompress_blob(
                 int compression,
-                const void *src, uint64_t src_size,
-                void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max) {
+                const void *src,
+                uint64_t src_size,
+                void **dst,
+                size_t* dst_size,
+                size_t dst_max) {
 
         if (compression == OBJECT_COMPRESSED_XZ)
                 return decompress_blob_xz(
                                 src, src_size,
-                                dst, dst_alloc_size, dst_size, dst_max);
+                                dst, dst_size, dst_max);
         else if (compression == OBJECT_COMPRESSED_LZ4)
                 return decompress_blob_lz4(
                                 src, src_size,
-                                dst, dst_alloc_size, dst_size, dst_max);
+                                dst, dst_size, dst_max);
         else if (compression == OBJECT_COMPRESSED_ZSTD)
                 return decompress_blob_zstd(
                                 src, src_size,
-                                dst, dst_alloc_size, dst_size, dst_max);
+                                dst, dst_size, dst_max);
         else
                 return -EPROTONOSUPPORT;
 }
 
-int decompress_startswith_xz(const void *src, uint64_t src_size,
-                             void **buffer, size_t *buffer_size,
-                             const void *prefix, size_t prefix_len,
-                             uint8_t extra) {
+int decompress_startswith_xz(
+                const void *src,
+                uint64_t src_size,
+                void **buffer,
+                const void *prefix,
+                size_t prefix_len,
+                uint8_t extra) {
 
 #if HAVE_XZ
         _cleanup_(lzma_end) lzma_stream s = LZMA_STREAM_INIT;
+        size_t allocated;
         lzma_ret ret;
 
-        /* Checks whether the decompressed blob starts with the
-         * mentioned prefix. The byte extra needs to follow the
-         * prefix */
+        /* Checks whether the decompressed blob starts with the mentioned prefix. The byte extra needs to
+         * follow the prefix */
 
         assert(src);
         assert(src_size > 0);
         assert(buffer);
-        assert(buffer_size);
         assert(prefix);
-        assert(*buffer_size == 0 || *buffer);
 
         ret = lzma_stream_decoder(&s, UINT64_MAX, 0);
         if (ret != LZMA_OK)
                 return -EBADMSG;
 
-        if (!(greedy_realloc(buffer, buffer_size, ALIGN_8(prefix_len + 1), 1)))
+        if (!(greedy_realloc(buffer, ALIGN_8(prefix_len + 1), 1)))
                 return -ENOMEM;
 
+        allocated = MALLOC_SIZEOF_SAFE(*buffer);
+
         s.next_in = src;
         s.avail_in = src_size;
 
         s.next_out = *buffer;
-        s.avail_out = *buffer_size;
+        s.avail_out = allocated;
 
         for (;;) {
                 ret = lzma_code(&s, LZMA_FINISH);
@@ -370,19 +377,20 @@ int decompress_startswith_xz(const void *src, uint64_t src_size,
                 if (!IN_SET(ret, LZMA_OK, LZMA_STREAM_END))
                         return -EBADMSG;
 
-                if (*buffer_size - s.avail_out >= prefix_len + 1)
+                if (allocated - s.avail_out >= prefix_len + 1)
                         return memcmp(*buffer, prefix, prefix_len) == 0 &&
                                 ((const uint8_t*) *buffer)[prefix_len] == extra;
 
                 if (ret == LZMA_STREAM_END)
                         return 0;
 
-                s.avail_out += *buffer_size;
+                s.avail_out += allocated;
 
-                if (!(greedy_realloc(buffer, buffer_size, *buffer_size * 2, 1)))
+                if (!(greedy_realloc(buffer, allocated * 2, 1)))
                         return -ENOMEM;
 
-                s.next_out = *(uint8_t**)buffer + *buffer_size - s.avail_out;
+                allocated = MALLOC_SIZEOF_SAFE(*buffer);
+                s.next_out = *(uint8_t**)buffer + allocated - s.avail_out;
         }
 
 #else
@@ -390,36 +398,43 @@ int decompress_startswith_xz(const void *src, uint64_t src_size,
 #endif
 }
 
-int decompress_startswith_lz4(const void *src, uint64_t src_size,
-                              void **buffer, size_t *buffer_size,
-                              const void *prefix, size_t prefix_len,
-                              uint8_t extra) {
+int decompress_startswith_lz4(
+                const void *src,
+                uint64_t src_size,
+                void **buffer,
+                const void *prefix,
+                size_t prefix_len,
+                uint8_t extra) {
+
 #if HAVE_LZ4
-        /* Checks whether the decompressed blob starts with the
-         * mentioned prefix. The byte extra needs to follow the
-         * prefix */
+        /* Checks whether the decompressed blob starts with the mentioned prefix. The byte extra needs to
+         * follow the prefix */
 
+        size_t allocated;
         int r;
 
         assert(src);
         assert(src_size > 0);
         assert(buffer);
-        assert(buffer_size);
         assert(prefix);
-        assert(*buffer_size == 0 || *buffer);
 
         if (src_size <= 8)
                 return -EBADMSG;
 
-        if (!(greedy_realloc(buffer, buffer_size, ALIGN_8(prefix_len + 1), 1)))
+        if (!(greedy_realloc(buffer, ALIGN_8(prefix_len + 1), 1)))
                 return -ENOMEM;
-
-        r = LZ4_decompress_safe_partial((char*)src + 8, *buffer, src_size - 8,
-                                        prefix_len + 1, *buffer_size);
-        /* One lz4 < 1.8.3, we might get "failure" (r < 0), or "success" where
-         * just a part of the buffer is decompressed. But if we get a smaller
-         * amount of bytes than requested, we don't know whether there isn't enough
-         * data to fill the requested size or whether we just got a partial answer.
+        allocated = MALLOC_SIZEOF_SAFE(*buffer);
+
+        r = LZ4_decompress_safe_partial(
+                        (char*)src + 8,
+                        *buffer,
+                        src_size - 8,
+                        prefix_len + 1,
+                        allocated);
+
+        /* One lz4 < 1.8.3, we might get "failure" (r < 0), or "success" where just a part of the buffer is
+         * decompressed. But if we get a smaller amount of bytes than requested, we don't know whether there
+         * isn't enough data to fill the requested size or whether we just got a partial answer.
          */
         if (r < 0 || (size_t) r < prefix_len + 1) {
                 size_t size;
@@ -437,7 +452,7 @@ int decompress_startswith_lz4(const void *src, uint64_t src_size,
 
                 /* Before version 1.8.3, lz4 always tries to decode full a "sequence",
                  * so in pathological cases might need to decompress the full field. */
-                r = decompress_blob_lz4(src, src_size, buffer, buffer_size, &size, 0);
+                r = decompress_blob_lz4(src, src_size, buffer, &size, 0);
                 if (r < 0)
                         return r;
 
@@ -453,17 +468,17 @@ int decompress_startswith_lz4(const void *src, uint64_t src_size,
 }
 
 int decompress_startswith_zstd(
-                const void *src, uint64_t src_size,
-                void **buffer, size_t *buffer_size,
-                const void *prefix, size_t prefix_len,
+                const void *src,
+                uint64_t src_size,
+                void **buffer,
+                const void *prefix,
+                size_t prefix_len,
                 uint8_t extra) {
 #if HAVE_ZSTD
         assert(src);
         assert(src_size > 0);
         assert(buffer);
-        assert(buffer_size);
         assert(prefix);
-        assert(*buffer_size == 0 || *buffer);
 
         uint64_t size = ZSTD_getFrameContentSize(src, src_size);
         if (IN_SET(size, ZSTD_CONTENTSIZE_ERROR, ZSTD_CONTENTSIZE_UNKNOWN))
@@ -476,7 +491,7 @@ int decompress_startswith_zstd(
         if (!dctx)
                 return -ENOMEM;
 
-        if (!(greedy_realloc(buffer, buffer_size, MAX(ZSTD_DStreamOutSize(), prefix_len + 1), 1)))
+        if (!(greedy_realloc(buffer, MAX(ZSTD_DStreamOutSize(), prefix_len + 1), 1)))
                 return -ENOMEM;
 
         ZSTD_inBuffer input = {
@@ -485,7 +500,7 @@ int decompress_startswith_zstd(
         };
         ZSTD_outBuffer output = {
                 .dst = *buffer,
-                .size = *buffer_size,
+                .size = MALLOC_SIZEOF_SAFE(*buffer),
         };
         size_t k;
 
@@ -505,28 +520,30 @@ int decompress_startswith_zstd(
 
 int decompress_startswith(
                 int compression,
-                const void *src, uint64_t src_size,
-                void **buffer, size_t *buffer_size,
-                const void *prefix, size_t prefix_len,
+                const void *src,
+                uint64_t src_size,
+                void **buffer,
+                const void *prefix,
+                size_t prefix_len,
                 uint8_t extra) {
 
         if (compression == OBJECT_COMPRESSED_XZ)
                 return decompress_startswith_xz(
                                 src, src_size,
-                                buffer, buffer_size,
+                                buffer,
                                 prefix, prefix_len,
                                 extra);
 
         else if (compression == OBJECT_COMPRESSED_LZ4)
                 return decompress_startswith_lz4(
                                 src, src_size,
-                                buffer, buffer_size,
+                                buffer,
                                 prefix, prefix_len,
                                 extra);
         else if (compression == OBJECT_COMPRESSED_ZSTD)
                 return decompress_startswith_zstd(
                                 src, src_size,
-                                buffer, buffer_size,
+                                buffer,
                                 prefix, prefix_len,
                                 extra);
         else
index db7f3999ed7b06a5faf50eecf175cdc3a9fd8a71..a82049edd8a8f94e30656f2791bd60d3b41b76db 100644 (file)
@@ -37,30 +37,30 @@ static inline int compress_blob(const void *src, uint64_t src_size,
 }
 
 int decompress_blob_xz(const void *src, uint64_t src_size,
-                       void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max);
+                       void **dst, size_t* dst_size, size_t dst_max);
 int decompress_blob_lz4(const void *src, uint64_t src_size,
-                        void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max);
+                        void **dst, size_t* dst_size, size_t dst_max);
 int decompress_blob_zstd(const void *src, uint64_t src_size,
-                        void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max);
+                        void **dst, size_t* dst_size, size_t dst_max);
 int decompress_blob(int compression,
                     const void *src, uint64_t src_size,
-                    void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max);
+                    void **dst, size_t* dst_size, size_t dst_max);
 
 int decompress_startswith_xz(const void *src, uint64_t src_size,
-                             void **buffer, size_t *buffer_size,
+                             void **buffer,
                              const void *prefix, size_t prefix_len,
                              uint8_t extra);
 int decompress_startswith_lz4(const void *src, uint64_t src_size,
-                              void **buffer, size_t *buffer_size,
+                              void **buffer,
                               const void *prefix, size_t prefix_len,
                               uint8_t extra);
 int decompress_startswith_zstd(const void *src, uint64_t src_size,
-                               void **buffer, size_t *buffer_size,
+                               void **buffer,
                                const void *prefix, size_t prefix_len,
                                uint8_t extra);
 int decompress_startswith(int compression,
                           const void *src, uint64_t src_size,
-                          void **buffer, size_t *buffer_size,
+                          void **buffer,
                           const void *prefix, size_t prefix_len,
                           uint8_t extra);
 
index 3cf26f8e44353659e0039e38b9b3c73c5db478a4..5f63633bf1796a3cc0be80a6e7f751cad2cb24ba 100644 (file)
@@ -1495,7 +1495,7 @@ int journal_file_find_data_object_with_hash(
                         l -= offsetof(Object, data.payload);
 
                         r = decompress_blob(o->object.flags & OBJECT_COMPRESSION_MASK,
-                                            o->data.payload, l, &f->compress_buffer, &f->compress_buffer_size, &rsize, 0);
+                                            o->data.payload, l, &f->compress_buffer, &rsize, 0);
                         if (r < 0)
                                 return r;
 
@@ -3919,8 +3919,11 @@ int journal_file_copy_entry(JournalFile *from, JournalFile *to, Object *o, uint6
 #if HAVE_COMPRESSION
                         size_t rsize = 0;
 
-                        r = decompress_blob(o->object.flags & OBJECT_COMPRESSION_MASK,
-                                            o->data.payload, l, &from->compress_buffer, &from->compress_buffer_size, &rsize, 0);
+                        r = decompress_blob(
+                                        o->object.flags & OBJECT_COMPRESSION_MASK,
+                                        o->data.payload, l,
+                                        &from->compress_buffer, &rsize,
+                                        0);
                         if (r < 0)
                                 return r;
 
index 931c8742686ef4163696e1d93000dda4069df9a5..de3df7692d35696f1784fd90358fff8320c45c18 100644 (file)
@@ -108,7 +108,6 @@ typedef struct JournalFile {
         uint64_t compress_threshold_bytes;
 #if HAVE_COMPRESSION
         void *compress_buffer;
-        size_t compress_buffer_size;
 #endif
 
 #if HAVE_GCRYPT
index c2d29aae78ad7458a07de3e85bda9ed5d5c21417..7fc6896522e27df3c73cd52251ba83032b3af1e3 100644 (file)
@@ -99,7 +99,6 @@ struct sd_journal {
         uint64_t fields_offset;
         uint64_t fields_hash_table_index;
         char *fields_buffer;
-        size_t fields_buffer_allocated;
 
         int flags;
 
index 0f1c9eb8f73b19dfb5e7f3690bdbcec9f48d4963..7b57ba5ec3c6cafa5e15e76ce1d0c77a2950d775 100644 (file)
@@ -127,7 +127,7 @@ int journal_directory_vacuum(
                 bool verbose) {
 
         uint64_t sum = 0, freed = 0, n_active_files = 0;
-        size_t n_list = 0, n_allocated = 0, i;
+        size_t n_list = 0, i;
         _cleanup_closedir_ DIR *d = NULL;
         struct vacuum_info *list = NULL;
         usec_t retention_limit = 0;
@@ -262,7 +262,7 @@ int journal_directory_vacuum(
 
                 patch_realtime(dirfd(d), p, &st, &realtime);
 
-                if (!GREEDY_REALLOC(list, n_allocated, n_list + 1)) {
+                if (!GREEDY_REALLOC(list, n_list + 1)) {
                         r = -ENOMEM;
                         goto finish;
                 }
index 8c38396eacd2fe3004eb17c07e5530b9de79938c..6bc0796f05dc9e5f24fb2cdcf411296b605e5670 100644 (file)
@@ -153,12 +153,12 @@ static int journal_file_object_verify(JournalFile *f, uint64_t offset, Object *o
                 compression = o->object.flags & OBJECT_COMPRESSION_MASK;
                 if (compression) {
                         _cleanup_free_ void *b = NULL;
-                        size_t alloc = 0, b_size;
+                        size_t b_size;
 
                         r = decompress_blob(compression,
                                             o->data.payload,
                                             le64toh(o->object.size) - offsetof(Object, data.payload),
-                                            &b, &alloc, &b_size, 0);
+                                            &b, &b_size, 0);
                         if (r < 0) {
                                 error_errno(offset, r, "%s decompression failed: %m",
                                             object_compressed_to_string(compression));
index c90b4c926e6d9abd5ecf627c7023739efcc519ee..fce0ae0f871abe9ba37e6582dbd89d81aacf9dc8 100644 (file)
@@ -2321,7 +2321,7 @@ _public_ int sd_journal_get_data(sd_journal *j, const char *field, const void **
 #if HAVE_COMPRESSION
                         r = decompress_startswith(compression,
                                                   o->data.payload, l,
-                                                  &f->compress_buffer, &f->compress_buffer_size,
+                                                  &f->compress_buffer,
                                                   field, field_length, '=');
                         if (r < 0)
                                 log_debug_errno(r, "Cannot decompress %s object of length %"PRIu64" at offset "OFSfmt": %m",
@@ -2332,7 +2332,7 @@ _public_ int sd_journal_get_data(sd_journal *j, const char *field, const void **
 
                                 r = decompress_blob(compression,
                                                     o->data.payload, l,
-                                                    &f->compress_buffer, &f->compress_buffer_size, &rsize,
+                                                    &f->compress_buffer, &rsize,
                                                     j->data_threshold);
                                 if (r < 0)
                                         return r;
@@ -2389,9 +2389,11 @@ static int return_data(sd_journal *j, JournalFile *f, Object *o, const void **da
                 size_t rsize;
                 int r;
 
-                r = decompress_blob(compression,
-                                    o->data.payload, l, &f->compress_buffer,
-                                    &f->compress_buffer_size, &rsize, j->data_threshold);
+                r = decompress_blob(
+                                compression,
+                                o->data.payload, l,
+                                &f->compress_buffer, &rsize,
+                                j->data_threshold);
                 if (r < 0)
                         return r;
 
@@ -3143,7 +3145,7 @@ _public_ int sd_journal_enumerate_fields(sd_journal *j, const char **field) {
                 if (sz > j->data_threshold)
                         sz = j->data_threshold;
 
-                if (!GREEDY_REALLOC(j->fields_buffer, j->fields_buffer_allocated, sz + 1))
+                if (!GREEDY_REALLOC(j->fields_buffer, sz + 1))
                         return -ENOMEM;
 
                 memcpy(j->fields_buffer, o->field.payload, sz);
index 001976076eddad9e552b93f56d6599e1ab817bb4..eb3459e27eac8d958f483c40d363edb5be2a1fcc 100644 (file)
@@ -15,7 +15,7 @@
 typedef int (compress_t)(const void *src, uint64_t src_size, void *dst,
                          size_t dst_alloc_size, size_t *dst_size);
 typedef int (decompress_t)(const void *src, uint64_t src_size,
-                           void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max);
+                           void **dst, size_t* dst_size, size_t dst_max);
 
 #if HAVE_COMPRESSION
 
@@ -80,7 +80,6 @@ static void test_compress_decompress(const char* label, const char* type,
 
         _cleanup_free_ char *text, *buf;
         _cleanup_free_ void *buf2 = NULL;
-        size_t buf2_allocated = 0;
         size_t skipped = 0, compressed = 0, total = 0;
 
         text = make_buf(MAX_SIZE, type);
@@ -116,9 +115,8 @@ static void test_compress_decompress(const char* label, const char* type,
                 if (j >= size)
                         log_error("%s \"compressed\" %zu -> %zu", label, size, j);
 
-                r = decompress(buf, j, &buf2, &buf2_allocated, &k, 0);
+                r = decompress(buf, j, &buf2, &k, 0);
                 assert_se(r == 0);
-                assert_se(buf2_allocated >= k);
                 assert_se(k == size);
 
                 assert_se(memcmp(text, buf2, size) == 0);
index ccd46050166c2e9085dc1d44220a633ede0c12e2..0d5069d477281f34151ed8f791c15260bf11c016 100644 (file)
 typedef int (compress_blob_t)(const void *src, uint64_t src_size,
                               void *dst, size_t dst_alloc_size, size_t *dst_size);
 typedef int (decompress_blob_t)(const void *src, uint64_t src_size,
-                                void **dst, size_t *dst_alloc_size,
+                                void **dst,
                                 size_t* dst_size, size_t dst_max);
 typedef int (decompress_sw_t)(const void *src, uint64_t src_size,
-                              void **buffer, size_t *buffer_size,
+                              void **buffer,
                               const void *prefix, size_t prefix_len,
                               uint8_t extra);
 
@@ -45,14 +45,16 @@ typedef int (compress_stream_t)(int fdf, int fdt, uint64_t max_bytes);
 typedef int (decompress_stream_t)(int fdf, int fdt, uint64_t max_size);
 
 #if HAVE_COMPRESSION
-_unused_ static void test_compress_decompress(const char *compression,
-                                              compress_blob_t compress,
-                                              decompress_blob_t decompress,
-                                              const char *data,
-                                              size_t data_len,
-                                              bool may_fail) {
+_unused_ static void test_compress_decompress(
+                const char *compression,
+                compress_blob_t compress,
+                decompress_blob_t decompress,
+                const char *data,
+                size_t data_len,
+                bool may_fail) {
+
         char compressed[512];
-        size_t csize, usize = 0;
+        size_t csize;
         _cleanup_free_ char *decompressed = NULL;
         int r;
 
@@ -66,26 +68,26 @@ _unused_ static void test_compress_decompress(const char *compression,
         } else {
                 assert_se(r == 0);
                 r = decompress(compressed, csize,
-                               (void **) &decompressed, &usize, &csize, 0);
+                               (void **) &decompressed, &csize, 0);
                 assert_se(r == 0);
                 assert_se(decompressed);
                 assert_se(memcmp(decompressed, data, data_len) == 0);
         }
 
         r = decompress("garbage", 7,
-                       (void **) &decompressed, &usize, &csize, 0);
+                       (void **) &decompressed, &csize, 0);
         assert_se(r < 0);
 
         /* make sure to have the minimal lz4 compressed size */
         r = decompress("00000000\1g", 9,
-                       (void **) &decompressed, &usize, &csize, 0);
+                       (void **) &decompressed, &csize, 0);
         assert_se(r < 0);
 
         r = decompress("\100000000g", 9,
-                       (void **) &decompressed, &usize, &csize, 0);
+                       (void **) &decompressed, &csize, 0);
         assert_se(r < 0);
 
-        memzero(decompressed, usize);
+        explicit_bzero_safe(decompressed, MALLOC_SIZEOF_SAFE(decompressed));
 }
 
 _unused_ static void test_decompress_startswith(const char *compression,
@@ -97,7 +99,7 @@ _unused_ static void test_decompress_startswith(const char *compression,
 
         char *compressed;
         _cleanup_free_ char *compressed1 = NULL, *compressed2 = NULL, *decompressed = NULL;
-        size_t csize, usize = 0, len;
+        size_t csize, len;
         int r;
 
         log_info("/* testing decompress_startswith with %s on %.20s text */",
@@ -122,17 +124,17 @@ _unused_ static void test_decompress_startswith(const char *compression,
 
         len = strlen(data);
 
-        r = decompress_sw(compressed, csize, (void **) &decompressed, &usize, data, len, '\0');
+        r = decompress_sw(compressed, csize, (void **) &decompressed, data, len, '\0');
         assert_se(r > 0);
-        r = decompress_sw(compressed, csize, (void **) &decompressed, &usize, data, len, 'w');
+        r = decompress_sw(compressed, csize, (void **) &decompressed, data, len, 'w');
         assert_se(r == 0);
-        r = decompress_sw(compressed, csize, (void **) &decompressed, &usize, "barbarbar", 9, ' ');
+        r = decompress_sw(compressed, csize, (void **) &decompressed, "barbarbar", 9, ' ');
         assert_se(r == 0);
-        r = decompress_sw(compressed, csize, (void **) &decompressed, &usize, data, len - 1, data[len-1]);
+        r = decompress_sw(compressed, csize, (void **) &decompressed, data, len - 1, data[len-1]);
         assert_se(r > 0);
-        r = decompress_sw(compressed, csize, (void **) &decompressed, &usize, data, len - 1, 'w');
+        r = decompress_sw(compressed, csize, (void **) &decompressed, data, len - 1, 'w');
         assert_se(r == 0);
-        r = decompress_sw(compressed, csize, (void **) &decompressed, &usize, data, len, '\0');
+        r = decompress_sw(compressed, csize, (void **) &decompressed, data, len, '\0');
         assert_se(r > 0);
 }
 
@@ -152,13 +154,12 @@ _unused_ static void test_decompress_startswith_short(const char *compression,
         assert_se(r == 0);
 
         for (i = 1; i < strlen(TEXT); i++) {
-                size_t alloc_size = i;
                 _cleanup_free_ void *buf2 = NULL;
 
                 assert_se(buf2 = malloc(i));
 
-                assert_se(decompress_sw(buf, csize, &buf2, &alloc_size, TEXT, i, TEXT[i]) == 1);
-                assert_se(decompress_sw(buf, csize, &buf2, &alloc_size, TEXT, i, 'y') == 0);
+                assert_se(decompress_sw(buf, csize, &buf2, TEXT, i, TEXT[i]) == 1);
+                assert_se(decompress_sw(buf, csize, &buf2, TEXT, i, 'y') == 0);
         }
 }
 
index fd7f07a6c1ff384092c493c13b611b1fd297b68d..49c7dd773dce934d683f6945f179cc3cfc02bfc9 100644 (file)
@@ -71,14 +71,11 @@ struct sd_netlink {
 
         sd_netlink_message **rqueue;
         unsigned rqueue_size;
-        size_t rqueue_allocated;
 
         sd_netlink_message **rqueue_partial;
         unsigned rqueue_partial_size;
-        size_t rqueue_partial_allocated;
 
         struct nlmsghdr *rbuffer;
-        size_t rbuffer_allocated;
 
         bool processing:1;
 
index d7bc1ce14dab1d65033b06f019b1cbcdc92564bc..22f8a4aa7390e8c156a344f209a4ff7e8ca88fc8 100644 (file)
@@ -1064,7 +1064,7 @@ static int netlink_container_parse(sd_netlink_message *m,
                                    struct rtattr *rta,
                                    size_t rt_len) {
         _cleanup_free_ struct netlink_attribute *attributes = NULL;
-        size_t n_allocated = 0;
+        size_t n = 0;
 
         /* RTA_OK() macro compares with rta->rt_len, which is unsigned short, and
          * LGTM.com analysis does not like the type difference. Hence, here we
@@ -1075,7 +1075,7 @@ static int netlink_container_parse(sd_netlink_message *m,
 
                 type = RTA_TYPE(rta);
 
-                if (!GREEDY_REALLOC0(attributes, n_allocated, type + 1))
+                if (!GREEDY_REALLOC0(attributes, type + 1))
                         return -ENOMEM;
 
                 if (attributes[type].offset != 0)
@@ -1084,10 +1084,13 @@ static int netlink_container_parse(sd_netlink_message *m,
                 attributes[type].offset = (uint8_t *) rta - (uint8_t *) m->hdr;
                 attributes[type].nested = RTA_FLAGS(rta) & NLA_F_NESTED;
                 attributes[type].net_byteorder = RTA_FLAGS(rta) & NLA_F_NET_BYTEORDER;
+
+                if (type + 1U > n)
+                        n = type + 1U;
         }
 
         container->attributes = TAKE_PTR(attributes);
-        container->n_attributes = n_allocated;
+        container->n_attributes = n;
 
         return 0;
 }
index 7af9a94e3e1d14d8850d474ff0ee3675f257eea6..273af6383e5e5b0ece7abba7474aebf9e4f717e7 100644 (file)
@@ -1,5 +1,6 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
+#include <malloc.h>
 #include <netinet/in.h>
 #include <stdbool.h>
 #include <unistd.h>
@@ -297,16 +298,15 @@ static int socket_recv_message(int fd, struct iovec *iov, uint32_t *ret_mcast_gr
  */
 int socket_read_message(sd_netlink *rtnl) {
         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *first = NULL;
+        bool multi_part = false, done = false;
+        size_t len, allocated;
         struct iovec iov = {};
         uint32_t group = 0;
-        bool multi_part = false, done = false;
-        size_t len;
-        int r;
         unsigned i = 0;
+        int r;
 
         assert(rtnl);
         assert(rtnl->rbuffer);
-        assert(rtnl->rbuffer_allocated >= sizeof(struct nlmsghdr));
 
         /* read nothing, just get the pending message size */
         r = socket_recv_message(rtnl->fd, &iov, NULL, true);
@@ -316,12 +316,11 @@ int socket_read_message(sd_netlink *rtnl) {
                 len = (size_t) r;
 
         /* make room for the pending message */
-        if (!greedy_realloc((void **)&rtnl->rbuffer,
-                            &rtnl->rbuffer_allocated,
-                            len, sizeof(uint8_t)))
+        if (!greedy_realloc((void **)&rtnl->rbuffer, len, sizeof(uint8_t)))
                 return -ENOMEM;
 
-        iov = IOVEC_MAKE(rtnl->rbuffer, rtnl->rbuffer_allocated);
+        allocated = MALLOC_SIZEOF_SAFE(rtnl->rbuffer);
+        iov = IOVEC_MAKE(rtnl->rbuffer, allocated);
 
         /* read the pending message */
         r = socket_recv_message(rtnl->fd, &iov, &group, false);
@@ -330,7 +329,7 @@ int socket_read_message(sd_netlink *rtnl) {
         else
                 len = (size_t) r;
 
-        if (len > rtnl->rbuffer_allocated)
+        if (len > allocated)
                 /* message did not fit in read buffer */
                 return -EIO;
 
index a003c25719c0fbde8c79349cbeccbd9429ca88d6..25ba700ee20c06880d461aa8ce2c6b9053b47636 100644 (file)
@@ -63,8 +63,7 @@ static int sd_netlink_new(sd_netlink **ret) {
 
         /* We guarantee that the read buffer has at least space for
          * a message header */
-        if (!greedy_realloc((void**)&rtnl->rbuffer, &rtnl->rbuffer_allocated,
-                            sizeof(struct nlmsghdr), sizeof(uint8_t)))
+        if (!greedy_realloc((void**)&rtnl->rbuffer, sizeof(struct nlmsghdr), sizeof(uint8_t)))
                 return -ENOMEM;
 
         *ret = TAKE_PTR(rtnl);
@@ -295,7 +294,7 @@ int rtnl_rqueue_make_room(sd_netlink *rtnl) {
                                        "rtnl: exhausted the read queue size (%d)",
                                        RTNL_RQUEUE_MAX);
 
-        if (!GREEDY_REALLOC(rtnl->rqueue, rtnl->rqueue_allocated, rtnl->rqueue_size + 1))
+        if (!GREEDY_REALLOC(rtnl->rqueue, rtnl->rqueue_size + 1))
                 return -ENOMEM;
 
         return 0;
@@ -309,8 +308,7 @@ int rtnl_rqueue_partial_make_room(sd_netlink *rtnl) {
                                        "rtnl: exhausted the partial read queue size (%d)",
                                        RTNL_RQUEUE_MAX);
 
-        if (!GREEDY_REALLOC(rtnl->rqueue_partial, rtnl->rqueue_partial_allocated,
-                            rtnl->rqueue_partial_size + 1))
+        if (!GREEDY_REALLOC(rtnl->rqueue_partial, rtnl->rqueue_partial_size + 1))
                 return -ENOMEM;
 
         return 0;
index 552ec381b674352550ce52590a8b015601b17618..7273a77156a1dd3128a996bce6f3e1db73971cb1 100644 (file)
@@ -340,7 +340,7 @@ static int network_link_get_ifindexes(int ifindex, const char *key, int **ret) {
         char path[STRLEN("/run/systemd/netif/links/") + DECIMAL_STR_MAX(ifindex) + 1];
         _cleanup_free_ int *ifis = NULL;
         _cleanup_free_ char *s = NULL;
-        size_t allocated = 0, c = 0;
+        size_t c = 0;
         int r;
 
         assert_return(ifindex > 0, -EINVAL);
@@ -362,7 +362,7 @@ static int network_link_get_ifindexes(int ifindex, const char *key, int **ret) {
                 if (r == 0)
                         break;
 
-                if (!GREEDY_REALLOC(ifis, allocated, c + 2))
+                if (!GREEDY_REALLOC(ifis, c + 2))
                         return -ENOMEM;
 
                 r = ifis[c++] = parse_ifindex(word);
index 3eb0e8fd4e7f29bcd26b86674ba293d4c9a55d95..61353c7f1749364ae41b4428cb0da9d28f87d6a2 100644 (file)
@@ -828,7 +828,7 @@ static int method_create_session(sd_bus_message *message, void *userdata, sd_bus
          */
         if (c != SESSION_GREETER &&
             vtnr > 0 &&
-            vtnr < m->seat0->position_count &&
+            vtnr < MALLOC_ELEMENTSOF(m->seat0->positions) &&
             m->seat0->positions[vtnr] &&
             m->seat0->positions[vtnr]->class != SESSION_GREETER)
                 return sd_bus_error_set(error, BUS_ERROR_SESSION_BUSY, "Already occupied by a session");
index b70afea62e57dc3e021fd59517eac372e7f505e4..2d546817457187789df72e6e970d0097a4697f1b 100644 (file)
@@ -267,13 +267,25 @@ int seat_set_active(Seat *s, Session *session) {
         return 0;
 }
 
+static Session* seat_get_position(Seat *s, unsigned pos) {
+        assert(s);
+
+        if (pos >= MALLOC_ELEMENTSOF(s->positions))
+                return NULL;
+
+        return s->positions[pos];
+}
+
 int seat_switch_to(Seat *s, unsigned num) {
+        Session *session;
+
         /* Public session positions skip 0 (there is only F1-F12). Maybe it
          * will get reassigned in the future, so return error for now. */
         if (num == 0)
                 return -EINVAL;
 
-        if (num >= s->position_count || !s->positions[num]) {
+        session = seat_get_position(s, num);
+        if (!session) {
                 /* allow switching to unused VTs to trigger auto-activate */
                 if (seat_has_vts(s) && num < 64)
                         return chvt(num);
@@ -281,47 +293,57 @@ int seat_switch_to(Seat *s, unsigned num) {
                 return -EINVAL;
         }
 
-        return session_activate(s->positions[num]);
+        return session_activate(session);
 }
 
 int seat_switch_to_next(Seat *s) {
         unsigned start, i;
+        Session *session;
 
-        if (s->position_count == 0)
+        if (MALLOC_ELEMENTSOF(s->positions) == 0)
                 return -EINVAL;
 
         start = 1;
         if (s->active && s->active->position > 0)
                 start = s->active->position;
 
-        for (i = start + 1; i < s->position_count; ++i)
-                if (s->positions[i])
-                        return session_activate(s->positions[i]);
+        for (i = start + 1; i < MALLOC_ELEMENTSOF(s->positions); ++i) {
+                session = seat_get_position(s, i);
+                if (session)
+                        return session_activate(session);
+        }
 
-        for (i = 1; i < start; ++i)
-                if (s->positions[i])
-                        return session_activate(s->positions[i]);
+        for (i = 1; i < start; ++i) {
+                session = seat_get_position(s, i);
+                if (session)
+                        return session_activate(session);
+        }
 
         return -EINVAL;
 }
 
 int seat_switch_to_previous(Seat *s) {
         unsigned start, i;
+        Session *session;
 
-        if (s->position_count == 0)
+        if (MALLOC_ELEMENTSOF(s->positions) == 0)
                 return -EINVAL;
 
         start = 1;
         if (s->active && s->active->position > 0)
                 start = s->active->position;
 
-        for (i = start - 1; i > 0; --i)
-                if (s->positions[i])
-                        return session_activate(s->positions[i]);
+        for (i = start - 1; i > 0; --i) {
+                session = seat_get_position(s, i);
+                if (session)
+                        return session_activate(session);
+        }
 
-        for (i = s->position_count - 1; i > start; --i)
-                if (s->positions[i])
-                        return session_activate(s->positions[i]);
+        for (i = MALLOC_ELEMENTSOF(s->positions) - 1; i > start; --i) {
+                session = seat_get_position(s, i);
+                if (session)
+                        return session_activate(session);
+        }
 
         return -EINVAL;
 }
@@ -468,7 +490,7 @@ void seat_evict_position(Seat *s, Session *session) {
         if (pos == 0)
                 return;
 
-        if (pos < s->position_count && s->positions[pos] == session) {
+        if (pos < MALLOC_ELEMENTSOF(s->positions) && s->positions[pos] == session) {
                 s->positions[pos] = NULL;
 
                 /* There might be another session claiming the same
@@ -488,7 +510,7 @@ void seat_claim_position(Seat *s, Session *session, unsigned pos) {
         if (seat_has_vts(s))
                 pos = session->vtnr;
 
-        if (!GREEDY_REALLOC0(s->positions, s->position_count, pos + 1))
+        if (!GREEDY_REALLOC0(s->positions, pos + 1))
                 return;
 
         seat_evict_position(s, session);
@@ -504,7 +526,7 @@ static void seat_assign_position(Seat *s, Session *session) {
         if (session->position > 0)
                 return;
 
-        for (pos = 1; pos < s->position_count; ++pos)
+        for (pos = 1; pos < MALLOC_ELEMENTSOF(s->positions); ++pos)
                 if (!s->positions[pos])
                         break;
 
index ddc45f48703df1fb9d56b2e65dd87e2ab05c7845..2d18e7595ace6931b68d42aa42208f91e340a1e6 100644 (file)
@@ -19,7 +19,6 @@ struct Seat {
         LIST_HEAD(Session, sessions);
 
         Session **positions;
-        size_t position_count;
 
         bool in_gc_queue:1;
         bool started:1;
index 6215b29c2768d54f51d48f3c47a23af7724a122b..c0ed24b6452eb051edba21210a1475c9ffbe639e 100644 (file)
@@ -292,9 +292,9 @@ int machine_load(Machine *m) {
                 (void) deserialize_usec(monotonic, &m->timestamp.monotonic);
 
         if (netif) {
-                size_t allocated = 0, nr = 0;
-                const char *p;
                 _cleanup_free_ int *ni = NULL;
+                size_t nr = 0;
+                const char *p;
 
                 p = netif;
                 for (;;) {
@@ -314,14 +314,13 @@ int machine_load(Machine *m) {
                         if (r < 0)
                                 continue;
 
-                        if (!GREEDY_REALLOC(ni, allocated, nr + 1))
+                        if (!GREEDY_REALLOC(ni, nr + 1))
                                 return log_oom();
 
                         ni[nr++] = r;
                 }
 
-                free(m->netif);
-                m->netif = TAKE_PTR(ni);
+                free_and_replace(m->netif, ni);
                 m->n_netif = nr;
         }
 
index 15f7d39921ec7bbd3c227f3c3cb943d093325e96..beab50e3158fd17fe7f90df7b31b6acd4328d9f1 100644 (file)
@@ -2261,10 +2261,10 @@ static int list_transfers(int argc, char *argv[], void *userdata) {
         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
         _cleanup_free_ TransferInfo *transfers = NULL;
-        size_t n_transfers = 0, n_allocated = 0;
         const char *type, *remote, *local;
         sd_bus *bus = userdata;
         uint32_t id, max_id = 0;
+        size_t n_transfers = 0;
         double progress;
         int r;
 
@@ -2281,7 +2281,7 @@ static int list_transfers(int argc, char *argv[], void *userdata) {
         while ((r = sd_bus_message_read(reply, "(usssdo)", &id, &type, &remote, &local, &progress, NULL)) > 0) {
                 size_t l;
 
-                if (!GREEDY_REALLOC(transfers, n_allocated, n_transfers + 1))
+                if (!GREEDY_REALLOC(transfers, n_transfers + 1))
                         return log_oom();
 
                 transfers[n_transfers].id = id;
index 8c36dec12a6cef0da5127726356f84ccaafee183..2e69a99bc6ff4ab8f2cda30655f33330d5cdac74 100644 (file)
@@ -723,7 +723,7 @@ static int find_mount_points(const char *what, char ***list) {
         _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
         _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
         _cleanup_strv_free_ char **l = NULL;
-        size_t bufsize = 0, n = 0;
+        size_t n = 0;
         int r;
 
         assert(what);
@@ -755,7 +755,7 @@ static int find_mount_points(const char *what, char ***list) {
                         continue;
 
                 /* one extra slot is needed for the terminating NULL */
-                if (!GREEDY_REALLOC0(l, bufsize, n + 2))
+                if (!GREEDY_REALLOC0(l, n + 2))
                         return log_oom();
 
                 l[n] = strdup(target);
@@ -764,7 +764,7 @@ static int find_mount_points(const char *what, char ***list) {
                 n++;
         }
 
-        if (!GREEDY_REALLOC0(l, bufsize, n + 1))
+        if (!GREEDY_REALLOC0(l, n + 1))
                 return log_oom();
 
         *list = TAKE_PTR(l);
index f36d725d7d7b448e0ff3c6c100f9058416f0e44e..b4e3bfc7ae0619bb298f220efcc370fe66b06a06 100644 (file)
@@ -716,7 +716,7 @@ static int acquire_link_info(sd_bus *bus, sd_netlink *rtnl, char **patterns, Lin
         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
         _cleanup_(link_info_array_freep) LinkInfo *links = NULL;
         _cleanup_close_ int fd = -1;
-        size_t allocated = 0, c = 0;
+        size_t c = 0;
         int r;
 
         assert(rtnl);
@@ -742,7 +742,7 @@ static int acquire_link_info(sd_bus *bus, sd_netlink *rtnl, char **patterns, Lin
         }
 
         for (sd_netlink_message *i = reply; i; i = sd_netlink_message_next(i)) {
-                if (!GREEDY_REALLOC0(links, allocated, c + 2)) /* We keep one trailing one as marker */
+                if (!GREEDY_REALLOC0(links, c + 2)) /* We keep one trailing one as marker */
                         return -ENOMEM;
 
                 r = decode_link(i, links + c, patterns, matched_patterns);
index 0a396a957f70483245cf875df20caf86381b7ffd..36707b6e8f0168a7e2548133d5b5b8ab95a747fe 100644 (file)
@@ -101,8 +101,8 @@ static int link_push_uplink_to_dhcp_server(
                 sd_dhcp_server *s) {
 
         _cleanup_free_ struct in_addr *addresses = NULL;
-        size_t n_addresses = 0, n_allocated = 0;
         bool use_dhcp_lease_data = true;
+        size_t n_addresses = 0;
 
         assert(link);
 
@@ -131,7 +131,7 @@ static int link_push_uplink_to_dhcp_server(
                         if (in4_addr_is_null(&ia) || in4_addr_is_localhost(&ia))
                                 continue;
 
-                        if (!GREEDY_REALLOC(addresses, n_allocated, n_addresses + 1))
+                        if (!GREEDY_REALLOC(addresses, n_addresses + 1))
                                 return log_oom();
 
                         addresses[n_addresses++] = ia;
@@ -156,7 +156,7 @@ static int link_push_uplink_to_dhcp_server(
                         if (in4_addr_is_null(&ia.in) || in4_addr_is_localhost(&ia.in))
                                 continue;
 
-                        if (!GREEDY_REALLOC(addresses, n_allocated, n_addresses + 1))
+                        if (!GREEDY_REALLOC(addresses, n_addresses + 1))
                                 return log_oom();
 
                         addresses[n_addresses++] = ia.in;
@@ -188,7 +188,7 @@ static int link_push_uplink_to_dhcp_server(
 
                 int n = sd_dhcp_lease_get_servers(link->dhcp_lease, what, &da);
                 if (n > 0) {
-                        if (!GREEDY_REALLOC(addresses, n_allocated, n_addresses + n))
+                        if (!GREEDY_REALLOC(addresses, n_addresses + n))
                                 return log_oom();
 
                         for (int j = 0; j < n; j++)
@@ -203,7 +203,11 @@ static int link_push_uplink_to_dhcp_server(
         return sd_dhcp_server_set_servers(s, what, addresses, n_addresses);
 }
 
-static int dhcp4_server_parse_dns_server_string_and_warn(Link *l, const char *string, struct in_addr **addresses, size_t *n_allocated, size_t *n_addresses) {
+static int dhcp4_server_parse_dns_server_string_and_warn(
+                const char *string,
+                struct in_addr **addresses,
+                size_t *n_addresses) {
+
         for (;;) {
                 _cleanup_free_ char *word = NULL, *server_name = NULL;
                 union in_addr_union address;
@@ -229,7 +233,7 @@ static int dhcp4_server_parse_dns_server_string_and_warn(Link *l, const char *st
                 if (in4_addr_is_null(&address.in) || in4_addr_is_localhost(&address.in))
                         continue;
 
-                if (!GREEDY_REALLOC(*addresses, *n_allocated, *n_addresses + 1))
+                if (!GREEDY_REALLOC(*addresses, *n_addresses + 1))
                         return log_oom();
 
                 (*addresses)[(*n_addresses)++] = address.in;
@@ -240,8 +244,8 @@ static int dhcp4_server_parse_dns_server_string_and_warn(Link *l, const char *st
 
 static int dhcp4_server_set_dns_from_resolve_conf(Link *link) {
         _cleanup_free_ struct in_addr *addresses = NULL;
-        size_t n_addresses = 0, n_allocated = 0;
         _cleanup_fclose_ FILE *f = NULL;
+        size_t n_addresses = 0;
         int n = 0, r;
 
         f = fopen(PRIVATE_UPLINK_RESOLV_CONF, "re");
@@ -273,7 +277,7 @@ static int dhcp4_server_set_dns_from_resolve_conf(Link *link) {
                 if (!a)
                         continue;
 
-                r = dhcp4_server_parse_dns_server_string_and_warn(link, a, &addresses, &n_allocated, &n_addresses);
+                r = dhcp4_server_parse_dns_server_string_and_warn(a, &addresses, &n_addresses);
                 if (r < 0)
                         log_warning_errno(r, "Failed to parse DNS server address '%s', ignoring.", a);
         }
index 52c9afcc5e3eb003b0746e6dd0a120321c920f37..0eaa4c29c99c5b4231f7cfc417ace20454d1452a 100644 (file)
@@ -515,7 +515,7 @@ int config_parse_route_prefix_lifetime(
 
 static int network_get_ipv6_dns(Network *network, struct in6_addr **ret_addresses, size_t *ret_size) {
         _cleanup_free_ struct in6_addr *addresses = NULL;
-        size_t n_addresses = 0, n_allocated = 0;
+        size_t n_addresses = 0;
 
         assert(network);
         assert(ret_addresses);
@@ -534,7 +534,7 @@ static int network_get_ipv6_dns(Network *network, struct in6_addr **ret_addresse
                     in_addr_is_localhost(AF_INET6, addr))
                         continue;
 
-                if (!GREEDY_REALLOC(addresses, n_allocated, n_addresses + 1))
+                if (!GREEDY_REALLOC(addresses, n_addresses + 1))
                         return -ENOMEM;
 
                 addresses[n_addresses++] = addr->in6;
index 09e7b99d239943efccb701c5502eafe4bdc26074..d04a7e35e8efafe8659caea66a7f274551902ae9 100644 (file)
@@ -46,11 +46,11 @@ static void test_deserialize_in_addr(void) {
 }
 
 static void test_deserialize_dhcp_routes(void) {
-        size_t size, allocated;
+        size_t size;
 
         {
                 _cleanup_free_ struct sd_dhcp_route *routes = NULL;
-                assert_se(deserialize_dhcp_routes(&routes, &size, &allocated, "") >= 0);
+                assert_se(deserialize_dhcp_routes(&routes, &size, "") >= 0);
                 assert_se(size == 0);
         }
 
@@ -59,7 +59,7 @@ static void test_deserialize_dhcp_routes(void) {
                 _cleanup_free_ struct sd_dhcp_route *routes = NULL;
                 const char *routes_string = "192.168.0.0/16,192.168.0.1 10.1.2.0/24,10.1.2.1 0.0.0.0/0,10.0.1.1";
 
-                assert_se(deserialize_dhcp_routes(&routes, &size, &allocated, routes_string) >= 0);
+                assert_se(deserialize_dhcp_routes(&routes, &size, routes_string) >= 0);
 
                 assert_se(size == 3);
                 assert_se(routes[0].dst_addr.s_addr == inet_addr("192.168.0.0"));
@@ -80,7 +80,7 @@ static void test_deserialize_dhcp_routes(void) {
                 _cleanup_free_ struct sd_dhcp_route *routes = NULL;
                 const char *routes_string = "192.168.0.0/16,192.168.0.1 10.1.2.0#24,10.1.2.1 0.0.0.0/0,10.0.1.1";
 
-                assert_se(deserialize_dhcp_routes(&routes, &size, &allocated, routes_string) >= 0);
+                assert_se(deserialize_dhcp_routes(&routes, &size, routes_string) >= 0);
 
                 assert_se(size == 2);
                 assert_se(routes[0].dst_addr.s_addr == inet_addr("192.168.0.0"));
@@ -97,7 +97,7 @@ static void test_deserialize_dhcp_routes(void) {
                 _cleanup_free_ struct sd_dhcp_route *routes = NULL;
                 const char *routes_string = "192.168.0.0/55,192.168.0.1 10.1.2.0#24,10.1.2.1 0.0.0.0/0,10.0.1.X";
 
-                assert_se(deserialize_dhcp_routes(&routes, &size, &allocated, routes_string) >= 0);
+                assert_se(deserialize_dhcp_routes(&routes, &size, routes_string) >= 0);
                 assert_se(size == 0);
         }
 }
index c224fd0b2f089c907c0f73e66eb1eac32e606032..2639b70934c8a9659cf60c343a0438097410d442 100644 (file)
@@ -96,7 +96,6 @@ int change_uid_gid(const char *user, bool chown_stdio, char **ret_home) {
         _cleanup_fclose_ FILE *f = NULL;
         _cleanup_close_ int fd = -1;
         unsigned n_gids = 0;
-        size_t sz = 0;
         uid_t uid;
         gid_t gid;
         pid_t pid;
@@ -219,7 +218,7 @@ int change_uid_gid(const char *user, bool chown_stdio, char **ret_home) {
                 if (r == 0)
                         break;
 
-                if (!GREEDY_REALLOC(gids, sz, n_gids+1))
+                if (!GREEDY_REALLOC(gids, n_gids+1))
                         return log_oom();
 
                 r = parse_gid(word, &gids[n_gids++]);
index 1ecd9ffea434af76c1751a766b74b7f08d201a7a..341cae33a6c62384072f5f44584828dfd40c1f83 100644 (file)
@@ -189,7 +189,7 @@ struct Context {
         size_t n_partitions;
 
         FreeArea **free_areas;
-        size_t n_free_areas, n_allocated_free_areas;
+        size_t n_free_areas;
 
         uint64_t start, end, total;
 
@@ -312,7 +312,6 @@ static void context_free_free_areas(Context *context) {
 
         context->free_areas = mfree(context->free_areas);
         context->n_free_areas = 0;
-        context->n_allocated_free_areas = 0;
 }
 
 static Context *context_free(Context *context) {
@@ -343,7 +342,7 @@ static int context_add_free_area(
         assert(context);
         assert(!after || !after->padding_area);
 
-        if (!GREEDY_REALLOC(context->free_areas, context->n_allocated_free_areas, context->n_free_areas + 1))
+        if (!GREEDY_REALLOC(context->free_areas, context->n_free_areas + 1))
                 return -ENOMEM;
 
         a = new(FreeArea, 1);
index 595fe8a60a4fd7a9fec381d972ce3f6a85194295..23c6e2633a99496f8288e00855ae7adadfde19fd 100644 (file)
@@ -1073,8 +1073,8 @@ not_found:
 int bus_image_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
         _cleanup_hashmap_free_ Hashmap *images = NULL;
         _cleanup_strv_free_ char **l = NULL;
-        size_t n_allocated = 0, n = 0;
         Manager *m = userdata;
+        size_t n = 0;
         Image *image;
         int r;
 
@@ -1097,7 +1097,7 @@ int bus_image_node_enumerator(sd_bus *bus, const char *path, void *userdata, cha
                 if (r < 0)
                         return r;
 
-                if (!GREEDY_REALLOC(l, n_allocated, n+2)) {
+                if (!GREEDY_REALLOC(l, n+2)) {
                         free(p);
                         return -ENOMEM;
                 }
index 6ce38f10a3e08dcb653a85b59379898be1a372b8..81538d9cb3e1c45cff29594b51a6f1b04e92d811 100644 (file)
@@ -101,7 +101,6 @@ typedef struct PStoreEntry {
 
 typedef struct PStoreList {
         PStoreEntry *entries;
-        size_t n_allocated;
         size_t n_entries;
 } PStoreList;
 
@@ -208,7 +207,7 @@ static int write_dmesg(const char *dmesg, size_t size, const char *id) {
 static void process_dmesg_files(PStoreList *list) {
         /* Move files, reconstruct dmesg.txt */
         _cleanup_free_ char *dmesg = NULL, *dmesg_id = NULL;
-        size_t dmesg_size = 0, dmesg_allocated = 0;
+        size_t dmesg_size = 0;
         bool dmesg_bad = false;
         PStoreEntry *pe;
 
@@ -303,7 +302,7 @@ static void process_dmesg_files(PStoreList *list) {
                 /* Reconstruction of dmesg is done as a useful courtesy: do not fail, but don't write garbled
                  * output either. */
                 size_t needed = strlen(pe->dirent.d_name) + strlen(":\n") + pe->content_size + 1;
-                if (!GREEDY_REALLOC(dmesg, dmesg_allocated, dmesg_size + needed)) {
+                if (!GREEDY_REALLOC(dmesg, dmesg_size + needed)) {
                         log_oom();
                         dmesg_bad = true;
                         continue;
@@ -348,7 +347,7 @@ static int list_files(PStoreList *list, const char *sourcepath) {
                         continue;
                 }
 
-                if (!GREEDY_REALLOC(list->entries, list->n_allocated, list->n_entries + 1))
+                if (!GREEDY_REALLOC(list->entries, list->n_entries + 1))
                         return log_oom();
 
                 list->entries[list->n_entries++] = (PStoreEntry) {
index 52bbae3293b8283f5e25468e6e7f3dccc4029b45..e652c82ffcb19d3d2253c19eb7f9e45b3c8e07f2 100644 (file)
@@ -1963,7 +1963,7 @@ static int status_all(sd_bus *bus, StatusMode mode) {
                 return log_error_errno(r, "Failed to enumerate links: %m");
 
         _cleanup_free_ InterfaceInfo *infos = NULL;
-        size_t n_allocated = 0, n_infos = 0;
+        size_t n_infos = 0;
 
         for (sd_netlink_message *i = reply; i; i = sd_netlink_message_next(i)) {
                 const char *name;
@@ -1988,7 +1988,7 @@ static int status_all(sd_bus *bus, StatusMode mode) {
                 if (r < 0)
                         return rtnl_log_parse_error(r);
 
-                if (!GREEDY_REALLOC(infos, n_allocated, n_infos + 1))
+                if (!GREEDY_REALLOC(infos, n_infos + 1))
                         return log_oom();
 
                 infos[n_infos++] = (InterfaceInfo) { ifindex, name };
index c93b7c1ddeeca47a36375ed57c13165d9d8db476..b37f57fe676a7ed3708a9dfc18aca669f9cc14a8 100644 (file)
@@ -1445,8 +1445,8 @@ int dns_packet_read_name(
         _cleanup_(rewind_dns_packet) DnsPacketRewinder rewinder;
         size_t after_rindex = 0, jump_barrier;
         _cleanup_free_ char *name = NULL;
-        size_t n = 0, allocated = 0;
         bool first = true;
+        size_t n = 0;
         int r;
 
         assert(p);
@@ -1475,7 +1475,7 @@ int dns_packet_read_name(
                         if (r < 0)
                                 return r;
 
-                        if (!GREEDY_REALLOC(name, allocated, n + !first + DNS_LABEL_ESCAPED_MAX))
+                        if (!GREEDY_REALLOC(name, n + !first + DNS_LABEL_ESCAPED_MAX))
                                 return -ENOMEM;
 
                         if (first)
@@ -1511,7 +1511,7 @@ int dns_packet_read_name(
                         return -EBADMSG;
         }
 
-        if (!GREEDY_REALLOC(name, allocated, n + 1))
+        if (!GREEDY_REALLOC(name, n + 1))
                 return -ENOMEM;
 
         name[n] = 0;
index 65b815c43c34e25a25e8ca7fb6592c816e10f402..9af3a27bb10b0961e2422ad818f90e76544e1cf1 100644 (file)
@@ -147,7 +147,7 @@ static int parse_line(EtcHosts *hosts, unsigned nr, const char *line) {
                         bn->name = TAKE_PTR(name);
                 }
 
-                if (!GREEDY_REALLOC(bn->addresses, bn->n_allocated, bn->n_addresses + 1))
+                if (!GREEDY_REALLOC(bn->addresses, bn->n_addresses + 1))
                         return log_oom();
 
                 bn->addresses[bn->n_addresses++] = &item->address;
index 459b310185298aa7cdf20dc0c5443484eabc2a4e..c85926a3dcb155e03d48cf49edf2cc5764217406 100644 (file)
@@ -15,7 +15,7 @@ typedef struct EtcHostsItemByName {
         char *name;
 
         struct in_addr_data **addresses;
-        size_t n_addresses, n_allocated;
+        size_t n_addresses;
 } EtcHostsItemByName;
 
 int etc_hosts_parse(EtcHosts *hosts, FILE *f);
index 045952e825d4a1cc2ff24c631e12136552f34999..6706885ebd0e63b1f74521c0c8d8b1566157746f 100644 (file)
@@ -1,6 +1,7 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
 #include <arpa/inet.h>
+#include <malloc.h>
 #include <netinet/in.h>
 #include <sys/socket.h>
 
@@ -77,19 +78,19 @@ static void test_parse_etc_hosts(void) {
         EtcHostsItemByName *bn;
         assert_se(bn = hashmap_get(hosts.by_name, "some.where"));
         assert_se(bn->n_addresses == 3);
-        assert_se(bn->n_allocated >= 3);
+        assert_se(MALLOC_ELEMENTSOF(bn->addresses) >= 3);
         assert_se(address_equal_4(bn->addresses[0], inet_addr("1.2.3.4")));
         assert_se(address_equal_4(bn->addresses[1], inet_addr("1.2.3.5")));
         assert_se(address_equal_6(bn->addresses[2], {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5}));
 
         assert_se(bn = hashmap_get(hosts.by_name, "dash"));
         assert_se(bn->n_addresses == 1);
-        assert_se(bn->n_allocated >= 1);
+        assert_se(MALLOC_ELEMENTSOF(bn->addresses) >= 1);
         assert_se(address_equal_4(bn->addresses[0], inet_addr("1.2.3.6")));
 
         assert_se(bn = hashmap_get(hosts.by_name, "dash-dash.where-dash"));
         assert_se(bn->n_addresses == 1);
-        assert_se(bn->n_allocated >= 1);
+        assert_se(MALLOC_ELEMENTSOF(bn->addresses) >= 1);
         assert_se(address_equal_4(bn->addresses[0], inet_addr("1.2.3.6")));
 
         /* See https://tools.ietf.org/html/rfc1035#section-2.3.1 */
@@ -98,7 +99,7 @@ static void test_parse_etc_hosts(void) {
 
         assert_se(bn = hashmap_get(hosts.by_name, "before.comment"));
         assert_se(bn->n_addresses == 4);
-        assert_se(bn->n_allocated >= 4);
+        assert_se(MALLOC_ELEMENTSOF(bn->addresses) >= 4);
         assert_se(address_equal_4(bn->addresses[0], inet_addr("1.2.3.9")));
         assert_se(address_equal_4(bn->addresses[1], inet_addr("1.2.3.10")));
         assert_se(address_equal_4(bn->addresses[2], inet_addr("1.2.3.11")));
@@ -118,7 +119,7 @@ static void test_parse_etc_hosts(void) {
 
         assert_se(bn = hashmap_get(hosts.by_name, "some.other"));
         assert_se(bn->n_addresses == 1);
-        assert_se(bn->n_allocated >= 1);
+        assert_se(MALLOC_ELEMENTSOF(bn->addresses) >= 1);
         assert_se(address_equal_6(bn->addresses[0], {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5}));
 
         assert_se( set_contains(hosts.no_address, "some.where"));
index dcf67fbbd80c4d0f70541c26fbe8bc3e10e40f0e..6cf08b898f0b70f971759b4ed6be320a475aebb7 100644 (file)
@@ -39,7 +39,7 @@ Bitmap* bitmap_copy(Bitmap *b) {
         if (!ret->bitmaps)
                 return mfree(ret);
 
-        ret->n_bitmaps = ret->bitmaps_allocated = b->n_bitmaps;
+        ret->n_bitmaps = b->n_bitmaps;
         return ret;
 }
 
@@ -81,7 +81,7 @@ int bitmap_set(Bitmap *b, unsigned n) {
         offset = BITMAP_NUM_TO_OFFSET(n);
 
         if (offset >= b->n_bitmaps) {
-                if (!GREEDY_REALLOC0(b->bitmaps, b->bitmaps_allocated, offset + 1))
+                if (!GREEDY_REALLOC0(b->bitmaps, offset + 1))
                         return -ENOMEM;
 
                 b->n_bitmaps = offset + 1;
@@ -147,7 +147,6 @@ void bitmap_clear(Bitmap *b) {
 
         b->bitmaps = mfree(b->bitmaps);
         b->n_bitmaps = 0;
-        b->bitmaps_allocated = 0;
 }
 
 bool bitmap_iterate(const Bitmap *b, Iterator *i, unsigned *n) {
index 5c7651c68db7f54f68dbd7cf0c4311aa30ccb337..e77e2e1f889d62015ed3f2cdf012b9142fae7457 100644 (file)
@@ -9,7 +9,6 @@
 typedef struct Bitmap {
         uint64_t *bitmaps;
         size_t n_bitmaps;
-        size_t bitmaps_allocated;
 } Bitmap;
 
 Bitmap* bitmap_new(void);
index 17c28c6e0181cf8574c8154db8c2cfc90f4f3b9a..354eb6828d75f04f43c652b4a5fc81089c92a186 100644 (file)
@@ -254,7 +254,6 @@ static int boot_entries_find(
                 size_t *n_entries) {
 
         _cleanup_strv_free_ char **files = NULL;
-        size_t n_allocated = *n_entries;
         char **f;
         int r;
 
@@ -268,7 +267,7 @@ static int boot_entries_find(
                 return log_error_errno(r, "Failed to list files in \"%s\": %m", dir);
 
         STRV_FOREACH(f, files) {
-                if (!GREEDY_REALLOC0(*entries, n_allocated, *n_entries + 1))
+                if (!GREEDY_REALLOC0(*entries, *n_entries + 1))
                         return log_oom();
 
                 r = boot_entry_load(root, *f, *entries + *n_entries);
@@ -463,7 +462,6 @@ static int boot_entries_find_unified(
                 size_t *n_entries) {
 
         _cleanup_(closedirp) DIR *d = NULL;
-        size_t n_allocated = *n_entries;
         struct dirent *de;
         int r;
 
@@ -491,7 +489,7 @@ static int boot_entries_find_unified(
                 if (!endswith_no_case(de->d_name, ".efi"))
                         continue;
 
-                if (!GREEDY_REALLOC0(*entries, n_allocated, *n_entries + 1))
+                if (!GREEDY_REALLOC0(*entries, *n_entries + 1))
                         return log_oom();
 
                 fd = openat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
@@ -749,7 +747,6 @@ int boot_entries_augment_from_loader(
                 "auto-reboot-to-firmware-setup", "Reboot Into Firmware Interface",
         };
 
-        size_t n_allocated;
         char **i;
 
         assert(config);
@@ -757,8 +754,6 @@ int boot_entries_augment_from_loader(
         /* Let's add the entries discovered by the boot loader to the end of our list, unless they are
          * already included there. */
 
-        n_allocated = config->n_entries;
-
         STRV_FOREACH(i, found_by_loader) {
                 _cleanup_free_ char *c = NULL, *t = NULL, *p = NULL;
                 char **a, **b;
@@ -785,7 +780,7 @@ int boot_entries_augment_from_loader(
                 if (!p)
                         return log_oom();
 
-                if (!GREEDY_REALLOC0(config->entries, n_allocated, config->n_entries + 1))
+                if (!GREEDY_REALLOC0(config->entries, config->n_entries + 1))
                         return log_oom();
 
                 config->entries[config->n_entries++] = (BootEntry) {
index a8a34521fd640c24c1a816e39c8424e4769f9f86..ec8437d583bdda658ed77dd7c1f2f221fcdd9769 100644 (file)
@@ -133,7 +133,7 @@ int bpf_program_add_instructions(BPFProgram *p, const struct bpf_insn *instructi
         if (p->kernel_fd >= 0) /* don't allow modification after we uploaded things to the kernel */
                 return -EBUSY;
 
-        if (!GREEDY_REALLOC(p->instructions, p->allocated, p->n_instructions + count))
+        if (!GREEDY_REALLOC(p->instructions, p->n_instructions + count))
                 return -ENOMEM;
 
         memcpy(p->instructions + p->n_instructions, instructions, sizeof(struct bpf_insn) * count);
index 86fd338c93c44d61a18d4c792bcff75e84060dc5..edde86c11958bf220f068a6dbce3c49fab15427b 100644 (file)
@@ -17,7 +17,6 @@ struct BPFProgram {
         uint32_t prog_type;
 
         size_t n_instructions;
-        size_t allocated;
         struct bpf_insn *instructions;
 
         char *attached_path;
index 14a4a4cfd6bac8380a687f6b29b02cf8afcf1c0a..53f63508ebc93a60a4c5d4e60026c8849604cc4a 100644 (file)
@@ -137,7 +137,7 @@ int bus_message_read_dns_servers(
                         size_t *ret_n_dns) {
 
         struct in_addr_full **dns = NULL;
-        size_t n = 0, allocated = 0;
+        size_t n = 0;
         int r;
 
         assert(message);
@@ -160,7 +160,7 @@ int bus_message_read_dns_servers(
                 if (r == 0)
                         break;
 
-                if (!GREEDY_REALLOC(dns, allocated, n+1)) {
+                if (!GREEDY_REALLOC(dns, n+1)) {
                         r = -ENOMEM;
                         goto clear;
                 }
index b76125e551f91225119973f10aa06eaa3c56cb9a..6f4a71138ce5ea5bf06cc351e9a922a151a35287 100644 (file)
@@ -255,7 +255,7 @@ static int dump_extra_processes(
         _cleanup_free_ pid_t *pids = NULL;
         _cleanup_hashmap_free_ Hashmap *names = NULL;
         struct CGroupInfo *cg;
-        size_t n_allocated = 0, n = 0, k;
+        size_t n = 0, k;
         int width, r;
 
         /* Prints the extra processes, i.e. those that are in cgroups we haven't displayed yet. We show them as
@@ -275,7 +275,7 @@ static int dump_extra_processes(
                 if (r < 0)
                         return r;
 
-                if (!GREEDY_REALLOC(pids, n_allocated, n + hashmap_size(cg->pids)))
+                if (!GREEDY_REALLOC(pids, n + hashmap_size(cg->pids)))
                         return -ENOMEM;
 
                 HASHMAP_FOREACH_KEY(name, pidp, cg->pids) {
index f7c24f88604a59937b264f21ae5dcdb438a7f054..c7e63be5080693d80ac9f7548823d496f7f65659 100644 (file)
@@ -82,12 +82,12 @@ static int show_cgroup_one_by_path(
                 bool more,
                 OutputFlags flags) {
 
-        char *fn;
-        _cleanup_fclose_ FILE *f = NULL;
-        size_t n = 0, n_allocated = 0;
         _cleanup_free_ pid_t *pids = NULL;
+        _cleanup_fclose_ FILE *f = NULL;
         _cleanup_free_ char *p = NULL;
+        size_t n = 0;
         pid_t pid;
+        char *fn;
         int r;
 
         r = cg_mangle_path(path, &p);
@@ -104,10 +104,9 @@ static int show_cgroup_one_by_path(
                 if (!(flags & OUTPUT_KERNEL_THREADS) && is_kernel_thread(pid) > 0)
                         continue;
 
-                if (!GREEDY_REALLOC(pids, n_allocated, n + 1))
+                if (!GREEDY_REALLOC(pids, n + 1))
                         return -ENOMEM;
 
-                assert(n < n_allocated);
                 pids[n++] = pid;
         }
 
index 2c3b5bb034340cef1bcf09c91f092cdf3e9076a3..e3e69635a6e861d34ea29fe997aaaa008f68ffb1 100644 (file)
 
 char* cpu_set_to_string(const CPUSet *a) {
         _cleanup_free_ char *str = NULL;
-        size_t allocated = 0, len = 0;
+        size_t len = 0;
         int i, r;
 
         for (i = 0; (size_t) i < a->allocated * 8; i++) {
                 if (!CPU_ISSET_S(i, a->allocated, a->set))
                         continue;
 
-                if (!GREEDY_REALLOC(str, allocated, len + 1 + DECIMAL_STR_MAX(int)))
+                if (!GREEDY_REALLOC(str, len + 1 + DECIMAL_STR_MAX(int)))
                         return NULL;
 
                 r = sprintf(str + len, len > 0 ? " %d" : "%d", i);
@@ -43,8 +43,8 @@ char* cpu_set_to_string(const CPUSet *a) {
 char *cpu_set_to_range_string(const CPUSet *set) {
         unsigned range_start = 0, range_end;
         _cleanup_free_ char *str = NULL;
-        size_t allocated = 0, len = 0;
         bool in_range = false;
+        size_t len = 0;
         int r;
 
         for (unsigned i = 0; i < set->allocated * 8; i++)
@@ -58,7 +58,7 @@ char *cpu_set_to_range_string(const CPUSet *set) {
                 } else if (in_range) {
                         in_range = false;
 
-                        if (!GREEDY_REALLOC(str, allocated, len + 2 + 2 * DECIMAL_STR_MAX(unsigned)))
+                        if (!GREEDY_REALLOC(str, len + 2 + 2 * DECIMAL_STR_MAX(unsigned)))
                                 return NULL;
 
                         if (range_end > range_start)
@@ -70,7 +70,7 @@ char *cpu_set_to_range_string(const CPUSet *set) {
                 }
 
         if (in_range) {
-                if (!GREEDY_REALLOC(str, allocated, len + 2 + 2 * DECIMAL_STR_MAX(int)))
+                if (!GREEDY_REALLOC(str, len + 2 + 2 * DECIMAL_STR_MAX(int)))
                         return NULL;
 
                 if (range_end > range_start)
index 3cf00adb6f0350766ceef83b8fdb2385955b8e3e..a8402c1dcd1a3d07ee7ee54519bfe9eb0f5f85b6 100644 (file)
@@ -1780,7 +1780,6 @@ typedef struct DecryptedPartition {
 struct DecryptedImage {
         DecryptedPartition *decrypted;
         size_t n_decrypted;
-        size_t n_allocated;
 };
 #endif
 
@@ -1876,7 +1875,7 @@ static int decrypt_partition(
         if (r < 0)
                 return r;
 
-        if (!GREEDY_REALLOC0(d->decrypted, d->n_allocated, d->n_decrypted + 1))
+        if (!GREEDY_REALLOC0(d->decrypted, d->n_decrypted + 1))
                 return -ENOMEM;
 
         r = sym_crypt_init(&cd, m->node);
@@ -2028,7 +2027,7 @@ static int verity_partition(
         if (r < 0)
                 return r;
 
-        if (!GREEDY_REALLOC0(d->decrypted, d->n_allocated, d->n_decrypted + 1))
+        if (!GREEDY_REALLOC0(d->decrypted, d->n_decrypted + 1))
                 return -ENOMEM;
 
         /* If activating fails because the device already exists, check the metadata and reuse it if it matches.
index 716c0100764e6a56d6057fafd4313932ce1b688f..216e1a0647173a5fd916d25cb86f7923da60084b 100644 (file)
@@ -407,7 +407,7 @@ int dns_label_undo_idna(const char *encoded, size_t encoded_size, char *decoded,
 
 int dns_name_concat(const char *a, const char *b, DNSLabelFlags flags, char **_ret) {
         _cleanup_free_ char *ret = NULL;
-        size_t n = 0, allocated = 0;
+        size_t n = 0;
         const char *p;
         bool first = true;
         int r;
@@ -439,7 +439,7 @@ int dns_name_concat(const char *a, const char *b, DNSLabelFlags flags, char **_r
                 }
 
                 if (_ret) {
-                        if (!GREEDY_REALLOC(ret, allocated, n + !first + DNS_LABEL_ESCAPED_MAX))
+                        if (!GREEDY_REALLOC(ret, n + !first + DNS_LABEL_ESCAPED_MAX))
                                 return -ENOMEM;
 
                         r = dns_label_escape(label, r, ret + n + !first, DNS_LABEL_ESCAPED_MAX);
@@ -471,12 +471,12 @@ finish:
         if (_ret) {
                 if (n == 0) {
                         /* Nothing appended? If so, generate at least a single dot, to indicate the DNS root domain */
-                        if (!GREEDY_REALLOC(ret, allocated, 2))
+                        if (!GREEDY_REALLOC(ret, 2))
                                 return -ENOMEM;
 
                         ret[n++] = '.';
                 } else {
-                        if (!GREEDY_REALLOC(ret, allocated, n + 1))
+                        if (!GREEDY_REALLOC(ret, n + 1))
                                 return -ENOMEM;
                 }
 
index ee32b0b097f153829c5441785a52f50bc81c2fa7..c250bc2d575541dad50fd8754e242c186d3007b9 100644 (file)
@@ -493,7 +493,6 @@ int efi_get_boot_options(uint16_t **options) {
         _cleanup_closedir_ DIR *dir = NULL;
         _cleanup_free_ uint16_t *list = NULL;
         struct dirent *de;
-        size_t alloc = 0;
         int count = 0;
 
         assert(options);
@@ -521,7 +520,7 @@ int efi_get_boot_options(uint16_t **options) {
                 if (id < 0)
                         continue;
 
-                if (!GREEDY_REALLOC(list, alloc, count + 1))
+                if (!GREEDY_REALLOC(list, count + 1))
                         return -ENOMEM;
 
                 list[count++] = id;
index 76cf3343db78a1e88480c569eade2a13415bcf1c..8b1240d63a1288f614e6a4365016e7e41fde1059 100644 (file)
@@ -136,7 +136,6 @@ struct Table {
         size_t cell_height_max; /* Maximum number of lines per cell. (If there are more, ellipsis is shown. If SIZE_MAX then no limit is set, the default. == 0 is not allowed.) */
 
         TableData **data;
-        size_t n_allocated;
 
         size_t *display_map;  /* List of columns to show (by their index). It's fine if columns are listed multiple times or not at all */
         size_t n_display_map;
@@ -453,7 +452,7 @@ int table_add_cell_full(
                         return -ENOMEM;
         }
 
-        if (!GREEDY_REALLOC(t->data, t->n_allocated, MAX(t->n_cells + 1, t->n_columns)))
+        if (!GREEDY_REALLOC(t->data, MAX(t->n_cells + 1, t->n_columns)))
                 return -ENOMEM;
 
         if (ret_cell)
@@ -510,7 +509,7 @@ int table_dup_cell(Table *t, TableCell *cell) {
         if (i >= t->n_cells)
                 return -ENXIO;
 
-        if (!GREEDY_REALLOC(t->data, t->n_allocated, MAX(t->n_cells + 1, t->n_columns)))
+        if (!GREEDY_REALLOC(t->data, MAX(t->n_cells + 1, t->n_columns)))
                 return -ENOMEM;
 
         t->data[t->n_cells++] = table_data_ref(t->data[i]);
@@ -1094,19 +1093,18 @@ static int table_set_display_all(Table *t) {
 }
 
 int table_set_display_internal(Table *t, size_t first_column, ...) {
-        size_t allocated, column;
+        size_t column;
         va_list ap;
 
         assert(t);
 
-        allocated = t->n_display_map;
         column = first_column;
 
         va_start(ap, first_column);
         for (;;) {
                 assert(column < t->n_columns);
 
-                if (!GREEDY_REALLOC(t->display_map, allocated, MAX(t->n_columns, t->n_display_map+1))) {
+                if (!GREEDY_REALLOC(t->display_map, MAX(t->n_columns, t->n_display_map+1))) {
                         va_end(ap);
                         return -ENOMEM;
                 }
@@ -1124,19 +1122,18 @@ int table_set_display_internal(Table *t, size_t first_column, ...) {
 }
 
 int table_set_sort_internal(Table *t, size_t first_column, ...) {
-        size_t allocated, column;
+        size_t column;
         va_list ap;
 
         assert(t);
 
-        allocated = t->n_sort_map;
         column = first_column;
 
         va_start(ap, first_column);
         for (;;) {
                 assert(column < t->n_columns);
 
-                if (!GREEDY_REALLOC(t->sort_map, allocated, MAX(t->n_columns, t->n_sort_map+1))) {
+                if (!GREEDY_REALLOC(t->sort_map, MAX(t->n_columns, t->n_sort_map+1))) {
                         va_end(ap);
                         return -ENOMEM;
                 }
index 119980663ff39da667777a268359185e504fc1ff..3723ec031c115406eac8617bdaefa99ca1b8d371 100644 (file)
@@ -2166,10 +2166,10 @@ int unit_file_unmask(
         _cleanup_(lookup_paths_free) LookupPaths paths = {};
         _cleanup_set_free_free_ Set *remove_symlinks_to = NULL;
         _cleanup_strv_free_ char **todo = NULL;
-        size_t n_todo = 0, n_allocated = 0;
         const char *config_path;
-        char **i;
+        size_t n_todo = 0;
         bool dry_run;
+        char **i;
         int r, q;
 
         assert(scope >= 0);
@@ -2203,7 +2203,7 @@ int unit_file_unmask(
                 if (r == 0)
                         continue;
 
-                if (!GREEDY_REALLOC0(todo, n_allocated, n_todo + 2))
+                if (!GREEDY_REALLOC0(todo, n_todo + 2))
                         return -ENOMEM;
 
                 todo[n_todo] = strdup(*i);
@@ -2259,8 +2259,8 @@ int unit_file_link(
 
         _cleanup_(lookup_paths_free) LookupPaths paths = {};
         _cleanup_strv_free_ char **todo = NULL;
-        size_t n_todo = 0, n_allocated = 0;
         const char *config_path;
+        size_t n_todo = 0;
         char **i;
         int r, q;
 
@@ -2303,7 +2303,7 @@ int unit_file_link(
                 if (q > 0)
                         continue;
 
-                if (!GREEDY_REALLOC0(todo, n_allocated, n_todo + 2))
+                if (!GREEDY_REALLOC0(todo, n_todo + 2))
                         return -ENOMEM;
 
                 todo[n_todo] = strdup(*i);
@@ -2360,7 +2360,7 @@ int unit_file_revert(
         _cleanup_set_free_free_ Set *remove_symlinks_to = NULL;
         _cleanup_(lookup_paths_free) LookupPaths paths = {};
         _cleanup_strv_free_ char **todo = NULL;
-        size_t n_todo = 0, n_allocated = 0;
+        size_t n_todo = 0;
         char **i;
         int r, q;
 
@@ -2421,7 +2421,7 @@ int unit_file_revert(
                                 if (r < 0)
                                         return r;
                                 if (r > 0) {
-                                        if (!GREEDY_REALLOC0(todo, n_allocated, n_todo + 2))
+                                        if (!GREEDY_REALLOC0(todo, n_todo + 2))
                                                 return -ENOMEM;
 
                                         todo[n_todo++] = TAKE_PTR(dropin);
@@ -2450,7 +2450,7 @@ int unit_file_revert(
                                 if (r < 0)
                                         return r;
                                 if (r > 0) {
-                                        if (!GREEDY_REALLOC0(todo, n_allocated, n_todo + 2))
+                                        if (!GREEDY_REALLOC0(todo, n_todo + 2))
                                                 return -ENOMEM;
 
                                         todo[n_todo++] = TAKE_PTR(path);
@@ -2946,7 +2946,6 @@ static int presets_find_config(UnitFileScope scope, const char *root_dir, char *
 
 static int read_presets(UnitFileScope scope, const char *root_dir, UnitFilePresets *presets) {
         _cleanup_(unit_file_presets_freep) UnitFilePresets ps = {};
-        size_t n_allocated = 0;
         _cleanup_strv_free_ char **files = NULL;
         char **p;
         int r;
@@ -3025,7 +3024,7 @@ static int read_presets(UnitFileScope scope, const char *root_dir, UnitFilePrese
                         }
 
                         if (rule.action) {
-                                if (!GREEDY_REALLOC(ps.rules, n_allocated, ps.n_rules + 1))
+                                if (!GREEDY_REALLOC(ps.rules, ps.n_rules + 1))
                                         return -ENOMEM;
 
                                 ps.rules[ps.n_rules++] = rule;
index b2785f0552488b307afc0de6120deeca8ebaa51b..0d82acc25cdedcd017c5e269bdd00388c80d9558 100644 (file)
@@ -1,6 +1,7 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
 #include <errno.h>
+#include <malloc.h>
 #include <unistd.h>
 
 #include "alloc-util.h"
@@ -37,7 +38,7 @@ void journal_importer_cleanup(JournalImporter *imp) {
 static char* realloc_buffer(JournalImporter *imp, size_t size) {
         char *b, *old = imp->buf;
 
-        b = GREEDY_REALLOC(imp->buf, imp->size, size);
+        b = GREEDY_REALLOC(imp->buf, size);
         if (!b)
                 return NULL;
 
@@ -53,8 +54,7 @@ static int get_line(JournalImporter *imp, char **line, size_t *size) {
         assert(imp);
         assert(imp->state == IMPORTER_STATE_LINE);
         assert(imp->offset <= imp->filled);
-        assert(imp->filled <= imp->size);
-        assert(!imp->buf || imp->size > 0);
+        assert(imp->filled <= MALLOC_SIZEOF_SAFE(imp->buf));
         assert(imp->fd >= 0);
 
         for (;;) {
@@ -80,22 +80,22 @@ static int get_line(JournalImporter *imp, char **line, size_t *size) {
                 /* We know that imp->filled is at most DATA_SIZE_MAX, so if
                    we reallocate it, we'll increase the size at least a bit. */
                 assert_cc(DATA_SIZE_MAX < ENTRY_SIZE_MAX);
-                if (imp->size - imp->filled < LINE_CHUNK &&
+                if (MALLOC_SIZEOF_SAFE(imp->buf) - imp->filled < LINE_CHUNK &&
                     !realloc_buffer(imp, MIN(imp->filled + LINE_CHUNK, ENTRY_SIZE_MAX)))
                                 return log_oom();
 
                 assert(imp->buf);
-                assert(imp->size - imp->filled >= LINE_CHUNK ||
-                       imp->size == ENTRY_SIZE_MAX);
+                assert(MALLOC_SIZEOF_SAFE(imp->buf) - imp->filled >= LINE_CHUNK ||
+                       MALLOC_SIZEOF_SAFE(imp->buf) >= ENTRY_SIZE_MAX);
 
                 n = read(imp->fd,
                          imp->buf + imp->filled,
-                         imp->size - imp->filled);
+                         MALLOC_SIZEOF_SAFE(imp->buf) - imp->filled);
                 if (n < 0) {
                         if (errno != EAGAIN)
                                 log_error_errno(errno, "read(%d, ..., %zu): %m",
                                                 imp->fd,
-                                                imp->size - imp->filled);
+                                                MALLOC_SIZEOF_SAFE(imp->buf) - imp->filled);
                         return -errno;
                 } else if (n == 0)
                         return 0;
@@ -116,9 +116,7 @@ static int fill_fixed_size(JournalImporter *imp, void **data, size_t size) {
         assert(IN_SET(imp->state, IMPORTER_STATE_DATA_START, IMPORTER_STATE_DATA, IMPORTER_STATE_DATA_FINISH));
         assert(size <= DATA_SIZE_MAX);
         assert(imp->offset <= imp->filled);
-        assert(imp->filled <= imp->size);
-        assert(imp->buf || imp->size == 0);
-        assert(!imp->buf || imp->size > 0);
+        assert(imp->filled <= MALLOC_SIZEOF_SAFE(imp->buf));
         assert(imp->fd >= 0);
         assert(data);
 
@@ -133,11 +131,11 @@ static int fill_fixed_size(JournalImporter *imp, void **data, size_t size) {
                         return log_oom();
 
                 n = read(imp->fd, imp->buf + imp->filled,
-                         imp->size - imp->filled);
+                         MALLOC_SIZEOF_SAFE(imp->buf) - imp->filled);
                 if (n < 0) {
                         if (errno != EAGAIN)
                                 log_error_errno(errno, "read(%d, ..., %zu): %m", imp->fd,
-                                                imp->size - imp->filled);
+                                                MALLOC_SIZEOF_SAFE(imp->buf) - imp->filled);
                         return -errno;
                 } else if (n == 0)
                         return 0;
@@ -431,7 +429,7 @@ int journal_importer_push_data(JournalImporter *imp, const char *data, size_t si
                 return log_error_errno(SYNTHETIC_ERRNO(ENOMEM),
                                        "Failed to store received data of size %zu "
                                        "(in addition to existing %zu bytes with %zu filled): %s",
-                                       size, imp->size, imp->filled,
+                                       size, MALLOC_SIZEOF_SAFE(imp->buf), imp->filled,
                                        strerror_safe(ENOMEM));
 
         memcpy(imp->buf + imp->filled, data, size);
@@ -452,18 +450,21 @@ void journal_importer_drop_iovw(JournalImporter *imp) {
 
         if (remain == 0) /* no brainer */
                 imp->offset = imp->scanned = imp->filled = 0;
-        else if (imp->offset > imp->size - imp->filled &&
+        else if (imp->offset > MALLOC_SIZEOF_SAFE(imp->buf) - imp->filled &&
                  imp->offset > remain) {
                 memcpy(imp->buf, imp->buf + imp->offset, remain);
                 imp->offset = imp->scanned = 0;
                 imp->filled = remain;
         }
 
-        target = imp->size;
+        target = MALLOC_SIZEOF_SAFE(imp->buf);
         while (target > 16 * LINE_CHUNK && imp->filled < target / 2)
                 target /= 2;
-        if (target < imp->size) {
+        if (target < MALLOC_SIZEOF_SAFE(imp->buf)) {
                 char *tmp;
+                size_t old_size;
+
+                old_size = MALLOC_SIZEOF_SAFE(imp->buf);
 
                 tmp = realloc(imp->buf, target);
                 if (!tmp)
@@ -471,9 +472,8 @@ void journal_importer_drop_iovw(JournalImporter *imp) {
                                     target);
                 else {
                         log_debug("Reallocated buffer from %zu to %zu bytes",
-                                  imp->size, target);
+                                  old_size, target);
                         imp->buf = tmp;
-                        imp->size = target;
                 }
         }
 }
index e0073fcea822e33a8c10524ba183f2307ab37075..7eb2169217153deabe9ff0a89c97bcff8eb54168 100644 (file)
@@ -31,7 +31,6 @@ typedef struct JournalImporter {
         char *name;
 
         char *buf;
-        size_t size;       /* total size of the buffer */
         size_t offset;     /* offset to the beginning of live data in the buffer */
         size_t scanned;    /* number of bytes since the beginning of data without a newline */
         size_t filled;     /* total number of bytes in the buffer */
index ef1e937645914ee714153b8e11f636ce3c4732ef..ea4b46ff26162c519ceadba178a908330e918726 100644 (file)
@@ -2399,7 +2399,7 @@ static int unhex_ucs2(const char *c, uint16_t *ret) {
 
 static int json_parse_string(const char **p, char **ret) {
         _cleanup_free_ char *s = NULL;
-        size_t n = 0, allocated = 0;
+        size_t n = 0;
         const char *c;
 
         assert(p);
@@ -2471,7 +2471,7 @@ static int json_parse_string(const char **p, char **ret) {
 
                                 c += 5;
 
-                                if (!GREEDY_REALLOC(s, allocated, n + 5))
+                                if (!GREEDY_REALLOC(s, n + 5))
                                         return -ENOMEM;
 
                                 if (!utf16_is_surrogate(x))
@@ -2500,7 +2500,7 @@ static int json_parse_string(const char **p, char **ret) {
                         } else
                                 return -EINVAL;
 
-                        if (!GREEDY_REALLOC(s, allocated, n + 2))
+                        if (!GREEDY_REALLOC(s, n + 2))
                                 return -ENOMEM;
 
                         s[n++] = ch;
@@ -2512,7 +2512,7 @@ static int json_parse_string(const char **p, char **ret) {
                 if (len < 0)
                         return len;
 
-                if (!GREEDY_REALLOC(s, allocated, n + len + 1))
+                if (!GREEDY_REALLOC(s, n + len + 1))
                         return -ENOMEM;
 
                 memcpy(s + n, c, len);
@@ -2825,7 +2825,7 @@ typedef enum JsonExpect {
 typedef struct JsonStack {
         JsonExpect expect;
         JsonVariant **elements;
-        size_t n_elements, n_elements_allocated;
+        size_t n_elements;
         unsigned line_before;
         unsigned column_before;
         size_t n_suppress; /* When building: if > 0, suppress this many subsequent elements. If == SIZE_MAX, suppress all subsequent elements */
@@ -2847,7 +2847,7 @@ static int json_parse_internal(
                 unsigned *column,
                 bool continue_end) {
 
-        size_t n_stack = 1, n_stack_allocated = 0, i;
+        size_t n_stack = 1, i;
         unsigned line_buffer = 0, column_buffer = 0;
         void *tokenizer_state = NULL;
         JsonStack *stack = NULL;
@@ -2859,7 +2859,7 @@ static int json_parse_internal(
 
         p = *input;
 
-        if (!GREEDY_REALLOC(stack, n_stack_allocated, n_stack))
+        if (!GREEDY_REALLOC(stack, n_stack))
                 return -ENOMEM;
 
         stack[0] = (JsonStack) {
@@ -2933,7 +2933,7 @@ static int json_parse_internal(
                                 goto finish;
                         }
 
-                        if (!GREEDY_REALLOC(stack, n_stack_allocated, n_stack+1)) {
+                        if (!GREEDY_REALLOC(stack, n_stack+1)) {
                                 r = -ENOMEM;
                                 goto finish;
                         }
@@ -2984,7 +2984,7 @@ static int json_parse_internal(
                                 goto finish;
                         }
 
-                        if (!GREEDY_REALLOC(stack, n_stack_allocated, n_stack+1)) {
+                        if (!GREEDY_REALLOC(stack, n_stack+1)) {
                                 r = -ENOMEM;
                                 goto finish;
                         }
@@ -3168,7 +3168,7 @@ static int json_parse_internal(
 
                         (void) json_variant_set_source(&add, source, line_token, column_token);
 
-                        if (!GREEDY_REALLOC(current->elements, current->n_elements_allocated, current->n_elements + 1)) {
+                        if (!GREEDY_REALLOC(current->elements, current->n_elements + 1)) {
                                 r = -ENOMEM;
                                 goto finish;
                         }
@@ -3229,12 +3229,12 @@ int json_parse_file_at(FILE *f, int dir_fd, const char *path, JsonParseFlags fla
 
 int json_buildv(JsonVariant **ret, va_list ap) {
         JsonStack *stack = NULL;
-        size_t n_stack = 1, n_stack_allocated = 0, i;
+        size_t n_stack = 1, i;
         int r;
 
         assert_return(ret, -EINVAL);
 
-        if (!GREEDY_REALLOC(stack, n_stack_allocated, n_stack))
+        if (!GREEDY_REALLOC(stack, n_stack))
                 return -ENOMEM;
 
         stack[0] = (JsonStack) {
@@ -3517,7 +3517,7 @@ int json_buildv(JsonVariant **ret, va_list ap) {
                                 goto finish;
                         }
 
-                        if (!GREEDY_REALLOC(stack, n_stack_allocated, n_stack+1)) {
+                        if (!GREEDY_REALLOC(stack, n_stack+1)) {
                                 r = -ENOMEM;
                                 goto finish;
                         }
@@ -3714,7 +3714,7 @@ int json_buildv(JsonVariant **ret, va_list ap) {
                                 goto finish;
                         }
 
-                        if (!GREEDY_REALLOC(stack, n_stack_allocated, n_stack+1)) {
+                        if (!GREEDY_REALLOC(stack, n_stack+1)) {
                                 r = -ENOMEM;
                                 goto finish;
                         }
@@ -3810,7 +3810,7 @@ int json_buildv(JsonVariant **ret, va_list ap) {
 
                 /* If a variant was generated, add it to our current variant, but only if we are not supposed to suppress additions */
                 if (add && current->n_suppress == 0) {
-                        if (!GREEDY_REALLOC(current->elements, current->n_elements_allocated, current->n_elements + 1)) {
+                        if (!GREEDY_REALLOC(current->elements, current->n_elements + 1)) {
                                 r = -ENOMEM;
                                 goto finish;
                         }
index 9dec7a7e1a708f04b15ceed3e78f001320d4b488..66e452adade77df25bdb7452f8c148ff741259ed 100644 (file)
@@ -67,7 +67,7 @@ int local_addresses(
         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
         _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
         _cleanup_free_ struct local_address *list = NULL;
-        size_t n_list = 0, n_allocated = 0;
+        size_t n_list = 0;
         sd_netlink_message *m;
         int r;
 
@@ -121,7 +121,7 @@ int local_addresses(
                 if (flags & IFA_F_DEPRECATED)
                         continue;
 
-                if (!GREEDY_REALLOC0(list, n_allocated, n_list+1))
+                if (!GREEDY_REALLOC0(list, n_list+1))
                         return -ENOMEM;
 
                 a = list + n_list;
@@ -175,7 +175,6 @@ int local_addresses(
 static int add_local_gateway(
                 struct local_address **list,
                 size_t *n_list,
-                size_t *n_allocated,
                 int af,
                 int ifindex,
                 uint32_t metric,
@@ -183,13 +182,12 @@ static int add_local_gateway(
 
         assert(list);
         assert(n_list);
-        assert(n_allocated);
         assert(via);
 
         if (af != AF_UNSPEC && af != via->family)
                 return 0;
 
-        if (!GREEDY_REALLOC(*list, *n_allocated, *n_list + 1))
+        if (!GREEDY_REALLOC(*list, *n_list + 1))
                 return -ENOMEM;
 
         (*list)[(*n_list)++] = (struct local_address) {
@@ -211,7 +209,7 @@ int local_gateways(
         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
         _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
         _cleanup_free_ struct local_address *list = NULL;
-        size_t n_list = 0, n_allocated = 0;
+        size_t n_list = 0;
         int r;
 
         if (context)
@@ -299,7 +297,7 @@ int local_gateways(
                         if (r >= 0) {
                                 via.family = family;
                                 via.address = gateway;
-                                r = add_local_gateway(&list, &n_list, &n_allocated, af, ifi, metric, &via);
+                                r = add_local_gateway(&list, &n_list, af, ifi, metric, &via);
                                 if (r < 0)
                                         return r;
 
@@ -313,7 +311,7 @@ int local_gateways(
                         if (r < 0 && r != -ENODATA)
                                 return r;
                         if (r >= 0) {
-                                r = add_local_gateway(&list, &n_list, &n_allocated, af, ifi, metric, &via);
+                                r = add_local_gateway(&list, &n_list, af, ifi, metric, &via);
                                 if (r < 0)
                                         return r;
 
@@ -335,7 +333,7 @@ int local_gateways(
                                 if (ifindex > 0 && mr->ifindex != ifindex)
                                         continue;
 
-                                r = add_local_gateway(&list, &n_list, &n_allocated, af, ifi, metric, &mr->gateway);
+                                r = add_local_gateway(&list, &n_list, af, ifi, metric, &mr->gateway);
                                 if (r < 0)
                                         return r;
                         }
@@ -358,7 +356,7 @@ int local_outbounds(
                 struct local_address **ret) {
 
         _cleanup_free_ struct local_address *list = NULL, *gateways = NULL;
-        size_t n_list = 0, n_allocated = 0;
+        size_t n_list = 0;
         int r, n_gateways;
 
         /* Determines our default outbound addresses, i.e. the "primary" local addresses we use to talk to IP
@@ -457,7 +455,7 @@ int local_outbounds(
                         if (in4_addr_is_null(&sa.in.sin_addr)) /* Auto-binding didn't work. :-( */
                                 continue;
 
-                        if (!GREEDY_REALLOC(list, n_allocated, n_list+1))
+                        if (!GREEDY_REALLOC(list, n_list+1))
                                 return -ENOMEM;
 
                         list[n_list++] = (struct local_address) {
@@ -472,7 +470,7 @@ int local_outbounds(
                         if (in6_addr_is_null(&sa.in6.sin6_addr))
                                 continue;
 
-                        if (!GREEDY_REALLOC(list, n_allocated, n_list+1))
+                        if (!GREEDY_REALLOC(list, n_list+1))
                                 return -ENOMEM;
 
                         list[n_list++] = (struct local_address) {
index 53280cf40a2e1df81840d9961daaf245e581315b..6bb5d80a9515b48f39643b2ab8e342d7b9b9e575 100644 (file)
@@ -512,7 +512,6 @@ int read_fiemap(int fd, struct fiemap **ret) {
         uint32_t result_extents = 0;
         uint64_t fiemap_start = 0, fiemap_length;
         const size_t n_extra = DIV_ROUND_UP(sizeof(struct fiemap), sizeof(struct fiemap_extent));
-        size_t fiemap_allocated = n_extra, result_fiemap_allocated = n_extra;
 
         if (fstat(fd, &statinfo) < 0)
                 return log_debug_errno(errno, "Cannot determine file size: %m");
@@ -551,8 +550,7 @@ int read_fiemap(int fd, struct fiemap **ret) {
 
                 /* Resize fiemap to allow us to read in the extents, result fiemap has to hold all
                  * the extents for the whole file. Add space for the initial struct fiemap. */
-                if (!greedy_realloc0((void**) &fiemap, &fiemap_allocated,
-                                     n_extra + fiemap->fm_mapped_extents, sizeof(struct fiemap_extent)))
+                if (!greedy_realloc0((void**) &fiemap, n_extra + fiemap->fm_mapped_extents, sizeof(struct fiemap_extent)))
                         return -ENOMEM;
 
                 fiemap->fm_extent_count = fiemap->fm_mapped_extents;
@@ -562,7 +560,7 @@ int read_fiemap(int fd, struct fiemap **ret) {
                         return log_debug_errno(errno, "Failed to read extents: %m");
 
                 /* Resize result_fiemap to allow us to copy in the extents */
-                if (!greedy_realloc((void**) &result_fiemap, &result_fiemap_allocated,
+                if (!greedy_realloc((void**) &result_fiemap,
                                     n_extra + result_extents + fiemap->fm_mapped_extents, sizeof(struct fiemap_extent)))
                         return -ENOMEM;
 
index ef164b3942ba7fed66523f4d9eb4a69bc4b5ad7f..f4c73b2741edb797b07830629e570ca0b400427b 100644 (file)
 #define POSSIBLE_SPECIFIERS ALPHANUMERICAL "%"
 
 int specifier_printf(const char *text, size_t max_length, const Specifier table[], const void *userdata, char **ret) {
-        size_t l, allocated = 0;
         _cleanup_free_ char *result = NULL;
-        char *t;
-        const char *f;
         bool percent = false;
+        const char *f;
+        size_t l;
+        char *t;
         int r;
 
         assert(text);
         assert(table);
 
         l = strlen(text);
-        if (!GREEDY_REALLOC(result, allocated, l + 1))
+        if (!GREEDY_REALLOC(result, l + 1))
                 return -ENOMEM;
         t = result;
 
@@ -67,7 +67,7 @@ int specifier_printf(const char *text, size_t max_length, const Specifier table[
                                         j = t - result;
                                         k = strlen(w);
 
-                                        if (!GREEDY_REALLOC(result, allocated, j + k + l + 1))
+                                        if (!GREEDY_REALLOC(result, j + k + l + 1))
                                                 return -ENOMEM;
                                         memcpy(result + j, w, k);
                                         t = result + j + k;
@@ -98,13 +98,6 @@ int specifier_printf(const char *text, size_t max_length, const Specifier table[
         }
         *(t++) = 0;
 
-        /* Try to deallocate unused bytes, but don't sweat it too much */
-        if ((size_t)(t - result) < allocated) {
-                t = realloc(result, t - result);
-                if (t)
-                        result = t;
-        }
-
         *ret = TAKE_PTR(result);
         return 0;
 }
index daac6fd1b037b51f1a10a6515a5183233a28d803..e77b1447798e39aeac875a91f35c66c50a742d36 100644 (file)
@@ -1,5 +1,6 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
+#include <malloc.h>
 #include <sys/poll.h>
 
 #include "alloc-util.h"
@@ -113,13 +114,11 @@ struct Varlink {
         int fd;
 
         char *input_buffer; /* valid data starts at input_buffer_index, ends at input_buffer_index+input_buffer_size */
-        size_t input_buffer_allocated;
         size_t input_buffer_index;
         size_t input_buffer_size;
         size_t input_buffer_unscanned;
 
         char *output_buffer; /* valid data starts at output_buffer_index, ends at output_buffer_index+output_buffer_size */
-        size_t output_buffer_allocated;
         size_t output_buffer_index;
         size_t output_buffer_size;
 
@@ -506,14 +505,14 @@ static int varlink_read(Varlink *v) {
 
         assert(v->fd >= 0);
 
-        if (v->input_buffer_allocated <= v->input_buffer_index + v->input_buffer_size) {
+        if (MALLOC_SIZEOF_SAFE(v->input_buffer) <= v->input_buffer_index + v->input_buffer_size) {
                 size_t add;
 
                 add = MIN(VARLINK_BUFFER_MAX - v->input_buffer_size, VARLINK_READ_SIZE);
 
                 if (v->input_buffer_index == 0) {
 
-                        if (!GREEDY_REALLOC(v->input_buffer, v->input_buffer_allocated, v->input_buffer_size + add))
+                        if (!GREEDY_REALLOC(v->input_buffer, v->input_buffer_size + add))
                                 return -ENOMEM;
 
                 } else {
@@ -526,13 +525,11 @@ static int varlink_read(Varlink *v) {
                         memcpy(b, v->input_buffer + v->input_buffer_index, v->input_buffer_size);
 
                         free_and_replace(v->input_buffer, b);
-
-                        v->input_buffer_allocated = v->input_buffer_size + add;
                         v->input_buffer_index = 0;
                 }
         }
 
-        rs = v->input_buffer_allocated - (v->input_buffer_index + v->input_buffer_size);
+        rs = MALLOC_SIZEOF_SAFE(v->input_buffer) - (v->input_buffer_index + v->input_buffer_size);
 
         bool prefer_read = v->prefer_read_write;
         if (!prefer_read) {
@@ -577,7 +574,7 @@ static int varlink_parse_message(Varlink *v) {
                 return 0;
 
         assert(v->input_buffer_unscanned <= v->input_buffer_size);
-        assert(v->input_buffer_index + v->input_buffer_size <= v->input_buffer_allocated);
+        assert(v->input_buffer_index + v->input_buffer_size <= MALLOC_SIZEOF_SAFE(v->input_buffer));
 
         begin = v->input_buffer + v->input_buffer_index;
 
@@ -1257,12 +1254,12 @@ static int varlink_enqueue_json(Varlink *v, JsonVariant *m) {
 
                 free_and_replace(v->output_buffer, text);
 
-                v->output_buffer_size = v->output_buffer_allocated = r + 1;
+                v->output_buffer_size = r + 1;
                 v->output_buffer_index = 0;
 
         } else if (v->output_buffer_index == 0) {
 
-                if (!GREEDY_REALLOC(v->output_buffer, v->output_buffer_allocated, v->output_buffer_size + r + 1))
+                if (!GREEDY_REALLOC(v->output_buffer, v->output_buffer_size + r + 1))
                         return -ENOMEM;
 
                 memcpy(v->output_buffer + v->output_buffer_size, text, r + 1);
@@ -1279,7 +1276,7 @@ static int varlink_enqueue_json(Varlink *v, JsonVariant *m) {
                 memcpy(mempcpy(n, v->output_buffer + v->output_buffer_index, v->output_buffer_size), text, r + 1);
 
                 free_and_replace(v->output_buffer, n);
-                v->output_buffer_allocated = v->output_buffer_size = new_size;
+                v->output_buffer_size = new_size;
                 v->output_buffer_index = 0;
         }
 
index 7ac049638202e9bd68d3d3047fd171328dbeddf1..a050e09c1788ae6b69b62b2470a5de589e30f03c 100644 (file)
@@ -131,7 +131,6 @@ int list_jobs(int argc, char *argv[], void *userdata) {
         _cleanup_free_ struct job_info *jobs = NULL;
         const char *name, *type, *state;
         bool skipped = false;
-        size_t size = 0;
         unsigned c = 0;
         sd_bus *bus;
         uint32_t id;
@@ -157,7 +156,7 @@ int list_jobs(int argc, char *argv[], void *userdata) {
                         continue;
                 }
 
-                if (!GREEDY_REALLOC(jobs, size, c + 1))
+                if (!GREEDY_REALLOC(jobs, c + 1))
                         return log_oom();
 
                 jobs[c++] = job;
index 2e891b103c4d97c66d813a7e6f6507ef9c7dcb0b..1a2f2d7153d6a8b4c72133f2a242d8b81ef6878d 100644 (file)
@@ -93,7 +93,6 @@ static int get_machine_list(
         struct machine_info *machine_infos = NULL;
         _cleanup_strv_free_ char **m = NULL;
         _cleanup_free_ char *hn = NULL;
-        size_t sz = 0;
         char **i;
         int c = 0, r;
 
@@ -102,7 +101,7 @@ static int get_machine_list(
                 return log_oom();
 
         if (output_show_machine(hn, patterns)) {
-                if (!GREEDY_REALLOC0(machine_infos, sz, c+1))
+                if (!GREEDY_REALLOC0(machine_infos, c+1))
                         return log_oom();
 
                 machine_infos[c].is_host = true;
@@ -126,7 +125,7 @@ static int get_machine_list(
                 if (!streq_ptr(class, "container"))
                         continue;
 
-                if (!GREEDY_REALLOC0(machine_infos, sz, c+1)) {
+                if (!GREEDY_REALLOC0(machine_infos, c+1)) {
                         free_machines_list(machine_infos, c);
                         return log_oom();
                 }
index 1bf2fc18f3f0ed0cdfc88dea041e6cbd1fc25b56..fd10e7965b48d8f776cefa025a7049d8a66acfe6 100644 (file)
@@ -136,7 +136,6 @@ static int output_unit_file_list(const UnitFileList *units, unsigned c) {
 int list_unit_files(int argc, char *argv[], void *userdata) {
         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
         _cleanup_free_ UnitFileList *units = NULL;
-        size_t size = 0;
         unsigned c = 0;
         const char *state;
         char *path;
@@ -234,7 +233,7 @@ int list_unit_files(int argc, char *argv[], void *userdata) {
 
                 while ((r = sd_bus_message_read(reply, "(ss)", &path, &state)) > 0) {
 
-                        if (!GREEDY_REALLOC(units, size, c + 1))
+                        if (!GREEDY_REALLOC(units, c + 1))
                                 return log_oom();
 
                         units[c] = (struct UnitFileList) {
index 135d8388a3ffd1b152f9a1890974d543b9922fe4..1efb569971e4d60b88d4f739128ec7a6559e67c9 100644 (file)
@@ -432,7 +432,6 @@ int list_sockets(int argc, char *argv[], void *userdata) {
         _cleanup_free_ UnitInfo *unit_infos = NULL;
         _cleanup_free_ struct socket_info *socket_infos = NULL;
         unsigned cs = 0;
-        size_t size = 0;
         int r, n;
         sd_bus *bus;
 
@@ -468,7 +467,7 @@ int list_sockets(int argc, char *argv[], void *userdata) {
                                 goto cleanup;
                         }
 
-                        if (!GREEDY_REALLOC(socket_infos, size, cs + c)) {
+                        if (!GREEDY_REALLOC(socket_infos, cs + c)) {
                                 r = log_oom();
                                 goto cleanup;
                         }
@@ -695,7 +694,6 @@ int list_timers(int argc, char *argv[], void *userdata) {
         _cleanup_strv_free_ char **timers_with_suffix = NULL;
         _cleanup_free_ struct timer_info *timer_infos = NULL;
         _cleanup_free_ UnitInfo *unit_infos = NULL;
-        size_t size = 0;
         int n, c = 0;
         dual_timestamp nw;
         sd_bus *bus;
@@ -736,7 +734,7 @@ int list_timers(int argc, char *argv[], void *userdata) {
 
                         get_last_trigger(bus, u->unit_path, &last);
 
-                        if (!GREEDY_REALLOC(timer_infos, size, c+1)) {
+                        if (!GREEDY_REALLOC(timer_infos, c+1)) {
                                 r = log_oom();
                                 goto cleanup;
                         }
index d1490c906d080a0f79023cd4ebfeb3c3344e682f..99f1394bcfa631382ad73fe0e2c7640e8bc71bca 100644 (file)
@@ -158,7 +158,6 @@ int get_unit_list(
         _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
-        size_t size = c;
         int r;
         bool fallback = false;
 
@@ -218,7 +217,7 @@ int get_unit_list(
                 if (!output_show_unit(&u, fallback ? patterns : NULL))
                         continue;
 
-                if (!GREEDY_REALLOC(*unit_infos, size, c+1))
+                if (!GREEDY_REALLOC(*unit_infos, c+1))
                         return log_oom();
 
                 (*unit_infos)[c++] = u;
@@ -261,17 +260,16 @@ int expand_unit_names(sd_bus *bus, char **names, const char* suffix, char ***ret
         if (expanded) {
                 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
                 _cleanup_free_ UnitInfo *unit_infos = NULL;
-                size_t allocated, n;
+                size_t n;
 
                 r = get_unit_list(bus, NULL, globs, &unit_infos, 0, &reply);
                 if (r < 0)
                         return r;
 
                 n = strv_length(mangled);
-                allocated = n + 1;
 
                 for (int i = 0; i < r; i++) {
-                        if (!GREEDY_REALLOC(mangled, allocated, n+2))
+                        if (!GREEDY_REALLOC(mangled, n+2))
                                 return log_oom();
 
                         mangled[n] = strdup(unit_infos[i].id);
index 3b77dd98ca4532c09b6d9408be4220bf8be307e2..2baae0cb9ba4a44f149e4255fa6413a7e45af6d0 100644 (file)
@@ -24,31 +24,31 @@ static void test_alloca(void) {
 
 static void test_GREEDY_REALLOC(void) {
         _cleanup_free_ int *a = NULL, *b = NULL;
-        size_t n_allocated = 0, i, j;
+        size_t i, j;
 
         /* Give valgrind a chance to verify our realloc() operations */
 
         for (i = 0; i < 20480; i++) {
-                assert_se(GREEDY_REALLOC(a, n_allocated, i + 1));
-                assert_se(n_allocated >= i + 1);
-                assert_se(malloc_usable_size(a) >= (i + 1) * sizeof(int));
+                assert_se(GREEDY_REALLOC(a, i + 1));
+                assert_se(MALLOC_ELEMENTSOF(a) >= i + 1);
+                assert_se(MALLOC_SIZEOF_SAFE(a) >= (i + 1) * sizeof(int));
                 a[i] = (int) i;
-                assert_se(GREEDY_REALLOC(a, n_allocated, i / 2));
-                assert_se(n_allocated >= i / 2);
-                assert_se(malloc_usable_size(a) >= (i / 2) * sizeof(int));
+                assert_se(GREEDY_REALLOC(a, i / 2));
+                assert_se(MALLOC_ELEMENTSOF(a) >= i / 2);
+                assert_se(MALLOC_SIZEOF_SAFE(a) >= (i / 2) * sizeof(int));
         }
 
         for (j = 0; j < i / 2; j++)
                 assert_se(a[j] == (int) j);
 
-        for (i = 30, n_allocated = 0; i < 20480; i += 7) {
-                assert_se(GREEDY_REALLOC(b, n_allocated, i + 1));
-                assert_se(n_allocated >= i + 1);
-                assert_se(malloc_usable_size(b) >= (i + 1) * sizeof(int));
+        for (i = 30; i < 20480; i += 7) {
+                assert_se(GREEDY_REALLOC(b, i + 1));
+                assert_se(MALLOC_ELEMENTSOF(b) >= i + 1);
+                assert_se(MALLOC_SIZEOF_SAFE(b) >= (i + 1) * sizeof(int));
                 b[i] = (int) i;
-                assert_se(GREEDY_REALLOC(b, n_allocated, i / 2));
-                assert_se(n_allocated >= i / 2);
-                assert_se(malloc_usable_size(b) >= (i / 2) * sizeof(int));
+                assert_se(GREEDY_REALLOC(b, i / 2));
+                assert_se(MALLOC_ELEMENTSOF(b) >= i / 2);
+                assert_se(MALLOC_SIZEOF_SAFE(b) >= (i / 2) * sizeof(int));
         }
 
         for (j = 30; j < i / 2; j += 7)
@@ -58,8 +58,8 @@ static void test_GREEDY_REALLOC(void) {
 static void test_memdup_multiply_and_greedy_realloc(void) {
         static const int org[] = { 1, 2, 3 };
         _cleanup_free_ int *dup;
+        size_t i;
         int *p;
-        size_t i, allocated = 3;
 
         dup = memdup_suffix0_multiply(org, sizeof(int), 3);
         assert_se(dup);
@@ -75,16 +75,18 @@ static void test_memdup_multiply_and_greedy_realloc(void) {
         assert_se(dup[1] == 2);
         assert_se(dup[2] == 3);
 
+        memzero(dup + 3, malloc_usable_size(dup) - sizeof(int) * 3);
+
         p = dup;
-        assert_se(greedy_realloc0((void**) &dup, &allocated, 2, sizeof(int)) == p);
+        assert_se(GREEDY_REALLOC0(dup, 2) == p);
 
-        p = (int *) greedy_realloc0((void**) &dup, &allocated, 10, sizeof(int));
+        p = GREEDY_REALLOC0(dup, 10);
         assert_se(p == dup);
-        assert_se(allocated >= 10);
+        assert_se(MALLOC_ELEMENTSOF(p) >= 10);
         assert_se(p[0] == 1);
         assert_se(p[1] == 2);
         assert_se(p[2] == 3);
-        for (i = 3; i < allocated; i++)
+        for (i = 3; i < MALLOC_ELEMENTSOF(p); i++)
                 assert_se(p[i] == 0);
 }
 
@@ -139,14 +141,15 @@ static void test_auto_erase_memory(void) {
         /* print address of p2, else e.g. clang-11 will optimize it out */
         log_debug("p1: %p p2: %p", &p1, &p2);
 
-        assert_se(p1 = new(uint8_t, 1024));
-        assert_se(p2 = new(uint8_t, 1024));
+        assert_se(p1 = new(uint8_t, 4703)); /* use prime size, to ensure that there will be free space at the
+                                             * end of the allocation, since malloc() enforces alignment */
+        assert_se(p2 = new(uint8_t, 4703));
 
-        assert_se(genuine_random_bytes(p1, 1024, RANDOM_BLOCK) == 0);
+        assert_se(genuine_random_bytes(p1, 4703, RANDOM_BLOCK) == 0);
 
         /* before we exit the scope, do something with this data, so that the compiler won't optimize this away */
-        memcpy(p2, p1, 1024);
-        for (size_t i = 0; i < 1024; i++)
+        memcpy(p2, p1, 4703);
+        for (size_t i = 0; i < 4703; i++)
                 assert_se(p1[i] == p2[i]);
 }
 
index 42ed53adc35b70bb13d8e0f71cd758ef2dae6682..eddb04795133bc5682fbc653fa20a2e2c274941a 100644 (file)
@@ -354,15 +354,13 @@ static void test_byaddr(void *handle,
 
 static int make_addresses(struct local_address **addresses) {
         int n;
-        size_t n_alloc;
         _cleanup_free_ struct local_address *addrs = NULL;
 
         n = local_addresses(NULL, 0, AF_UNSPEC, &addrs);
         if (n < 0)
                 log_info_errno(n, "Failed to query local addresses: %m");
 
-        n_alloc = n; /* we _can_ do that */
-        assert_se(GREEDY_REALLOC(addrs, n_alloc, n + 3));
+        assert_se(GREEDY_REALLOC(addrs, n + 3));
 
         addrs[n++] = (struct local_address) { .family = AF_INET,
                                               .address.in = { htobe32(0x7F000001) } };
@@ -406,7 +404,6 @@ static int parse_argv(int argc, char **argv,
 
         _cleanup_strv_free_ char **modules = NULL, **names = NULL;
         _cleanup_free_ struct local_address *addrs = NULL;
-        size_t n_allocated = 0;
         const char *p;
         int r, n = 0;
 
@@ -446,7 +443,7 @@ static int parse_argv(int argc, char **argv,
                                 if (r < 0)
                                         return r;
                         } else {
-                                assert_se(GREEDY_REALLOC0(addrs, n_allocated, n + 1));
+                                assert_se(GREEDY_REALLOC0(addrs, n + 1));
 
                                 addrs[n++] = (struct local_address) { .family = family,
                                                                       .address = address };
index 1e09c256b0fad944549c6a9003ff8e0c84168b48..3eac712b0bf76413de0f3607a00afec0e579a126 100644 (file)
@@ -148,7 +148,6 @@ typedef struct Item {
 typedef struct ItemArray {
         Item *items;
         size_t n_items;
-        size_t allocated;
 
         struct ItemArray *parent;
         Set *children;
@@ -2952,7 +2951,7 @@ static int parse_line(
                 }
         }
 
-        if (!GREEDY_REALLOC(existing->items, existing->allocated, existing->n_items + 1))
+        if (!GREEDY_REALLOC(existing->items, existing->n_items + 1))
                 return log_oom();
 
         existing->items[existing->n_items++] = i;
index f2d54b297dad91ed3ec9103f5e4fb0414729175e..df0ee6a3bb2653adc20dfcbd750a861ca7fd5685 100644 (file)
@@ -89,7 +89,6 @@ typedef struct Context {
 
         Feature *drive_features;
         size_t n_drive_feature;
-        size_t n_allocated;
 
         Feature media_feature;
         bool has_media;
@@ -137,7 +136,7 @@ static int set_drive_feature(Context *c, Feature f) {
         if (drive_has_feature(c, f))
                 return 0;
 
-        if (!GREEDY_REALLOC(c->drive_features, c->n_allocated, c->n_drive_feature + 1))
+        if (!GREEDY_REALLOC(c->drive_features, c->n_drive_feature + 1))
                 return -ENOMEM;
 
         c->drive_features[c->n_drive_feature++] = f;
index 33fdecca50eeec1356dde4e932df66b95af6a643..fa7f04f14ce8cda0721dd426fece6d2c550f2290 100644 (file)
@@ -66,8 +66,8 @@ static int sysattr_compare(const SysAttr *a, const SysAttr *b) {
 
 static int print_all_attributes(sd_device *device, bool is_parent) {
         _cleanup_free_ SysAttr *sysattrs = NULL;
-        size_t n_items = 0, n_allocated = 0;
         const char *name, *value;
+        size_t n_items = 0;
 
         value = NULL;
         (void) sd_device_get_devpath(device, &value);
@@ -105,7 +105,7 @@ static int print_all_attributes(sd_device *device, bool is_parent) {
                 if (len > 0)
                         continue;
 
-                if (!GREEDY_REALLOC(sysattrs, n_allocated, n_items + 1))
+                if (!GREEDY_REALLOC(sysattrs, n_items + 1))
                         return log_oom();
 
                 sysattrs[n_items] = (SysAttr) {
index 00d8b76539844a0ab218c7e8db454c9acd59192c..5068c0ebb39da4e46b64aa2e320538dd7fa2edc3 100644 (file)
@@ -188,7 +188,6 @@ static int strv_strndup_unescape_and_push(
                 const char *filename,
                 unsigned line,
                 char ***sv,
-                size_t *n_allocated,
                 size_t *n,
                 const char *start,
                 const char *end) {
@@ -207,7 +206,7 @@ static int strv_strndup_unescape_and_push(
         if (r < 0)
                 return r;
 
-        if (!greedy_realloc((void**) sv, n_allocated, *n + 2, sizeof(char*))) /* One extra for NULL */
+        if (!GREEDY_REALLOC(*sv, *n + 2)) /* One extra for NULL */
                 return log_oom();
 
         (*sv)[*n] = TAKE_PTR(copy);
@@ -243,10 +242,10 @@ static int xdg_config_parse_strv(
                 return 0;
         }
 
-        size_t n = 0, n_allocated = 0;
+        size_t n = 0;
         _cleanup_strv_free_ char **sv = NULL;
 
-        if (!GREEDY_REALLOC0(sv, n_allocated, 1))
+        if (!GREEDY_REALLOC0(sv, 1))
                 return log_oom();
 
         /* We cannot use strv_split because it does not handle escaping correctly. */
@@ -265,7 +264,7 @@ static int xdg_config_parse_strv(
 
                 if (*end == ';') {
                         r = strv_strndup_unescape_and_push(unit, filename, line,
-                                                           &sv, &n_allocated, &n,
+                                                           &sv, &n,
                                                            start, end);
                         if (r < 0)
                                 return r;
@@ -276,7 +275,7 @@ static int xdg_config_parse_strv(
 
         /* Handle the trailing entry after the last separator */
         r = strv_strndup_unescape_and_push(unit, filename, line,
-                                           &sv, &n_allocated, &n,
+                                           &sv, &n,
                                            start, end);
         if (r < 0)
                 return r;