alloc-util: simplify GREEDY_REALLOC() logic by relying on malloc_usable_size()
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.)
This commit is contained in:
+2
-3
@@ -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) {
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
typedef struct Bitmap {
|
||||
uint64_t *bitmaps;
|
||||
size_t n_bitmaps;
|
||||
size_t bitmaps_allocated;
|
||||
} Bitmap;
|
||||
|
||||
Bitmap* bitmap_new(void);
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -17,7 +17,6 @@ struct BPFProgram {
|
||||
uint32_t prog_type;
|
||||
|
||||
size_t n_instructions;
|
||||
size_t allocated;
|
||||
struct bpf_insn *instructions;
|
||||
|
||||
char *attached_path;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -22,14 +22,14 @@
|
||||
|
||||
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)
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
+9
-10
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 */
|
||||
|
||||
+15
-15
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
+5
-12
@@ -30,18 +30,18 @@
|
||||
#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;
|
||||
}
|
||||
|
||||
+8
-11
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user