]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
treewide: tighten variable scope in loops (#18372)
authorSusant Sahani <ssahani@vmware.com>
Wed, 27 Jan 2021 07:19:39 +0000 (08:19 +0100)
committerGitHub <noreply@github.com>
Wed, 27 Jan 2021 07:19:39 +0000 (08:19 +0100)
Also use _cleanup_free_ in one more place.

30 files changed:
src/basic/cgroup-util.c
src/basic/fd-util.c
src/basic/format-util.c
src/basic/io-util.c
src/basic/parse-util.c
src/basic/rlimit-util.c
src/basic/sigbus.c
src/basic/signal-util.c
src/basic/string-util.c
src/basic/time-util.c
src/basic/tmpfile-util.c
src/basic/unit-file.c
src/basic/utf8.c
src/basic/virt.c
src/core/dbus-cgroup.c
src/core/dbus-execute.c
src/core/dbus-job.c
src/core/dbus-manager.c
src/core/device.c
src/core/execute.c
src/core/namespace.c
src/core/socket.c
src/core/swap.c
src/core/target.c
src/coredump/coredump.c
src/fuzz/fuzz-main.c
src/libsystemd-network/sd-dhcp-server.c
src/libsystemd/sd-bus/bus-kernel.c
src/libsystemd/sd-daemon/sd-daemon.c
src/libsystemd/sd-device/device-enumerator.c

index 375fd1c3be39a7ce9fb965d2c2737b493c42a772..b567822b7ef4ff0341d96885a1c90b2dd691f3fc 100644 (file)
@@ -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;
 }
 
index a03ba83e19b1f19842c1a07cfbd869decf7a9332..d63f012ad5199f00a4809b8f48cbd9de37b5084a 100644 (file)
@@ -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;
 
index bf23037792f12ef8b7856d37f52f8c67f7319805..04ed4be9418b770b126eb8149beff5a22a2f8c47 100644 (file)
@@ -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,
index 4d7405296b8766320eef21f716739db81d5c8ce7..8ea350dcc396155f3960b8c04a1d066f00770b29 100644 (file)
@@ -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;
index 8ca8f251e142b6d9cf7b6ea5c69a4835115cf72b..2a7280fc3821a1afbf3d993c0711886ff4835e56 100644 (file)
@@ -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;
index 880976312cb63056c77e218d525e062f96b6b92b..e01ffdb0744fc01338fd1d980e8ee475679f1075 100644 (file)
@@ -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;
 
index 4c2e9ec33ee68ef021d9073c84af68d54383e0ba..8ff060a8d1ea4145472eb5297f751be1648533f1 100644 (file)
@@ -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;
index 63b833b2181043b5a9c110c6cbbbac726bcb62b5..c04f8be05bbfa8bd8a72fc4e7be9e556a77ec032 100644 (file)
@@ -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))
index be42d5c4f5ddff82a78e13a729b3599770dd8557..3f663e4ac0ed53cf7f602789949931ba523feb59 100644 (file)
@@ -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;
index 52c564a68d3353242c2f5d90489d875de83e92eb..3c2b25bd2a72039f14516457618b20c6c8eb7ea7 100644 (file)
@@ -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);
index 49c343773cfc3e530f7dcf9a1a9164c26b78bbe0..bac5eb6b26a0232acd9800118be76ee628ecf11f 100644 (file)
@@ -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;
         }
index f27352905d46c56f21ee45f24b67a27271c75c51..3383d1afadfc0d1fa52a51a623de8b20aae1a9f6 100644 (file)
@@ -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];
 
index 59663c0350cccccbc3ec2ec576965cf64572d633..d61692d07f191790045b1edf30209a6fc5e370ae 100644 (file)
@@ -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;
 
index 3775bea4e66acc5b1d415f91eb8f143d5ce7e137..02e7fbf1f72fe7456645d5259610f86e138e5dc7 100644 (file)
@@ -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;
 
index 37c581fb22a2d06febc3984f7fbeaeab5dd9b060..a7d9312d9781ed4ed245de19a9ac2313d38ab33e 100644 (file)
@@ -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;
 
index 8434ccb48e844244dea8829769683468946c1825..0fbf0b167ce677ba4d6fb698fecb72db795bac46 100644 (file)
@@ -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;
index 1526b316cc4e1f3da0b73a0cb6cb88f0f74c41d4..3334b977bfea7fd0cebd52493f3d08cb462cab5a 100644 (file)
@@ -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]);
index eeb74353da5a9057211f6a3251f83858883a7d78..9053d48149598dbffaf9f97cd6c4704a51f1af7f 100644 (file)
@@ -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;
index cbfb87a80863fa7716423c0c556df9d1b8de65e8..db39479c8fc4c9a1e38a65b503bcea138cbee1f6 100644 (file)
@@ -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;
 
index c56a4ef03b59486fb444afd41fc93ee94a30ba8f..1a679da435023d64894464698ac55938310d1dd2 100644 (file)
@@ -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);
 }
 
index db9a12319d771635959291053a0f310b83510b00..e8306a8d550889058f9aeb2d36b8e97b0bea5d64 100644 (file)
@@ -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);
         }
index 1f799830d1650421abb8d72f46623563de46223f..cb5316af3a2b7da5a40b8fd1e72d106679d00f0c 100644 (file)
@@ -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;
index ed06cf4b4b8cab68cb7a97f7a3822292276be67c..5746940fa8771a15f33dcc09eeb41f263bbf9c9d 100644 (file)
@@ -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;
 
index a422056803da68786c83a8534f67ad1ae4e35bbf..5755f266155cc886ba61ed563df08de653b5e7eb 100644 (file)
@@ -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;
 
index 0a1cb9103a99f556697ae27dd18f839e7fee070b..d546e1b8f666a02392932fb14406306d033cb55a 100644 (file)
@@ -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);
index 2df2993cbfbee03e6d8ad03bea523be41799a0de..c39db5d59d0083d4497121e7d8de440f9b5844c6 100644 (file)
 #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);
index faea0dd5cab34c5c611b85bb53356b3db9a027bb..80292befeb728e0e2f40a6388fdd3a5100ad63d4 100644 (file)
@@ -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)
index ba8ffae52297011465d0e7eed1f5f2d60cc6fb5f..cba1ab2953382a81ff6ffc8e9cb7b1862e61de31 100644 (file)
@@ -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);
 }
index 6f0b975627e7632938b2d099cc1d23d75726ccf9..b373c173c158498a2dcd2e9e04939219d4da2946 100644 (file)
@@ -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;
index 3641348881c02fe4fcf58569e1542c8f0af42dc1..a6606cff2f10fc334843e79c9a264b4f5e856adc 100644 (file)
@@ -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;