summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSusant Sahani <ssahani@vmware.com>2021-01-27 08:19:39 +0100
committerGitHub <noreply@github.com>2021-01-27 08:19:39 +0100
commitfe96c0f86d15e844d74d539c6cff7f971078cf84 (patch)
treecb4f1a6248826c163b766c1be5d82d882f9fbb28 /src
parent37baf8db563f085e8867a0764f9832dcd7e94605 (diff)
downloadsystemd-fe96c0f86d15e844d74d539c6cff7f971078cf84.tar.gz
treewide: tighten variable scope in loops (#18372)
Also use _cleanup_free_ in one more place.
Diffstat (limited to 'src')
-rw-r--r--src/basic/cgroup-util.c8
-rw-r--r--src/basic/fd-util.c8
-rw-r--r--src/basic/format-util.c4
-rw-r--r--src/basic/io-util.c8
-rw-r--r--src/basic/parse-util.c3
-rw-r--r--src/basic/rlimit-util.c4
-rw-r--r--src/basic/sigbus.c4
-rw-r--r--src/basic/signal-util.c4
-rw-r--r--src/basic/string-util.c5
-rw-r--r--src/basic/time-util.c6
-rw-r--r--src/basic/tmpfile-util.c6
-rw-r--r--src/basic/unit-file.c3
-rw-r--r--src/basic/utf8.c15
-rw-r--r--src/basic/virt.c3
-rw-r--r--src/core/dbus-cgroup.c3
-rw-r--r--src/core/dbus-execute.c9
-rw-r--r--src/core/dbus-job.c4
-rw-r--r--src/core/dbus-manager.c6
-rw-r--r--src/core/device.c3
-rw-r--r--src/core/execute.c4
-rw-r--r--src/core/namespace.c26
-rw-r--r--src/core/socket.c6
-rw-r--r--src/core/swap.c4
-rw-r--r--src/core/target.c3
-rw-r--r--src/coredump/coredump.c23
-rw-r--r--src/fuzz/fuzz-main.c8
-rw-r--r--src/libsystemd-network/sd-dhcp-server.c3
-rw-r--r--src/libsystemd/sd-bus/bus-kernel.c4
-rw-r--r--src/libsystemd/sd-daemon/sd-daemon.c4
-rw-r--r--src/libsystemd/sd-device/device-enumerator.c10
30 files changed, 74 insertions, 127 deletions
diff --git a/src/basic/cgroup-util.c b/src/basic/cgroup-util.c
index 375fd1c3be..b567822b7e 100644
--- a/src/basic/cgroup-util.c
+++ b/src/basic/cgroup-util.c
@@ -1131,8 +1131,8 @@ static const char *skip_slices(const char *p) {
}
int cg_path_get_unit(const char *path, char **ret) {
+ _cleanup_free_ char *unit = NULL;
const char *e;
- char *unit;
int r;
assert(path);
@@ -1145,12 +1145,10 @@ int cg_path_get_unit(const char *path, char **ret) {
return r;
/* We skipped over the slices, don't accept any now */
- if (endswith(unit, ".slice")) {
- free(unit);
+ if (endswith(unit, ".slice"))
return -ENXIO;
- }
- *ret = unit;
+ *ret = TAKE_PTR(unit);
return 0;
}
diff --git a/src/basic/fd-util.c b/src/basic/fd-util.c
index a03ba83e19..d63f012ad5 100644
--- a/src/basic/fd-util.c
+++ b/src/basic/fd-util.c
@@ -91,11 +91,9 @@ void safe_close_pair(int p[static 2]) {
}
void close_many(const int fds[], size_t n_fd) {
- size_t i;
-
assert(fds || n_fd <= 0);
- for (i = 0; i < n_fd; i++)
+ for (size_t i = 0; i < n_fd; i++)
safe_close(fds[i]);
}
@@ -179,11 +177,9 @@ int fd_cloexec(int fd, bool cloexec) {
}
_pure_ static bool fd_in_set(int fd, const int fdset[], size_t n_fdset) {
- size_t i;
-
assert(n_fdset == 0 || fdset);
- for (i = 0; i < n_fdset; i++)
+ for (size_t i = 0; i < n_fdset; i++)
if (fdset[i] == fd)
return true;
diff --git a/src/basic/format-util.c b/src/basic/format-util.c
index bf23037792..04ed4be941 100644
--- a/src/basic/format-util.c
+++ b/src/basic/format-util.c
@@ -43,7 +43,7 @@ char *format_bytes_full(char *buf, size_t l, uint64_t t, FormatBytesFlag flag) {
{ "K", UINT64_C(1000) },
};
const suffix_table *table;
- size_t n, i;
+ size_t n;
assert_cc(ELEMENTSOF(table_iec) == ELEMENTSOF(table_si));
@@ -53,7 +53,7 @@ char *format_bytes_full(char *buf, size_t l, uint64_t t, FormatBytesFlag flag) {
table = flag & FORMAT_BYTES_USE_IEC ? table_iec : table_si;
n = ELEMENTSOF(table_iec);
- for (i = 0; i < n; i++)
+ for (size_t i = 0; i < n; i++)
if (t >= table[i].factor) {
if (flag & FORMAT_BYTES_BELOW_POINT) {
snprintf(buf, l,
diff --git a/src/basic/io-util.c b/src/basic/io-util.c
index 4d7405296b..8ea350dcc3 100644
--- a/src/basic/io-util.c
+++ b/src/basic/io-util.c
@@ -319,16 +319,14 @@ int iovw_put_string_field_free(struct iovec_wrapper *iovw, const char *field, ch
}
void iovw_rebase(struct iovec_wrapper *iovw, char *old, char *new) {
- size_t i;
-
- for (i = 0; i < iovw->count; i++)
+ for (size_t i = 0; i < iovw->count; i++)
iovw->iovec[i].iov_base = (char *)iovw->iovec[i].iov_base - old + new;
}
size_t iovw_size(struct iovec_wrapper *iovw) {
- size_t n = 0, i;
+ size_t n = 0;
- for (i = 0; i < iovw->count; i++)
+ for (size_t i = 0; i < iovw->count; i++)
n += iovw->iovec[i].iov_len;
return n;
diff --git a/src/basic/parse-util.c b/src/basic/parse-util.c
index 8ca8f251e1..2a7280fc38 100644
--- a/src/basic/parse-util.c
+++ b/src/basic/parse-util.c
@@ -593,14 +593,13 @@ int safe_atod(const char *s, double *ret_d) {
}
int parse_fractional_part_u(const char **p, size_t digits, unsigned *res) {
- size_t i;
unsigned val = 0;
const char *s;
s = *p;
/* accept any number of digits, strtoull is limited to 19 */
- for (i=0; i < digits; i++,s++) {
+ for (size_t i = 0; i < digits; i++,s++) {
if (*s < '0' || *s > '9') {
if (i == 0)
return -EINVAL;
diff --git a/src/basic/rlimit-util.c b/src/basic/rlimit-util.c
index 880976312c..e01ffdb074 100644
--- a/src/basic/rlimit-util.c
+++ b/src/basic/rlimit-util.c
@@ -50,13 +50,13 @@ int setrlimit_closest(int resource, const struct rlimit *rlim) {
}
int setrlimit_closest_all(const struct rlimit *const *rlim, int *which_failed) {
- int i, r;
+ int r;
assert(rlim);
/* On failure returns the limit's index that failed in *which_failed, but only if non-NULL */
- for (i = 0; i < _RLIMIT_MAX; i++) {
+ for (int i = 0; i < _RLIMIT_MAX; i++) {
if (!rlim[i])
continue;
diff --git a/src/basic/sigbus.c b/src/basic/sigbus.c
index 4c2e9ec33e..8ff060a8d1 100644
--- a/src/basic/sigbus.c
+++ b/src/basic/sigbus.c
@@ -23,12 +23,10 @@ static void* volatile sigbus_queue[SIGBUS_QUEUE_MAX];
static volatile sig_atomic_t n_sigbus_queue = 0;
static void sigbus_push(void *addr) {
- unsigned u;
-
assert(addr);
/* Find a free place, increase the number of entries and leave, if we can */
- for (u = 0; u < SIGBUS_QUEUE_MAX; u++)
+ for (size_t u = 0; u < SIGBUS_QUEUE_MAX; u++)
if (__sync_bool_compare_and_swap(&sigbus_queue[u], NULL, addr)) {
__sync_fetch_and_add(&n_sigbus_queue, 1);
return;
diff --git a/src/basic/signal-util.c b/src/basic/signal-util.c
index 63b833b218..c04f8be05b 100644
--- a/src/basic/signal-util.c
+++ b/src/basic/signal-util.c
@@ -15,9 +15,9 @@ int reset_all_signal_handlers(void) {
.sa_handler = SIG_DFL,
.sa_flags = SA_RESTART,
};
- int sig, r = 0;
+ int r = 0;
- for (sig = 1; sig < _NSIG; sig++) {
+ for (int sig = 1; sig < _NSIG; sig++) {
/* These two cannot be caught... */
if (IN_SET(sig, SIGKILL, SIGSTOP))
diff --git a/src/basic/string-util.c b/src/basic/string-util.c
index be42d5c4f5..3f663e4ac0 100644
--- a/src/basic/string-util.c
+++ b/src/basic/string-util.c
@@ -862,9 +862,8 @@ char *strextend_with_separator_internal(char **x, const char *separator, ...) {
}
char *strrep(const char *s, unsigned n) {
- size_t l;
char *r, *p;
- unsigned i;
+ size_t l;
assert(s);
@@ -873,7 +872,7 @@ char *strrep(const char *s, unsigned n) {
if (!r)
return NULL;
- for (i = 0; i < n; i++)
+ for (unsigned i = 0; i < n; i++)
p = stpcpy(p, s);
*p = 0;
diff --git a/src/basic/time-util.c b/src/basic/time-util.c
index 52c564a68d..3c2b25bd2a 100644
--- a/src/basic/time-util.c
+++ b/src/basic/time-util.c
@@ -493,7 +493,6 @@ char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
{ "us", 1 },
};
- size_t i;
char *p = buf;
bool something = false;
@@ -514,7 +513,7 @@ char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
/* The result of this function can be parsed with parse_sec */
- for (i = 0; i < ELEMENTSOF(table); i++) {
+ for (size_t i = 0; i < ELEMENTSOF(table); i++) {
int k = 0;
size_t n;
bool done = false;
@@ -962,9 +961,8 @@ static const char* extract_multiplier(const char *p, usec_t *multiplier) {
{ "us", 1ULL },
{ "µs", 1ULL },
};
- size_t i;
- for (i = 0; i < ELEMENTSOF(table); i++) {
+ for (size_t i = 0; i < ELEMENTSOF(table); i++) {
char *e;
e = startswith(p, table[i].suffix);
diff --git a/src/basic/tmpfile-util.c b/src/basic/tmpfile-util.c
index 49c343773c..bac5eb6b26 100644
--- a/src/basic/tmpfile-util.c
+++ b/src/basic/tmpfile-util.c
@@ -132,7 +132,6 @@ int tempfn_random(const char *p, const char *extra, char **ret) {
const char *fn;
char *t, *x;
uint64_t u;
- unsigned i;
assert(ret);
@@ -162,7 +161,7 @@ int tempfn_random(const char *p, const char *extra, char **ret) {
x = stpcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), extra), fn);
u = random_u64();
- for (i = 0; i < 16; i++) {
+ for (unsigned i = 0; i < 16; i++) {
*(x++) = hexchar(u & 0xF);
u >>= 4;
}
@@ -176,7 +175,6 @@ int tempfn_random(const char *p, const char *extra, char **ret) {
int tempfn_random_child(const char *p, const char *extra, char **ret) {
char *t, *x;
uint64_t u;
- unsigned i;
int r;
assert(ret);
@@ -205,7 +203,7 @@ int tempfn_random_child(const char *p, const char *extra, char **ret) {
x = stpcpy(stpcpy(stpcpy(t, p), "/.#"), extra);
u = random_u64();
- for (i = 0; i < 16; i++) {
+ for (unsigned i = 0; i < 16; i++) {
*(x++) = hexchar(u & 0xF);
u >>= 4;
}
diff --git a/src/basic/unit-file.c b/src/basic/unit-file.c
index f27352905d..3383d1afad 100644
--- a/src/basic/unit-file.c
+++ b/src/basic/unit-file.c
@@ -580,7 +580,6 @@ static const char * const rlmap_initrd[] = {
const char* runlevel_to_target(const char *word) {
const char * const *rlmap_ptr;
- size_t i;
if (!word)
return NULL;
@@ -593,7 +592,7 @@ const char* runlevel_to_target(const char *word) {
rlmap_ptr = in_initrd() ? rlmap_initrd : rlmap;
- for (i = 0; rlmap_ptr[i]; i += 2)
+ for (size_t i = 0; rlmap_ptr[i]; i += 2)
if (streq(word, rlmap_ptr[i]))
return rlmap_ptr[i+1];
diff --git a/src/basic/utf8.c b/src/basic/utf8.c
index 59663c0350..d61692d07f 100644
--- a/src/basic/utf8.c
+++ b/src/basic/utf8.c
@@ -81,7 +81,7 @@ static size_t utf8_encoded_expected_len(uint8_t c) {
/* decode one unicode char */
int utf8_encoded_to_unichar(const char *str, char32_t *ret_unichar) {
char32_t unichar;
- size_t len, i;
+ size_t len;
assert(str);
@@ -110,7 +110,7 @@ int utf8_encoded_to_unichar(const char *str, char32_t *ret_unichar) {
return -EINVAL;
}
- for (i = 1; i < len; i++) {
+ for (size_t i = 1; i < len; i++) {
if (((char32_t)str[i] & 0xc0) != 0x80)
return -EINVAL;
@@ -302,14 +302,12 @@ char *ascii_is_valid(const char *str) {
}
char *ascii_is_valid_n(const char *str, size_t len) {
- size_t i;
-
/* Very similar to ascii_is_valid(), but checks exactly len
* bytes and rejects any NULs in that range. */
assert(str);
- for (i = 0; i < len; i++)
+ for (size_t i = 0; i < len; i++)
if ((unsigned char) str[i] >= 128 || str[i] == 0)
return NULL;
@@ -436,7 +434,6 @@ size_t utf16_encode_unichar(char16_t *out, char32_t c) {
char16_t *utf8_to_utf16(const char *s, size_t length) {
char16_t *n, *p;
- size_t i;
int r;
assert(s);
@@ -447,7 +444,7 @@ char16_t *utf8_to_utf16(const char *s, size_t length) {
p = n;
- for (i = 0; i < length;) {
+ for (size_t i = 0; i < length;) {
char32_t unichar;
size_t e;
@@ -505,7 +502,7 @@ static int utf8_unichar_to_encoded_len(char32_t unichar) {
/* validate one encoded unicode char and return its length */
int utf8_encoded_valid_unichar(const char *str, size_t length /* bytes */) {
char32_t unichar;
- size_t len, i;
+ size_t len;
int r;
assert(str);
@@ -526,7 +523,7 @@ int utf8_encoded_valid_unichar(const char *str, size_t length /* bytes */) {
return 1;
/* check if expected encoded chars are available */
- for (i = 0; i < len; i++)
+ for (size_t i = 0; i < len; i++)
if ((str[i] & 0x80) != 0x80)
return -EINVAL;
diff --git a/src/basic/virt.c b/src/basic/virt.c
index 3775bea4e6..02e7fbf1f7 100644
--- a/src/basic/virt.c
+++ b/src/basic/virt.c
@@ -159,10 +159,9 @@ static int detect_vm_dmi(void) {
/* https://wiki.freebsd.org/bhyve */
{ "BHYVE", VIRTUALIZATION_BHYVE },
};
- unsigned i;
int r;
- for (i = 0; i < ELEMENTSOF(dmi_vendors); i++) {
+ for (size_t i = 0; i < ELEMENTSOF(dmi_vendors); i++) {
_cleanup_free_ char *s = NULL;
unsigned j;
diff --git a/src/core/dbus-cgroup.c b/src/core/dbus-cgroup.c
index 37c581fb22..a7d9312d97 100644
--- a/src/core/dbus-cgroup.c
+++ b/src/core/dbus-cgroup.c
@@ -32,7 +32,6 @@ static int property_get_cgroup_mask(
sd_bus_error *error) {
CGroupMask *mask = userdata;
- CGroupController ctrl;
int r;
assert(bus);
@@ -42,7 +41,7 @@ static int property_get_cgroup_mask(
if (r < 0)
return r;
- for (ctrl = 0; ctrl < _CGROUP_CONTROLLER_MAX; ctrl++) {
+ for (CGroupController ctrl = 0; ctrl < _CGROUP_CONTROLLER_MAX; ctrl++) {
if ((*mask & CGROUP_CONTROLLER_TO_MASK(ctrl)) == 0)
continue;
diff --git a/src/core/dbus-execute.c b/src/core/dbus-execute.c
index 8434ccb48e..0fbf0b167c 100644
--- a/src/core/dbus-execute.c
+++ b/src/core/dbus-execute.c
@@ -698,7 +698,6 @@ static int property_get_bind_paths(
sd_bus_error *error) {
ExecContext *c = userdata;
- unsigned i;
bool ro;
int r;
@@ -713,7 +712,7 @@ static int property_get_bind_paths(
if (r < 0)
return r;
- for (i = 0; i < c->n_bind_mounts; i++) {
+ for (size_t i = 0; i < c->n_bind_mounts; i++) {
if (ro != c->bind_mounts[i].read_only)
continue;
@@ -741,7 +740,6 @@ static int property_get_temporary_filesystems(
sd_bus_error *error) {
ExecContext *c = userdata;
- unsigned i;
int r;
assert(bus);
@@ -753,7 +751,7 @@ static int property_get_temporary_filesystems(
if (r < 0)
return r;
- for (i = 0; i < c->n_temporary_filesystems; i++) {
+ for (unsigned i = 0; i < c->n_temporary_filesystems; i++) {
TemporaryFileSystem *t = c->temporary_filesystems + i;
r = sd_bus_message_append(
@@ -777,7 +775,6 @@ static int property_get_log_extra_fields(
sd_bus_error *error) {
ExecContext *c = userdata;
- size_t i;
int r;
assert(bus);
@@ -789,7 +786,7 @@ static int property_get_log_extra_fields(
if (r < 0)
return r;
- for (i = 0; i < c->n_log_extra_fields; i++) {
+ for (size_t i = 0; i < c->n_log_extra_fields; i++) {
r = sd_bus_message_append_array(reply, 'y', c->log_extra_fields[i].iov_base, c->log_extra_fields[i].iov_len);
if (r < 0)
return r;
diff --git a/src/core/dbus-job.c b/src/core/dbus-job.c
index 1526b316cc..3334b977bf 100644
--- a/src/core/dbus-job.c
+++ b/src/core/dbus-job.c
@@ -71,7 +71,7 @@ int bus_job_method_get_waiting_jobs(sd_bus_message *message, void *userdata, sd_
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
_cleanup_free_ Job **list = NULL;
Job *j = userdata;
- int r, i, n;
+ int r, n;
if (strstr(sd_bus_message_get_member(message), "After"))
n = job_get_after(j, &list);
@@ -88,7 +88,7 @@ int bus_job_method_get_waiting_jobs(sd_bus_message *message, void *userdata, sd_
if (r < 0)
return r;
- for (i = 0; i < n; i ++) {
+ for (int i = 0; i < n; i ++) {
_cleanup_free_ char *unit_path = NULL, *job_path = NULL;
job_path = job_dbus_path(list[i]);
diff --git a/src/core/dbus-manager.c b/src/core/dbus-manager.c
index eeb74353da..9053d48149 100644
--- a/src/core/dbus-manager.c
+++ b/src/core/dbus-manager.c
@@ -1951,10 +1951,9 @@ static int install_error(
UnitFileChange *changes,
size_t n_changes) {
- size_t i;
int r;
- for (i = 0; i < n_changes; i++)
+ for (size_t i = 0; i < n_changes; i++)
switch(changes[i].type) {
@@ -2021,7 +2020,6 @@ static int reply_unit_file_changes_and_free(
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
bool bad = false, good = false;
- size_t i;
int r;
if (unit_file_changes_have_modification(changes, n_changes)) {
@@ -2044,7 +2042,7 @@ static int reply_unit_file_changes_and_free(
if (r < 0)
goto fail;
- for (i = 0; i < n_changes; i++) {
+ for (size_t i = 0; i < n_changes; i++) {
if (changes[i].type < 0) {
bad = true;
diff --git a/src/core/device.c b/src/core/device.c
index cbfb87a808..db39479c8f 100644
--- a/src/core/device.c
+++ b/src/core/device.c
@@ -193,11 +193,10 @@ static const struct {
static int device_found_to_string_many(DeviceFound flags, char **ret) {
_cleanup_free_ char *s = NULL;
- unsigned i;
assert(ret);
- for (i = 0; i < ELEMENTSOF(device_found_map); i++) {
+ for (size_t i = 0; i < ELEMENTSOF(device_found_map); i++) {
if (!FLAGS_SET(flags, device_found_map[i].flag))
continue;
diff --git a/src/core/execute.c b/src/core/execute.c
index c56a4ef03b..1a679da435 100644
--- a/src/core/execute.c
+++ b/src/core/execute.c
@@ -4911,9 +4911,7 @@ static void exec_command_done(ExecCommand *c) {
}
void exec_command_done_array(ExecCommand *c, size_t n) {
- size_t i;
-
- for (i = 0; i < n; i++)
+ for (size_t i = 0; i < n; i++)
exec_command_done(c+i);
}
diff --git a/src/core/namespace.c b/src/core/namespace.c
index db9a12319d..e8306a8d55 100644
--- a/src/core/namespace.c
+++ b/src/core/namespace.c
@@ -312,11 +312,9 @@ static int append_empty_dir_mounts(MountEntry **p, char **strv) {
}
static int append_bind_mounts(MountEntry **p, const BindMount *binds, size_t n) {
- size_t i;
-
assert(p);
- for (i = 0; i < n; i++) {
+ for (size_t i = 0; i < n; i++) {
const BindMount *b = binds + i;
*((*p)++) = (MountEntry) {
@@ -390,14 +388,12 @@ static int append_tmpfs_mounts(MountEntry **p, const TemporaryFileSystem *tmpfs,
}
static int append_static_mounts(MountEntry **p, const MountEntry *mounts, size_t n, bool ignore_protect) {
- size_t i;
-
assert(p);
assert(mounts);
/* Adds a list of static pre-defined entries */
- for (i = 0; i < n; i++)
+ for (size_t i = 0; i < n; i++)
*((*p)++) = (MountEntry) {
.path_const = mount_entry_path(mounts+i),
.mode = mounts[i].mode,
@@ -464,11 +460,11 @@ static int mount_path_compare(const MountEntry *a, const MountEntry *b) {
}
static int prefix_where_needed(MountEntry *m, size_t n, const char *root_directory) {
- size_t i;
-
/* Prefixes all paths in the bind mount table with the root directory if the entry needs that. */
- for (i = 0; i < n; i++) {
+ assert(m || n == 0);
+
+ for (size_t i = 0; i < n; i++) {
char *s;
if (m[i].has_prefix)
@@ -1913,11 +1909,9 @@ finish:
}
void bind_mount_free_many(BindMount *b, size_t n) {
- size_t i;
-
assert(b || n == 0);
- for (i = 0; i < n; i++) {
+ for (size_t i = 0; i < n; i++) {
free(b[i].source);
free(b[i].destination);
}
@@ -1960,12 +1954,10 @@ int bind_mount_add(BindMount **b, size_t *n, const BindMount *item) {
}
MountImage* mount_image_free_many(MountImage *m, size_t *n) {
- size_t i;
-
assert(n);
assert(m || *n == 0);
- for (i = 0; i < *n; i++) {
+ for (size_t i = 0; i < *n; i++) {
free(m[i].source);
free(m[i].destination);
mount_options_free_all(m[i].mount_options);
@@ -2028,11 +2020,9 @@ int mount_image_add(MountImage **m, size_t *n, const MountImage *item) {
}
void temporary_filesystem_free_many(TemporaryFileSystem *t, size_t n) {
- size_t i;
-
assert(t || n == 0);
- for (i = 0; i < n; i++) {
+ for (size_t i = 0; i < n; i++) {
free(t[i].path);
free(t[i].options);
}
diff --git a/src/core/socket.c b/src/core/socket.c
index 1f799830d1..cb5316af3a 100644
--- a/src/core/socket.c
+++ b/src/core/socket.c
@@ -1319,7 +1319,7 @@ static int usbffs_select_ep(const struct dirent *d) {
static int usbffs_dispatch_eps(SocketPort *p) {
_cleanup_free_ struct dirent **ent = NULL;
- size_t n, k, i;
+ size_t n, k;
int r;
r = scandir(p->path, &ent, usbffs_select_ep, alphasort);
@@ -1336,7 +1336,7 @@ static int usbffs_dispatch_eps(SocketPort *p) {
p->n_auxiliary_fds = n;
k = 0;
- for (i = 0; i < n; ++i) {
+ for (size_t i = 0; i < n; ++i) {
_cleanup_free_ char *ep = NULL;
ep = path_make_absolute(ent[i]->d_name, p->path);
@@ -1363,7 +1363,7 @@ fail:
p->n_auxiliary_fds = 0;
clear:
- for (i = 0; i < n; ++i)
+ for (size_t i = 0; i < n; ++i)
free(ent[i]);
return r;
diff --git a/src/core/swap.c b/src/core/swap.c
index ed06cf4b4b..5746940fa8 100644
--- a/src/core/swap.c
+++ b/src/core/swap.c
@@ -1183,15 +1183,13 @@ static int swap_dispatch_timer(sd_event_source *source, usec_t usec, void *userd
}
static int swap_load_proc_swaps(Manager *m, bool set_flags) {
- unsigned i;
-
assert(m);
rewind(m->proc_swaps);
(void) fscanf(m->proc_swaps, "%*s %*s %*s %*s %*s\n");
- for (i = 1;; i++) {
+ for (unsigned i = 1;; i++) {
_cleanup_free_ char *dev = NULL, *d = NULL;
int prio = 0, k;
diff --git a/src/core/target.c b/src/core/target.c
index a422056803..5755f26615 100644
--- a/src/core/target.c
+++ b/src/core/target.c
@@ -45,7 +45,6 @@ static int target_add_default_dependencies(Target *t) {
};
int r;
- unsigned k;
assert(t);
@@ -55,7 +54,7 @@ static int target_add_default_dependencies(Target *t) {
/* Imply ordering for requirement dependencies on target units. Note that when the user created a contradicting
* ordering manually we won't add anything in here to make sure we don't create a loop. */
- for (k = 0; k < ELEMENTSOF(deps); k++) {
+ for (size_t k = 0; k < ELEMENTSOF(deps); k++) {
Unit *other;
void *v;
diff --git a/src/coredump/coredump.c b/src/coredump/coredump.c
index 0a1cb9103a..d546e1b8f6 100644
--- a/src/coredump/coredump.c
+++ b/src/coredump/coredump.c
@@ -210,14 +210,13 @@ static int fix_xattr(int fd, const Context *context) {
};
int r = 0;
- unsigned i;
assert(fd >= 0);
/* Attach some metadata to coredumps via extended
* attributes. Just because we can. */
- for (i = 0; i < _META_MAX; i++) {
+ for (unsigned i = 0; i < _META_MAX; i++) {
int k;
if (isempty(context->meta[i]) || !xattrs[i])
@@ -808,7 +807,7 @@ log:
}
static int save_context(Context *context, const struct iovec_wrapper *iovw) {
- unsigned n, i, count = 0;
+ unsigned count = 0;
const char *unit;
int r;
@@ -818,10 +817,10 @@ static int save_context(Context *context, const struct iovec_wrapper *iovw) {
/* The context does not allocate any memory on its own */
- for (n = 0; n < iovw->count; n++) {
+ for (size_t n = 0; n < iovw->count; n++) {
struct iovec *iovec = iovw->iovec + n;
- for (i = 0; i < ELEMENTSOF(meta_field_names); i++) {
+ for (size_t i = 0; i < ELEMENTSOF(meta_field_names); i++) {
char *p;
/* Note that these strings are NUL terminated, because we made sure that a
@@ -858,7 +857,7 @@ static int process_socket(int fd) {
Context context = {};
struct iovec_wrapper iovw = {};
struct iovec iovec;
- int i, r;
+ int r;
assert(fd >= 0);
@@ -936,7 +935,7 @@ static int process_socket(int fd) {
goto finish;
/* Make sure we received at least all fields we need. */
- for (i = 0; i < _META_MANDATORY_MAX; i++)
+ for (int i = 0; i < _META_MANDATORY_MAX; i++)
if (!context.meta[i]) {
r = log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"A mandatory argument (%i) has not been sent, aborting.",
@@ -958,7 +957,6 @@ static int send_iovec(const struct iovec_wrapper *iovw, int input_fd) {
.un.sun_path = "/run/systemd/coredump",
};
_cleanup_close_ int fd = -1;
- size_t i;
int r;
assert(iovw);
@@ -971,7 +969,7 @@ static int send_iovec(const struct iovec_wrapper *iovw, int input_fd) {
if (connect(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0)
return log_error_errno(errno, "Failed to connect to coredump service: %m");
- for (i = 0; i < iovw->count; i++) {
+ for (size_t i = 0; i < iovw->count; i++) {
struct msghdr mh = {
.msg_iov = iovw->iovec + i,
.msg_iovlen = 1,
@@ -1022,7 +1020,7 @@ static int gather_pid_metadata_from_argv(
int argc, char **argv) {
_cleanup_free_ char *free_timestamp = NULL;
- int i, r, signo;
+ int r, signo;
char *t;
/* We gather all metadata that were passed via argv[] into an array of iovecs that
@@ -1033,7 +1031,7 @@ static int gather_pid_metadata_from_argv(
"Not enough arguments passed by the kernel (%i, expected %i).",
argc, _META_ARGV_MAX);
- for (i = 0; i < _META_ARGV_MAX; i++) {
+ for (int i = 0; i < _META_ARGV_MAX; i++) {
t = argv[i];
@@ -1224,7 +1222,6 @@ static int process_backtrace(int argc, char *argv[]) {
Context context = {};
struct iovec_wrapper *iovw;
char *message;
- size_t i;
int r;
_cleanup_(journal_importer_cleanup) JournalImporter importer = JOURNAL_IMPORTER_INIT(STDIN_FILENO);
@@ -1274,7 +1271,7 @@ static int process_backtrace(int argc, char *argv[]) {
/* The imported iovecs are not supposed to be freed by us so let's store
* them at the end of the array so we can skip them while freeing the
* rest. */
- for (i = 0; i < importer.iovw.count; i++) {
+ for (size_t i = 0; i < importer.iovw.count; i++) {
struct iovec *iovec = importer.iovw.iovec + i;
iovw_put(iovw, iovec->iov_base, iovec->iov_len);
diff --git a/src/fuzz/fuzz-main.c b/src/fuzz/fuzz-main.c
index 2df2993cbf..c39db5d59d 100644
--- a/src/fuzz/fuzz-main.c
+++ b/src/fuzz/fuzz-main.c
@@ -18,14 +18,14 @@
#define MIN_NUMBER_OF_RUNS 4
int main(int argc, char **argv) {
- int i, r;
- size_t size;
- char *name;
+ int r;
test_setup_logging(LOG_DEBUG);
- for (i = 1; i < argc; i++) {
+ for (int i = 1; i < argc; i++) {
_cleanup_free_ char *buf = NULL;
+ size_t size;
+ char *name;
name = argv[i];
r = read_full_file(name, &buf, &size);
diff --git a/src/libsystemd-network/sd-dhcp-server.c b/src/libsystemd-network/sd-dhcp-server.c
index faea0dd5ca..80292befeb 100644
--- a/src/libsystemd-network/sd-dhcp-server.c
+++ b/src/libsystemd-network/sd-dhcp-server.c
@@ -1057,13 +1057,12 @@ int sd_dhcp_server_start(sd_dhcp_server *server) {
}
int sd_dhcp_server_forcerenew(sd_dhcp_server *server) {
- unsigned i;
int r = 0;
assert_return(server, -EINVAL);
assert(server->bound_leases);
- for (i = 0; i < server->pool_size; i++) {
+ for (uint32_t i = 0; i < server->pool_size; i++) {
DHCPLease *lease = server->bound_leases[i];
if (!lease || lease == &server->invalid_lease)
diff --git a/src/libsystemd/sd-bus/bus-kernel.c b/src/libsystemd/sd-bus/bus-kernel.c
index ba8ffae522..cba1ab2953 100644
--- a/src/libsystemd/sd-bus/bus-kernel.c
+++ b/src/libsystemd/sd-bus/bus-kernel.c
@@ -40,10 +40,8 @@ void close_and_munmap(int fd, void *address, size_t size) {
}
void bus_flush_memfd(sd_bus *b) {
- unsigned i;
-
assert(b);
- for (i = 0; i < b->n_memfd_cache; i++)
+ for (unsigned i = 0; i < b->n_memfd_cache; i++)
close_and_munmap(b->memfd_cache[i].fd, b->memfd_cache[i].address, b->memfd_cache[i].mapped);
}
diff --git a/src/libsystemd/sd-daemon/sd-daemon.c b/src/libsystemd/sd-daemon/sd-daemon.c
index 6f0b975627..b373c173c1 100644
--- a/src/libsystemd/sd-daemon/sd-daemon.c
+++ b/src/libsystemd/sd-daemon/sd-daemon.c
@@ -40,7 +40,7 @@ static void unsetenv_all(bool unset_environment) {
_public_ int sd_listen_fds(int unset_environment) {
const char *e;
- int n, r, fd;
+ int n, r;
pid_t pid;
e = getenv("LISTEN_PID");
@@ -75,7 +75,7 @@ _public_ int sd_listen_fds(int unset_environment) {
goto finish;
}
- for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
+ for (int fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
r = fd_cloexec(fd, true);
if (r < 0)
goto finish;
diff --git a/src/libsystemd/sd-device/device-enumerator.c b/src/libsystemd/sd-device/device-enumerator.c
index 3641348881..a6606cff2f 100644
--- a/src/libsystemd/sd-device/device-enumerator.c
+++ b/src/libsystemd/sd-device/device-enumerator.c
@@ -63,11 +63,9 @@ _public_ int sd_device_enumerator_new(sd_device_enumerator **ret) {
}
static sd_device_enumerator *device_enumerator_free(sd_device_enumerator *enumerator) {
- size_t i;
-
assert(enumerator);
- for (i = 0; i < enumerator->n_devices; i++)
+ for (size_t i = 0; i < enumerator->n_devices; i++)
sd_device_unref(enumerator->devices[i]);
free(enumerator->devices);
@@ -784,7 +782,6 @@ static void device_enumerator_dedup_devices(sd_device_enumerator *enumerator) {
int device_enumerator_scan_devices(sd_device_enumerator *enumerator) {
int r = 0, k;
- size_t i;
assert(enumerator);
@@ -792,7 +789,7 @@ int device_enumerator_scan_devices(sd_device_enumerator *enumerator) {
enumerator->type == DEVICE_ENUMERATION_TYPE_DEVICES)
return 0;
- for (i = 0; i < enumerator->n_devices; i++)
+ for (size_t i = 0; i < enumerator->n_devices; i++)
sd_device_unref(enumerator->devices[i]);
enumerator->n_devices = 0;
@@ -851,7 +848,6 @@ _public_ sd_device *sd_device_enumerator_get_device_next(sd_device_enumerator *e
int device_enumerator_scan_subsystems(sd_device_enumerator *enumerator) {
const char *subsysdir;
int r = 0, k;
- size_t i;
assert(enumerator);
@@ -859,7 +855,7 @@ int device_enumerator_scan_subsystems(sd_device_enumerator *enumerator) {
enumerator->type == DEVICE_ENUMERATION_TYPE_SUBSYSTEMS)
return 0;
- for (i = 0; i < enumerator->n_devices; i++)
+ for (size_t i = 0; i < enumerator->n_devices; i++)
sd_device_unref(enumerator->devices[i]);
enumerator->n_devices = 0;