_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(
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);
_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;
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;
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;
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;
}
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) \
({ \
};
_cleanup_free_ uint64_t *items = NULL;
- size_t n_items = 0, n_allocated = 0;
+ size_t n_items = 0;
int r;
assert(fd >= 0);
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;
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);
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 */
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);
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)
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;
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;
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;
n_key = 0;
value = NULL;
- value_alloc = n_value = 0;
+ n_value = 0;
} else if (c == '\'')
state = SINGLE_QUOTE_VALUE;
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;
n_key = 0;
value = NULL;
- value_alloc = n_value = 0;
+ n_value = 0;
} else if (c == '\\') {
state = VALUE_ESCAPE;
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;
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;
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;
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;
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;
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;
* 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) {
/* 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;
}
for (;; (*p)++, c = **p) {
if (backslash) {
- if (!GREEDY_REALLOC(s, allocated, sz+7))
+ if (!GREEDY_REALLOC(s, sz+7))
return -ENOMEM;
if (c == 0) {
backslash = true;
break;
} else {
- if (!GREEDY_REALLOC(s, allocated, sz+2))
+ if (!GREEDY_REALLOC(s, sz+2))
return -ENOMEM;
s[sz++] = c;
goto finish;
} else {
- if (!GREEDY_REALLOC(s, allocated, sz+2))
+ if (!GREEDY_REALLOC(s, sz+2))
return -ENOMEM;
s[sz++] = c;
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);
* 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;
}
}
if (ret) {
- if (!GREEDY_REALLOC(buffer, allocated, n + 2))
+ if (!GREEDY_REALLOC(buffer, n + 2))
return -ENOMEM;
buffer[n] = c;
}
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);
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);
typedef struct CacheMem {
const void **ptr;
- size_t n_populated, n_allocated;
+ size_t n_populated;
bool active:1;
} CacheMem;
}
/* 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;
}
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);
}
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;
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) {
iovw->iovec = mfree(iovw->iovec);
iovw->count = 0;
- iovw->size_bytes = 0;
}
struct iovec_wrapper *iovw_free_free(struct iovec_wrapper *iovw) {
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);
struct iovec_wrapper {
struct iovec *iovec;
size_t count;
- size_t size_bytes;
};
struct iovec_wrapper *iovw_new(void);
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;
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);
}
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;
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;
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;
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);
if (r == 0)
break;
- if (!GREEDY_REALLOC(l, allocated, n + 2))
+ if (!GREEDY_REALLOC(l, n + 2))
return -ENOMEM;
l[n++] = TAKE_PTR(word);
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);
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);
* 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);
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);
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);
if (!zones)
return -ENOMEM;
- n_allocated = 2;
n_zones = 1;
f = fopen("/usr/share/zoneinfo/zone1970.tab", "re");
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);
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);
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;
}
}
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);
assert(type == 'e');
- if (!GREEDY_REALLOC(elements, n_allocated, n_elements + 2)) {
+ if (!GREEDY_REALLOC(elements, n_elements + 2)) {
r = log_oom();
goto finish;
}
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) {
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);
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. */
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;
}
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;
}
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);
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;
}
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;
}
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;
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);
p += 2;
p += strspn(p, WHITESPACE);
- if (!GREEDY_REALLOC(n, nbufsize, nlen + 2))
+ if (!GREEDY_REALLOC(n, nlen + 2))
return log_oom();
w = strdup(";");
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);
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);
continue;
}
- if (!GREEDY_REALLOC(n, nbufsize, nlen + 2))
+ if (!GREEDY_REALLOC(n, nlen + 2))
return log_oom();
n[nlen++] = TAKE_PTR(k);
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);
continue;
}
- if (!GREEDY_REALLOC(n, nbufsize, nlen + 2))
+ if (!GREEDY_REALLOC(n, nlen + 2))
return log_oom();
n[nlen++] = TAKE_PTR(k);
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;
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)
args[length++] = '"';
}
- if (!GREEDY_REALLOC(args, allocated, length + 1))
+ if (!GREEDY_REALLOC(args, length + 1))
return log_oom();
args[length++] = 0;
_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");
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] = ' ';
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);
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
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;
n += a;
}
- if (!GREEDY_REALLOC(result, allocated, n + 1))
+ if (!GREEDY_REALLOC(result, n + 1))
return NULL;
result[n] = 0;
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;
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) {
_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;
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);
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);
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;
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;
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);
j->payload = NULL;
j->payload_size = 0;
- j->payload_allocated = 0;
j->state = PULL_JOB_RUNNING;
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;
}
uint8_t *payload;
size_t payload_size;
- size_t payload_allocated;
int disk_fd;
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);
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) {
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);
* 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);
struct RemoteServer {
RemoteSource **sources;
- size_t sources_size;
size_t active;
sd_event *events;
if (buffer) {
s->buffer = memdup_suffix0(buffer, size);
assert_se(s->buffer);
- s->buffer_size = size + 1;
}
}
/* 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);
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;
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);
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;
} 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;
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;
} 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);
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;
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;
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 (;;) {
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);
}
}
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");
}
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");
}
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;
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;
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
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;
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);
*
* 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;
}
/* 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))) {
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++) {
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;
(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))
uint64_t seqnum;
char *buffer;
- size_t buffer_size;
JournalRateLimit *ratelimit;
usec_t sync_interval_usec;
char *buffer;
size_t length;
- size_t allocated;
sd_event_source *event_source;
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;
}
/* 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);
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 */
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);
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;
}
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);
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)
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)
}
if (n) {
- if (!GREEDY_REALLOC(ret, allocated, n + 1))
+ if (!GREEDY_REALLOC(ret, n + 1))
return -ENOMEM;
ret[n] = 0;
}
_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;
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)
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 */
if (r == 0)
break;
- if (!GREEDY_REALLOC(routes, allocated, size + 1))
+ if (!GREEDY_REALLOC(routes, size + 1))
return -ENOMEM;
tok = word;
}
*ret_size = size;
- *ret_allocated = allocated;
*ret = TAKE_PTR(routes);
return 0;
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);
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;
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) {
/* 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;
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;
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;
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;
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;
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)
return -EBADMSG;
}
- if (!GREEDY_REALLOC(name, allocated, n + 1))
+ if (!GREEDY_REALLOC(name, n + 1))
return -ENOMEM;
name[n] = 0;
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);
bool information_request;
bool iaid_set;
be16_t *req_opts;
- size_t req_opts_allocated;
size_t req_opts_len;
char *fqdn;
char *mudurl;
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);
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;
lease->ntp = mfree(lease->ntp);
lease->ntp_count = 0;
- lease->ntp_allocated = 0;
while ((r = dhcp6_option_parse(&optval, &optlen, &subopt, &sublen,
&subval)) >= 0) {
s = dhcp6_option_parse_ip6addrs(subval, sublen,
&lease->ntp,
- lease->ntp_count,
- &lease->ntp_allocated);
+ lease->ntp_count);
if (s < 0)
return s;
/* 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;
if (missing & SD_BUS_CREDS_SUPPLEMENTARY_GIDS) {
p = startswith(line, "Groups:");
if (p) {
- size_t allocated = 0;
-
for (;;) {
unsigned long g;
int n = 0;
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;
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 */
unsigned *ret_n_components) {
struct bus_match_component *components = NULL;
- size_t components_allocated = 0;
unsigned n_components = 0;
int r;
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;
}
}
- if (!GREEDY_REALLOC(value, value_allocated, j + 2)) {
+ if (!GREEDY_REALLOC(value, j + 2)) {
r = -ENOMEM;
goto fail;
}
} else
u = 0;
- if (!GREEDY_REALLOC(components, components_allocated, n_components + 1)) {
+ if (!GREEDY_REALLOC(components, n_components + 1)) {
r = -ENOMEM;
goto fail;
}
message_free_last_container(m);
m->containers = mfree(m->containers);
- m->containers_allocated = 0;
m->root_container.index = 0;
}
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;
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;
}
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))
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;
struct bus_container root_container, *containers;
size_t n_containers;
- size_t containers_allocated;
struct iovec *iovec;
struct iovec iovec_fixed[2];
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;
* 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;
}
goto fail;
}
- if (!GREEDY_REALLOC(new_watches, n_allocated, n + 1)) {
+ if (!GREEDY_REALLOC(new_watches, n + 1)) {
r = -ENOMEM;
goto fail;
}
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) {
};
/* 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);
}
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);
a++;
}
- if (!GREEDY_REALLOC(r, allocated, n + 2))
+ if (!GREEDY_REALLOC(r, n + 2))
return -ENOMEM;
r[n++] = c;
char *path = NULL;
unsigned n_argv = 0, j;
char **argv = NULL;
- size_t allocated = 0;
int r;
assert(b);
(*p)++;
if (ul >= n_argv) {
- if (!GREEDY_REALLOC0(argv, allocated, ul + 2)) {
+ if (!GREEDY_REALLOC0(argv, ul + 2)) {
r = -ENOMEM;
goto fail;
}
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;
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);
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;
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);
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);
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;
unsigned n_sources;
struct epoll_event *event_queue;
- size_t event_queue_allocated;
LIST_HEAD(sd_event_source, sources);
}
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;
}
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;
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)
/* 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
#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;
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;
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;
#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;
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;
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)
}
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;
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))
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();
};
ZSTD_outBuffer output = {
.dst = *dst,
- .size = *dst_alloc_size,
+ .size = MALLOC_SIZEOF_SAFE(*dst),
};
size_t k = ZSTD_decompressStream(dctx, &output, &input);
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);
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
#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;
/* 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;
}
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))
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 = {
};
ZSTD_outBuffer output = {
.dst = *buffer,
- .size = *buffer_size,
+ .size = MALLOC_SIZEOF_SAFE(*buffer),
};
size_t k;
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
}
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);
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;
#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;
uint64_t compress_threshold_bytes;
#if HAVE_COMPRESSION
void *compress_buffer;
- size_t compress_buffer_size;
#endif
#if HAVE_GCRYPT
uint64_t fields_offset;
uint64_t fields_hash_table_index;
char *fields_buffer;
- size_t fields_buffer_allocated;
int flags;
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;
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;
}
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));
#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",
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;
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;
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);
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
_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);
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);
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);
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;
} 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,
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 */",
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);
}
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);
}
}
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;
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
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)
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;
}
/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#include <malloc.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <unistd.h>
*/
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);
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);
else
len = (size_t) r;
- if (len > rtnl->rbuffer_allocated)
+ if (len > allocated)
/* message did not fit in read buffer */
return -EIO;
/* 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);
"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;
"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;
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);
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);
*/
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");
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);
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;
}
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
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);
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;
LIST_HEAD(Session, sessions);
Session **positions;
- size_t position_count;
bool in_gc_queue:1;
bool started:1;
(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 (;;) {
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;
}
_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;
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;
_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);
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);
n++;
}
- if (!GREEDY_REALLOC0(l, bufsize, n + 1))
+ if (!GREEDY_REALLOC0(l, n + 1))
return log_oom();
*list = TAKE_PTR(l);
_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);
}
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);
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);
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;
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;
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++)
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;
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;
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");
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);
}
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);
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;
}
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);
}
_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"));
_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"));
_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);
}
}
_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;
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++]);
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;
context->free_areas = mfree(context->free_areas);
context->n_free_areas = 0;
- context->n_allocated_free_areas = 0;
}
static Context *context_free(Context *context) {
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);
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;
if (r < 0)
return r;
- if (!GREEDY_REALLOC(l, n_allocated, n+2)) {
+ if (!GREEDY_REALLOC(l, n+2)) {
free(p);
return -ENOMEM;
}
typedef struct PStoreList {
PStoreEntry *entries;
- size_t n_allocated;
size_t n_entries;
} PStoreList;
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;
/* 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;
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) {
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;
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 };
_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);
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)
return -EBADMSG;
}
- if (!GREEDY_REALLOC(name, allocated, n + 1))
+ if (!GREEDY_REALLOC(name, n + 1))
return -ENOMEM;
name[n] = 0;
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;
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);
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <arpa/inet.h>
+#include <malloc.h>
#include <netinet/in.h>
#include <sys/socket.h>
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 */
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")));
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"));
if (!ret->bitmaps)
return mfree(ret);
- ret->n_bitmaps = ret->bitmaps_allocated = b->n_bitmaps;
+ ret->n_bitmaps = b->n_bitmaps;
return ret;
}
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;
b->bitmaps = mfree(b->bitmaps);
b->n_bitmaps = 0;
- b->bitmaps_allocated = 0;
}
bool bitmap_iterate(const Bitmap *b, Iterator *i, unsigned *n) {
typedef struct Bitmap {
uint64_t *bitmaps;
size_t n_bitmaps;
- size_t bitmaps_allocated;
} Bitmap;
Bitmap* bitmap_new(void);
size_t *n_entries) {
_cleanup_strv_free_ char **files = NULL;
- size_t n_allocated = *n_entries;
char **f;
int r;
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);
size_t *n_entries) {
_cleanup_(closedirp) DIR *d = NULL;
- size_t n_allocated = *n_entries;
struct dirent *de;
int r;
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);
"auto-reboot-to-firmware-setup", "Reboot Into Firmware Interface",
};
- size_t n_allocated;
char **i;
assert(config);
/* 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;
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) {
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);
uint32_t prog_type;
size_t n_instructions;
- size_t allocated;
struct bpf_insn *instructions;
char *attached_path;
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);
if (r == 0)
break;
- if (!GREEDY_REALLOC(dns, allocated, n+1)) {
+ if (!GREEDY_REALLOC(dns, n+1)) {
r = -ENOMEM;
goto clear;
}
_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
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) {
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);
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;
}
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);
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++)
} 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)
}
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)
struct DecryptedImage {
DecryptedPartition *decrypted;
size_t n_decrypted;
- size_t n_allocated;
};
#endif
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);
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.
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;
}
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);
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;
}
_cleanup_closedir_ DIR *dir = NULL;
_cleanup_free_ uint16_t *list = NULL;
struct dirent *de;
- size_t alloc = 0;
int count = 0;
assert(options);
if (id < 0)
continue;
- if (!GREEDY_REALLOC(list, alloc, count + 1))
+ if (!GREEDY_REALLOC(list, count + 1))
return -ENOMEM;
list[count++] = id;
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;
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)
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]);
}
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;
}
}
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;
}
_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);
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);
_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;
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);
_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;
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);
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);
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;
}
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;
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <errno.h>
+#include <malloc.h>
#include <unistd.h>
#include "alloc-util.h"
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;
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 (;;) {
/* 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;
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);
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;
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);
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)
target);
else {
log_debug("Reallocated buffer from %zu to %zu bytes",
- imp->size, target);
+ old_size, target);
imp->buf = tmp;
- imp->size = target;
}
}
}
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 */
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);
c += 5;
- if (!GREEDY_REALLOC(s, allocated, n + 5))
+ if (!GREEDY_REALLOC(s, n + 5))
return -ENOMEM;
if (!utf16_is_surrogate(x))
} else
return -EINVAL;
- if (!GREEDY_REALLOC(s, allocated, n + 2))
+ if (!GREEDY_REALLOC(s, n + 2))
return -ENOMEM;
s[n++] = ch;
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);
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 */
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;
p = *input;
- if (!GREEDY_REALLOC(stack, n_stack_allocated, n_stack))
+ if (!GREEDY_REALLOC(stack, n_stack))
return -ENOMEM;
stack[0] = (JsonStack) {
goto finish;
}
- if (!GREEDY_REALLOC(stack, n_stack_allocated, n_stack+1)) {
+ if (!GREEDY_REALLOC(stack, n_stack+1)) {
r = -ENOMEM;
goto finish;
}
goto finish;
}
- if (!GREEDY_REALLOC(stack, n_stack_allocated, n_stack+1)) {
+ if (!GREEDY_REALLOC(stack, n_stack+1)) {
r = -ENOMEM;
goto finish;
}
(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;
}
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) {
goto finish;
}
- if (!GREEDY_REALLOC(stack, n_stack_allocated, n_stack+1)) {
+ if (!GREEDY_REALLOC(stack, n_stack+1)) {
r = -ENOMEM;
goto finish;
}
goto finish;
}
- if (!GREEDY_REALLOC(stack, n_stack_allocated, n_stack+1)) {
+ if (!GREEDY_REALLOC(stack, n_stack+1)) {
r = -ENOMEM;
goto finish;
}
/* 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;
}
_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;
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;
static int add_local_gateway(
struct local_address **list,
size_t *n_list,
- size_t *n_allocated,
int af,
int ifindex,
uint32_t metric,
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) {
_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)
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;
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;
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;
}
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
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) {
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) {
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");
/* 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;
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;
#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;
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;
}
*(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;
}
/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#include <malloc.h>
#include <sys/poll.h>
#include "alloc-util.h"
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;
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 {
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) {
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;
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);
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;
}
_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;
continue;
}
- if (!GREEDY_REALLOC(jobs, size, c + 1))
+ if (!GREEDY_REALLOC(jobs, c + 1))
return log_oom();
jobs[c++] = job;
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;
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;
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();
}
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;
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) {
_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;
goto cleanup;
}
- if (!GREEDY_REALLOC(socket_infos, size, cs + c)) {
+ if (!GREEDY_REALLOC(socket_infos, cs + c)) {
r = log_oom();
goto cleanup;
}
_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;
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;
}
_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;
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;
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);
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)
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);
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);
}
/* 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]);
}
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) } };
_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;
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 };
typedef struct ItemArray {
Item *items;
size_t n_items;
- size_t allocated;
struct ItemArray *parent;
Set *children;
}
}
- 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;
Feature *drive_features;
size_t n_drive_feature;
- size_t n_allocated;
Feature media_feature;
bool has_media;
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;
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);
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) {
const char *filename,
unsigned line,
char ***sv,
- size_t *n_allocated,
size_t *n,
const char *start,
const char *end) {
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);
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. */
if (*end == ';') {
r = strv_strndup_unescape_and_push(unit, filename, line,
- &sv, &n_allocated, &n,
+ &sv, &n,
start, end);
if (r < 0)
return r;
/* 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;