tree-wide: replace reallocarray() with GREEDY_REALLOC()

This commit is contained in:
Yu Watanabe
2024-10-08 19:56:09 +09:00
parent 5f9f38b8df
commit 223a67e572
18 changed files with 81 additions and 177 deletions
+3 -6
View File
@@ -602,7 +602,6 @@ static bool map_ok(const sd_bus_error_map *map) {
}
_public_ int sd_bus_error_add_map(const sd_bus_error_map *map) {
const sd_bus_error_map **maps = NULL;
unsigned n = 0;
assert_return(map, -EINVAL);
@@ -613,13 +612,11 @@ _public_ int sd_bus_error_add_map(const sd_bus_error_map *map) {
if (additional_error_maps[n] == map)
return 0;
maps = reallocarray(additional_error_maps, n + 2, sizeof(struct sd_bus_error_map*));
if (!maps)
if (!GREEDY_REALLOC(additional_error_maps, n + 2))
return -ENOMEM;
maps[n] = map;
maps[n+1] = NULL;
additional_error_maps[n] = map;
additional_error_maps[n+1] = NULL;
additional_error_maps = maps;
return 1;
}
+3 -5
View File
@@ -1276,7 +1276,7 @@ static void *message_extend_body(
}
static int message_push_fd(sd_bus_message *m, int fd) {
int *f, copy;
int copy;
assert(m);
@@ -1290,15 +1290,13 @@ static int message_push_fd(sd_bus_message *m, int fd) {
if (copy < 0)
return -errno;
f = reallocarray(m->fds, m->n_fds + 1, sizeof(int));
if (!f) {
if (!GREEDY_REALLOC(m->fds, m->n_fds + 1)) {
m->poisoned = true;
safe_close(copy);
return -ENOMEM;
}
m->fds = f;
m->fds[m->n_fds] = copy;
m->fds[m->n_fds] = copy; /* m->n_fds will be incremented by the caller later */
m->free_fds = true;
return copy;
+11 -19
View File
@@ -1048,9 +1048,8 @@ _public_ int sd_get_sessions(char ***sessions) {
_public_ int sd_get_uids(uid_t **users) {
_cleanup_closedir_ DIR *d = NULL;
int r = 0;
unsigned n = 0;
_cleanup_free_ uid_t *l = NULL;
size_t n = 0;
d = opendir("/run/systemd/users/");
if (!d) {
@@ -1063,38 +1062,31 @@ _public_ int sd_get_uids(uid_t **users) {
}
FOREACH_DIRENT_ALL(de, d, return -errno) {
int k;
uid_t uid;
if (!dirent_is_file(de))
continue;
k = parse_uid(de->d_name, &uid);
if (k < 0)
if (parse_uid(de->d_name, &uid) < 0)
continue;
if (users) {
if ((unsigned) r >= n) {
uid_t *t;
if (!GREEDY_REALLOC(l, n + 1))
return -ENOMEM;
n = MAX(16, 2*r);
t = reallocarray(l, n, sizeof(uid_t));
if (!t)
return -ENOMEM;
l[n] = uid;
}
l = t;
}
assert((unsigned) r < n);
l[r++] = uid;
} else
r++;
n++;
}
if (n > INT_MAX)
return -EOVERFLOW;
if (users)
*users = TAKE_PTR(l);
return r;
return (int) n;
}
_public_ int sd_get_machine_names(char ***machines) {