]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: replace for loop with FOREACH_ELEMENT or FOREACH_ARRAY macros (#34893)
authorIntegral <integral@member.fsf.org>
Fri, 25 Oct 2024 22:10:22 +0000 (06:10 +0800)
committerGitHub <noreply@github.com>
Fri, 25 Oct 2024 22:10:22 +0000 (07:10 +0900)
46 files changed:
src/basic/architecture.c
src/basic/proc-cmdline.c
src/basic/time-util.c
src/basic/virt.c
src/boot/efi/random-seed.c
src/boot/efi/secure-boot.c
src/core/automount.c
src/core/device.c
src/core/load-fragment.c
src/core/unit-serialize.c
src/initctl/initctl.c
src/journal-remote/microhttpd-util.c
src/libsystemd-network/test-dhcp-option.c
src/libsystemd/sd-event/sd-event.c
src/login/test-session-properties.c
src/nspawn/nspawn-mount.c
src/nspawn/nspawn-oci.c
src/nspawn/nspawn-seccomp.c
src/nspawn/nspawn.c
src/resolve/resolved-dns-stub.c
src/resolve/resolved-etc-hosts.c
src/shared/dns-domain.c
src/shared/ethtool-util.c
src/shared/mount-util.c
src/shared/netif-naming-scheme.c
src/shared/seccomp-util.c
src/shared/user-record.c
src/test/test-bootspec.c
src/test/test-chase-manual.c
src/test/test-fileio.c
src/test/test-hashmap-plain.c
src/test/test-job-type.c
src/test/test-list.c
src/test/test-load-fragment.c
src/test/test-ordered-set.c
src/test/test-prioq.c
src/test/test-set.c
src/test/test-string-util.c
src/test/test-terminal-util.c
src/test/test-tpm2.c
src/timesync/timesyncd-manager.c
src/udev/net/link-config.c
src/udev/test-udev-rule-runner.c
src/udev/udev-builtin-input_id.c
src/udev/udev-format.c
src/udev/udevadm.c

index 488367c1884dae11b22a65e9ff9ae6a0ee4b723b..c7d5b0d8bd329fdaa0565ffe633829abd3bfe71f 100644 (file)
@@ -130,9 +130,9 @@ Architecture uname_architecture(void) {
 
         assert_se(uname(&u) >= 0);
 
-        for (size_t i = 0; i < ELEMENTSOF(arch_map); i++)
-                if (streq(arch_map[i].machine, u.machine))
-                        return cached = arch_map[i].arch;
+        FOREACH_ELEMENT(entry, arch_map)
+                if (streq(entry->machine, u.machine))
+                        return cached = entry->arch;
 
         assert_not_reached();
         return _ARCHITECTURE_INVALID;
index 5dad0e50d48f261507aba4670a38f37a2a7e72ae..4ffb90b69624e6204cef65a2102178dd4b62e904 100644 (file)
@@ -71,13 +71,13 @@ int proc_cmdline_filter_pid1_args(char **argv, char ***ret) {
                         }
 
                         /* long option, e.g. --foo */
-                        for (size_t i = 0; i < ELEMENTSOF(options); i++) {
-                                const char *q = startswith(a + 2, options[i].name);
+                        FOREACH_ELEMENT(option, options) {
+                                const char *q = startswith(a + 2, option->name);
                                 if (!q || !IN_SET(q[0], '=', '\0'))
                                         continue;
 
                                 /* Found matching option, updating the state if necessary. */
-                                if (q[0] == '\0' && options[i].has_arg == required_argument)
+                                if (q[0] == '\0' && option->has_arg == required_argument)
                                         state = required_argument;
 
                                 break;
index 1e042b8b600ced07fa0e1181680e7904bf9fbecf..29afb08ebc60e05ab2af1c512596b66a92b370b0 100644 (file)
@@ -556,7 +556,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 (size_t i = 0; i < ELEMENTSOF(table); i++) {
+        FOREACH_ELEMENT(i, table) {
                 int k = 0;
                 size_t n;
                 bool done = false;
@@ -568,20 +568,20 @@ char* format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
                 if (t < accuracy && something)
                         break;
 
-                if (t < table[i].usec)
+                if (t < i->usec)
                         continue;
 
                 if (l <= 1)
                         break;
 
-                a = t / table[i].usec;
-                b = t % table[i].usec;
+                a = t / i->usec;
+                b = t % i->usec;
 
                 /* Let's see if we should shows this in dot notation */
                 if (t < USEC_PER_MINUTE && b > 0) {
                         signed char j = 0;
 
-                        for (usec_t cc = table[i].usec; cc > 1; cc /= 10)
+                        for (usec_t cc = i->usec; cc > 1; cc /= 10)
                                 j++;
 
                         for (usec_t cc = accuracy; cc > 1; cc /= 10) {
@@ -596,7 +596,7 @@ char* format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
                                              a,
                                              j,
                                              b,
-                                             table[i].suffix);
+                                             i->suffix);
 
                                 t = 0;
                                 done = true;
@@ -609,7 +609,7 @@ char* format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
                                      "%s"USEC_FMT"%s",
                                      p > buf ? " " : "",
                                      a,
-                                     table[i].suffix);
+                                     i->suffix);
 
                         t = b;
                 }
@@ -795,12 +795,12 @@ static int parse_timestamp_impl(
                 goto from_tm;
         }
 
-        for (size_t i = 0; i < ELEMENTSOF(day_nr); i++) {
-                k = startswith_no_case(t, day_nr[i].name);
+        FOREACH_ELEMENT(day, day_nr) {
+                k = startswith_no_case(t, day->name);
                 if (!k || *k != ' ')
                         continue;
 
-                weekday = day_nr[i].nr;
+                weekday = day->nr;
                 t = k + 1;
                 break;
         }
@@ -1122,12 +1122,12 @@ static const char* extract_multiplier(const char *p, usec_t *ret) {
         assert(p);
         assert(ret);
 
-        for (size_t i = 0; i < ELEMENTSOF(table); i++) {
+        FOREACH_ELEMENT(i, table) {
                 char *e;
 
-                e = startswith(p, table[i].suffix);
+                e = startswith(p, i->suffix);
                 if (e) {
-                        *ret = table[i].usec;
+                        *ret = i->usec;
                         return e;
                 }
         }
@@ -1300,17 +1300,16 @@ static const char* extract_nsec_multiplier(const char *p, nsec_t *ret) {
                 { "ns",      1ULL            },
                 { "",        1ULL            }, /* default is nsec */
         };
-        size_t i;
 
         assert(p);
         assert(ret);
 
-        for (i = 0; i < ELEMENTSOF(table); i++) {
+        FOREACH_ELEMENT(i, table) {
                 char *e;
 
-                e = startswith(p, table[i].suffix);
+                e = startswith(p, i->suffix);
                 if (e) {
-                        *ret = table[i].nsec;
+                        *ret = i->nsec;
                         return e;
                 }
         }
index 2cb3a0839337a598dfaa6f76cd3fd45ba4462f96..fd0c353791643a805691ae3e3be16175774f69c4 100644 (file)
@@ -83,10 +83,10 @@ static Virtualization detect_vm_cpuid(void) {
 
                 log_debug("Virtualization found, CPUID=%s", sig.text);
 
-                for (size_t i = 0; i < ELEMENTSOF(vm_table); i++)
+                FOREACH_ELEMENT(vm, vm_table)
                         if (memcmp_nn(sig.text, sizeof(sig.text),
-                                      vm_table[i].sig, sizeof(vm_table[i].sig)) == 0)
-                                return vm_table[i].id;
+                                      vm->sig, sizeof(vm->sig)) == 0)
+                                return vm->id;
 
                 log_debug("Unknown virtualization with CPUID=%s. Add to vm_table[]?", sig.text);
                 return VIRTUALIZATION_VM_OTHER;
@@ -202,10 +202,10 @@ static Virtualization detect_vm_dmi_vendor(void) {
                         return r;
                 }
 
-                for (size_t i = 0; i < ELEMENTSOF(dmi_vendor_table); i++)
-                        if (startswith(s, dmi_vendor_table[i].vendor)) {
+                FOREACH_ELEMENT(dmi_vendor, dmi_vendor_table)
+                        if (startswith(s, dmi_vendor->vendor)) {
                                 log_debug("Virtualization %s found in DMI (%s)", s, *vendor);
-                                return dmi_vendor_table[i].id;
+                                return dmi_vendor->id;
                         }
         }
         log_debug("No virtualization found in DMI vendor table.");
@@ -658,14 +658,14 @@ static Virtualization detect_container_files(void) {
                 { "/.dockerenv",        VIRTUALIZATION_DOCKER },
         };
 
-        for (size_t i = 0; i < ELEMENTSOF(container_file_table); i++) {
-                if (access(container_file_table[i].file_path, F_OK) >= 0)
-                        return container_file_table[i].id;
+        FOREACH_ELEMENT(file, container_file_table) {
+                if (access(file->file_path, F_OK) >= 0)
+                        return file->id;
 
                 if (errno != ENOENT)
                         log_debug_errno(errno,
                                         "Checking if %s exists failed, ignoring: %m",
-                                        container_file_table[i].file_path);
+                                        file->file_path);
         }
 
         return VIRTUALIZATION_NONE;
index 0ce16615c16926a255aedddae5320131e00926b8..2edb2afb58cd7841d3d3e0884c4ce0f77d4b52b9 100644 (file)
@@ -108,8 +108,8 @@ static void validate_sha256(void) {
                     0xaf, 0xac, 0x45, 0x03, 0x7a, 0xfe, 0xe9, 0xd1 }},
         };
 
-        for (size_t i = 0; i < ELEMENTSOF(array); i++)
-                assert(memcmp(SHA256_DIRECT(array[i].string, strlen8(array[i].string)), array[i].hash, HASH_VALUE_SIZE) == 0);
+        FOREACH_ELEMENT(i, array)
+                assert(memcmp(SHA256_DIRECT(i->string, strlen8(i->string)), i->hash, HASH_VALUE_SIZE) == 0);
 #endif
 }
 
index 566688adf838cc074bb6355b190730dd81629a3a..dc61457512466d4946da3b54f742f4e34cb171fe 100644 (file)
@@ -136,14 +136,14 @@ EFI_STATUS secure_boot_enroll_at(EFI_FILE *root_dir, const char16_t *path, bool
         };
 
         /* Make sure all keys files exist before we start enrolling them by loading them from the disk first. */
-        for (size_t i = 0; i < ELEMENTSOF(sb_vars); i++) {
-                err = file_read(dir, sb_vars[i].filename, 0, 0, &sb_vars[i].buffer, &sb_vars[i].size);
-                if (err != EFI_SUCCESS && sb_vars[i].required) {
-                        log_error_status(err, "Failed reading file %ls\\%ls: %m", path, sb_vars[i].filename);
+        FOREACH_ELEMENT(sb_var, sb_vars) {
+                err = file_read(dir, sb_var->filename, 0, 0, &sb_var->buffer, &sb_var->size);
+                if (err != EFI_SUCCESS && sb_var->required) {
+                        log_error_status(err, "Failed reading file %ls\\%ls: %m", path, sb_var->filename);
                         goto out_deallocate;
                 }
-                if (streq16(sb_vars[i].name, u"PK") && sb_vars[i].size > 20) {
-                        assert(sb_vars[i].buffer);
+                if (streq16(sb_var->name, u"PK") && sb_var->size > 20) {
+                        assert(sb_var->buffer);
                         /*
                          * The buffer should be EFI_TIME (16 bytes), followed by
                          * EFI_VARIABLE_AUTHENTICATION_2 header.  First header field is the size.  If the
@@ -151,7 +151,7 @@ EFI_STATUS secure_boot_enroll_at(EFI_FILE *root_dir, const char16_t *path, bool
                          * bytes), leaving no space for an actual signature, we can conclude that no
                          * signature is present.
                          */
-                        uint32_t *sigsize = (uint32_t*)(sb_vars[i].buffer + 16);
+                        uint32_t *sigsize = (uint32_t*)(sb_var->buffer + 16);
                         if (*sigsize <= 24) {
                                 printf("PK is not signed (need custom mode).\n");
                                 need_custom_mode = true;
@@ -168,19 +168,19 @@ EFI_STATUS secure_boot_enroll_at(EFI_FILE *root_dir, const char16_t *path, bool
                 printf("Custom mode enabled.\n");
         }
 
-        for (size_t i = 0; i < ELEMENTSOF(sb_vars); i++) {
+        FOREACH_ELEMENT(sb_var, sb_vars) {
                 uint32_t sb_vars_opts =
                         EFI_VARIABLE_NON_VOLATILE |
                         EFI_VARIABLE_BOOTSERVICE_ACCESS |
                         EFI_VARIABLE_RUNTIME_ACCESS |
                         EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
 
-                if (!sb_vars[i].buffer)
+                if (!sb_var->buffer)
                         continue;
 
-                err = efivar_set_raw(&sb_vars[i].vendor, sb_vars[i].name, sb_vars[i].buffer, sb_vars[i].size, sb_vars_opts);
+                err = efivar_set_raw(&sb_var->vendor, sb_var->name, sb_var->buffer, sb_var->size, sb_vars_opts);
                 if (err != EFI_SUCCESS) {
-                        log_error_status(err, "Failed to write %ls secure boot variable: %m", sb_vars[i].name);
+                        log_error_status(err, "Failed to write %ls secure boot variable: %m", sb_var->name);
                         goto out_deallocate;
                 }
         }
@@ -193,8 +193,8 @@ EFI_STATUS secure_boot_enroll_at(EFI_FILE *root_dir, const char16_t *path, bool
         assert_not_reached();
 
 out_deallocate:
-        for (size_t i = 0; i < ELEMENTSOF(sb_vars); i++)
-                free(sb_vars[i].buffer);
+        FOREACH_ELEMENT(sb_var, sb_vars)
+                free(sb_var->buffer);
 
         return err;
 }
index 0bdd970efc96de44c28bc013d4b4fdca84755cf2..9d0f7a1f6fc4b99368e7e8d151d26bbd5f46a29d 100644 (file)
@@ -176,13 +176,13 @@ static int automount_verify(Automount *a) {
         if (!unit_has_name(UNIT(a), e))
                 return log_unit_error_errno(UNIT(a), SYNTHETIC_ERRNO(ENOEXEC), "Where= setting doesn't match unit name. Refusing.");
 
-        for (size_t i = 0; i < ELEMENTSOF(reserved_options); i++)
-                if (fstab_test_option(a->extra_options, reserved_options[i]))
+        FOREACH_ELEMENT(reserved_option, reserved_options)
+                if (fstab_test_option(a->extra_options, *reserved_option))
                         return log_unit_error_errno(
                                 UNIT(a),
                                 SYNTHETIC_ERRNO(ENOEXEC),
                                 "ExtraOptions= setting may not contain reserved option %s.",
-                                reserved_options[i]);
+                                *reserved_option);
 
         return 0;
 }
index 4b62477d2cffa2ab1500a59c586392924610114c..03a730f6240c1bc89d7ab9e940bd6f2f79288ce0 100644 (file)
@@ -372,7 +372,6 @@ static int device_found_from_string_many(const char *name, DeviceFound *ret) {
         for (;;) {
                 _cleanup_free_ char *word = NULL;
                 DeviceFound f = 0;
-                unsigned i;
 
                 r = extract_first_word(&name, &word, ",", 0);
                 if (r < 0)
@@ -380,9 +379,9 @@ static int device_found_from_string_many(const char *name, DeviceFound *ret) {
                 if (r == 0)
                         break;
 
-                for (i = 0; i < ELEMENTSOF(device_found_map); i++)
-                        if (streq(word, device_found_map[i].name)) {
-                                f = device_found_map[i].flag;
+                FOREACH_ELEMENT(i, device_found_map)
+                        if (streq(word, i->name)) {
+                                f = i->flag;
                                 break;
                         }
 
index 4b702038e6c87b9e9db62cd743b4ad8fdc7bdb22..fa8909e1d114a22262695b48a2e56ee2120c2a56 100644 (file)
@@ -6383,9 +6383,9 @@ void unit_dump_config_items(FILE *f) {
                     p->ltype == DISABLED_LEGACY)
                         continue;
 
-                for (size_t j = 0; j < ELEMENTSOF(table); j++)
-                        if (p->parse == table[j].callback) {
-                                rvalue = table[j].rvalue;
+                FOREACH_ELEMENT(j, table)
+                        if (p->parse == j->callback) {
+                                rvalue = j->rvalue;
                                 break;
                         }
 
index ec1d3df32f4ce480b425669975437b6c13cb0bf9..f196242eaf3efc75ad585f760b8cb76fe77d3518 100644 (file)
@@ -419,12 +419,11 @@ static void print_unit_dependency_mask(FILE *f, const char *kind, UnitDependency
         assert(kind);
         assert(space);
 
-        for (size_t i = 0; i < ELEMENTSOF(table); i++) {
-
+        FOREACH_ELEMENT(i, table) {
                 if (mask == 0)
                         break;
 
-                if (FLAGS_SET(mask, table[i].mask)) {
+                if (FLAGS_SET(mask, i->mask)) {
                         if (*space)
                                 fputc(' ', f);
                         else
@@ -432,9 +431,9 @@ static void print_unit_dependency_mask(FILE *f, const char *kind, UnitDependency
 
                         fputs(kind, f);
                         fputs("-", f);
-                        fputs(table[i].name, f);
+                        fputs(i->name, f);
 
-                        mask &= ~table[i].mask;
+                        mask &= ~i->mask;
                 }
         }
 
index d1b7c305624fb2e857d6e0bb029f5af6e373332f..8ed5f1e26416ce682bc7e9435c80864e2e5413f6 100644 (file)
@@ -74,12 +74,12 @@ static const char *translate_runlevel(int runlevel, bool *isolate) {
 
         assert(isolate);
 
-        for (size_t i = 0; i < ELEMENTSOF(table); i++)
-                if (table[i].runlevel == runlevel) {
-                        *isolate = table[i].isolate;
+        FOREACH_ELEMENT(i, table)
+                if (i->runlevel == runlevel) {
+                        *isolate = i->isolate;
                         if (runlevel == '6' && kexec_loaded())
                                 return SPECIAL_KEXEC_TARGET;
-                        return table[i].special;
+                        return i->special;
                 }
 
         return NULL;
index c1e35b7ed34ba19bd45f496319868bb6732358c0..0d5997a03fb6a4c5ae1c321915fbe9ad12c98dcf 100644 (file)
@@ -118,17 +118,15 @@ static void log_reset_gnutls_level(void) {
 }
 
 static int log_enable_gnutls_category(const char *cat) {
-        unsigned i;
-
         if (streq(cat, "all")) {
-                for (i = 0; i < ELEMENTSOF(gnutls_log_map); i++)
-                        gnutls_log_map[i].enabled = true;
+                FOREACH_ELEMENT(entry, gnutls_log_map)
+                        entry->enabled = true;
                 log_reset_gnutls_level();
                 return 0;
         } else
-                for (i = 0; i < ELEMENTSOF(gnutls_log_map); i++)
-                        if (strv_contains((char**)gnutls_log_map[i].names, cat)) {
-                                gnutls_log_map[i].enabled = true;
+                FOREACH_ELEMENT(entry, gnutls_log_map)
+                        if (strv_contains((char**)entry->names, cat)) {
+                                entry->enabled = true;
                                 log_reset_gnutls_level();
                                 return 0;
                         }
index bcd46e4720f01da976ece80a16f46915269b0343..162e826f98137c9e50397c47f321996fedf4e147 100644 (file)
@@ -370,13 +370,12 @@ int main(int argc, char *argv[]) {
 
         test_options(NULL);
 
-        for (unsigned i = 0; i < ELEMENTSOF(option_tests); i++)
-                test_options(&option_tests[i]);
+        FOREACH_ELEMENT(desc, option_tests)
+                test_options(desc);
 
         test_option_set();
 
-        for (unsigned i = 0; i < ELEMENTSOF(option_tests); i++) {
-                struct option_desc *desc = &option_tests[i];
+        FOREACH_ELEMENT(desc, option_tests) {
                 if (!desc->success || desc->snamelen > 0 || desc->filelen > 0)
                         continue;
                 test_option_removal(desc);
index 26b28d7bcf00451e3dfc3529ca487958c57386b8..a2fd0de9e9c9b40769c310c9951bea38a40dd230 100644 (file)
@@ -4858,13 +4858,13 @@ _public_ int sd_event_dispatch(sd_event *e) {
 
 static void event_log_delays(sd_event *e) {
         char b[ELEMENTSOF(e->delays) * DECIMAL_STR_MAX(unsigned) + 1], *p;
-        size_t l, i;
+        size_t l;
 
         p = b;
         l = sizeof(b);
-        for (i = 0; i < ELEMENTSOF(e->delays); i++) {
-                l = strpcpyf(&p, l, "%u ", e->delays[i]);
-                e->delays[i] = 0;
+        FOREACH_ELEMENT(delay, e->delays) {
+                l = strpcpyf(&p, l, "%u ", *delay);
+                *delay = 0;
         }
         log_debug("Event loop iterations: %s", b);
 }
index b5b5f60eaaac29b82fe1c3a9b323f365f27544bf..00a0caaf5bbb7dd30040dd66f1c71524b56f047b 100644 (file)
@@ -42,11 +42,11 @@ TEST(set_type) {
         assert_se(bus_call_method(bus, &session, "TakeControl", NULL, NULL, "b", true) >= 0);
 
         /* All defined session types can be set */
-        for (size_t i = 0; i < ELEMENTSOF(types); i++) {
+        FOREACH_ELEMENT(i, types) {
                 type = mfree(type);
-                assert_se(bus_call_method(bus, &session, "SetType", NULL, NULL, "s", types[i]) >= 0);
+                assert_se(bus_call_method(bus, &session, "SetType", NULL, NULL, "s", *i) >= 0);
                 assert_se(bus_get_property_string(bus, &session, "Type", NULL, &type) >= 0);
-                assert_se(streq(type, types[i]));
+                assert_se(streq(type, *i));
         }
 
         /* An unknown type is rejected */
index 3e579480fd7565ac344b21f54c7162306c3f5601..ddbdba6fb65652f16c206b4d3180f40c935f0326 100644 (file)
@@ -635,33 +635,33 @@ int mount_all(const char *dest,
         bool privileged = FLAGS_SET(mount_settings, MOUNT_PRIVILEGED);
         int r;
 
-        for (size_t k = 0; k < ELEMENTSOF(mount_table); k++) {
+        FOREACH_ELEMENT(m, mount_table) {
                 _cleanup_free_ char *where = NULL, *options = NULL, *prefixed = NULL;
-                bool fatal = FLAGS_SET(mount_table[k].mount_settings, MOUNT_FATAL);
+                bool fatal = FLAGS_SET(m->mount_settings, MOUNT_FATAL);
                 const char *o;
 
                 /* If we are not privileged but the entry is marked as privileged and to be mounted outside the user namespace, then skip it */
-                if (!privileged && FLAGS_SET(mount_table[k].mount_settings, MOUNT_PRIVILEGED) && !FLAGS_SET(mount_table[k].mount_settings, MOUNT_IN_USERNS))
+                if (!privileged && FLAGS_SET(m->mount_settings, MOUNT_PRIVILEGED) && !FLAGS_SET(m->mount_settings, MOUNT_IN_USERNS))
                         continue;
 
-                if (in_userns != FLAGS_SET(mount_table[k].mount_settings, MOUNT_IN_USERNS))
+                if (in_userns != FLAGS_SET(m->mount_settings, MOUNT_IN_USERNS))
                         continue;
 
-                if (!netns && FLAGS_SET(mount_table[k].mount_settings, MOUNT_APPLY_APIVFS_NETNS))
+                if (!netns && FLAGS_SET(m->mount_settings, MOUNT_APPLY_APIVFS_NETNS))
                         continue;
 
-                if (!ro && FLAGS_SET(mount_table[k].mount_settings, MOUNT_APPLY_APIVFS_RO))
+                if (!ro && FLAGS_SET(m->mount_settings, MOUNT_APPLY_APIVFS_RO))
                         continue;
 
-                if (!tmpfs_tmp && FLAGS_SET(mount_table[k].mount_settings, MOUNT_APPLY_TMPFS_TMP))
+                if (!tmpfs_tmp && FLAGS_SET(m->mount_settings, MOUNT_APPLY_TMPFS_TMP))
                         continue;
 
-                r = chase(mount_table[k].where, dest, CHASE_NONEXISTENT|CHASE_PREFIX_ROOT, &where, NULL);
+                r = chase(m->where, dest, CHASE_NONEXISTENT|CHASE_PREFIX_ROOT, &where, NULL);
                 if (r < 0)
-                        return log_error_errno(r, "Failed to resolve %s%s: %m", strempty(dest), mount_table[k].where);
+                        return log_error_errno(r, "Failed to resolve %s%s: %m", strempty(dest), m->where);
 
                 /* Skip this entry if it is not a remount. */
-                if (mount_table[k].what) {
+                if (m->what) {
                         r = path_is_mount_point(where);
                         if (r < 0 && r != -ENOENT)
                                 return log_error_errno(r, "Failed to detect whether %s is a mount point: %m", where);
@@ -669,10 +669,10 @@ int mount_all(const char *dest,
                                 continue;
                 }
 
-                if ((mount_table[k].mount_settings & (MOUNT_MKDIR|MOUNT_TOUCH)) != 0) {
+                if ((m->mount_settings & (MOUNT_MKDIR|MOUNT_TOUCH)) != 0) {
                         uid_t u = (use_userns && !in_userns) ? uid_shift : UID_INVALID;
 
-                        if (FLAGS_SET(mount_table[k].mount_settings, MOUNT_TOUCH))
+                        if (FLAGS_SET(m->mount_settings, MOUNT_TOUCH))
                                 r = mkdir_parents_safe(dest, where, 0755, u, u, 0);
                         else
                                 r = mkdir_p_safe(dest, where, 0755, u, u, 0);
@@ -689,7 +689,7 @@ int mount_all(const char *dest,
                         }
                 }
 
-                if (FLAGS_SET(mount_table[k].mount_settings, MOUNT_TOUCH)) {
+                if (FLAGS_SET(m->mount_settings, MOUNT_TOUCH)) {
                         r = touch(where);
                         if (r < 0 && r != -EEXIST) {
                                 if (fatal && r != -EROFS)
@@ -701,8 +701,8 @@ int mount_all(const char *dest,
                         }
                 }
 
-                o = mount_table[k].options;
-                if (streq_ptr(mount_table[k].type, "tmpfs")) {
+                o = m->options;
+                if (streq_ptr(m->type, "tmpfs")) {
                         r = tmpfs_patch_options(o, in_userns ? 0 : uid_shift, selinux_apifs_context, &options);
                         if (r < 0)
                                 return log_oom();
@@ -710,24 +710,24 @@ int mount_all(const char *dest,
                                 o = options;
                 }
 
-                if (FLAGS_SET(mount_table[k].mount_settings, MOUNT_PREFIX_ROOT)) {
+                if (FLAGS_SET(m->mount_settings, MOUNT_PREFIX_ROOT)) {
                         /* Optionally prefix the mount source with the root dir. This is useful in bind
                          * mounts to be created within the container image before we transition into it. Note
                          * that MOUNT_IN_USERNS is run after we transitioned hence prefixing is not necessary
                          * for those. */
-                        r = chase(mount_table[k].what, dest, CHASE_PREFIX_ROOT, &prefixed, NULL);
+                        r = chase(m->what, dest, CHASE_PREFIX_ROOT, &prefixed, NULL);
                         if (r < 0)
-                                return log_error_errno(r, "Failed to resolve %s%s: %m", strempty(dest), mount_table[k].what);
+                                return log_error_errno(r, "Failed to resolve %s%s: %m", strempty(dest), m->what);
                 }
 
                 r = mount_verbose_full(
                                 fatal ? LOG_ERR : LOG_DEBUG,
-                                prefixed ?: mount_table[k].what,
+                                prefixed ?: m->what,
                                 where,
-                                mount_table[k].type,
-                                mount_table[k].flags,
+                                m->type,
+                                m->flags,
                                 o,
-                                FLAGS_SET(mount_table[k].mount_settings, MOUNT_FOLLOW_SYMLINKS));
+                                FLAGS_SET(m->mount_settings, MOUNT_FOLLOW_SYMLINKS));
                 if (r < 0 && fatal)
                         return r;
         }
index 3228b19bbae098626555a881ff0f6e1591da9664..1b2fe76f8ac9d70ae31f8844ab980c1deb88520e 100644 (file)
@@ -1582,11 +1582,9 @@ static int oci_seccomp_action_from_string(const char *name, uint32_t *ret) {
                  * here */
         };
 
-        size_t i;
-
-        for (i = 0; i < ELEMENTSOF(table); i++)
-                if (streq_ptr(name, table[i].name)) {
-                        *ret = table[i].action;
+        FOREACH_ELEMENT(i, table)
+                if (streq_ptr(name, i->name)) {
+                        *ret = i->action;
                         return 0;
                 }
 
@@ -1630,11 +1628,9 @@ static int oci_seccomp_arch_from_string(const char *name, uint32_t *ret) {
                 { "SCMP_ARCH_X86_64",      SCMP_ARCH_X86_64      },
         };
 
-        size_t i;
-
-        for (i = 0; i < ELEMENTSOF(table); i++)
-                if (streq_ptr(table[i].name, name)) {
-                        *ret = table[i].arch;
+        FOREACH_ELEMENT(i, table)
+                if (streq_ptr(i->name, name)) {
+                        *ret = i->arch;
                         return 0;
                 }
 
@@ -1656,11 +1652,9 @@ static int oci_seccomp_compare_from_string(const char *name, enum scmp_compare *
                 { "SCMP_CMP_MASKED_EQ", SCMP_CMP_MASKED_EQ },
         };
 
-        size_t i;
-
-        for (i = 0; i < ELEMENTSOF(table); i++)
-                if (streq_ptr(table[i].name, name)) {
-                        *ret = table[i].op;
+        FOREACH_ELEMENT(i, table)
+                if (streq_ptr(i->name, name)) {
+                        *ret = i->op;
                         return 0;
                 }
 
index 673b627c3b13fac77b8b23b08917cebaff216726..4d45651b3b001edf843bc8df3d81c6bcbf3fd951 100644 (file)
@@ -136,18 +136,18 @@ static int add_syscall_filters(
         _cleanup_strv_free_ char **added = NULL;
         int r;
 
-        for (size_t i = 0; i < ELEMENTSOF(allow_list); i++) {
-                if (allow_list[i].capability != 0 && (cap_list_retain & (1ULL << allow_list[i].capability)) == 0)
+        FOREACH_ELEMENT(i, allow_list) {
+                if (i->capability != 0 && (cap_list_retain & (1ULL << i->capability)) == 0)
                         continue;
 
                 r = seccomp_add_syscall_filter_item(ctx,
-                                                    allow_list[i].name,
+                                                    i->name,
                                                     SCMP_ACT_ALLOW,
                                                     syscall_deny_list,
                                                     false,
                                                     &added);
                 if (r < 0)
-                        return log_error_errno(r, "Failed to add syscall filter item %s: %m", allow_list[i].name);
+                        return log_error_errno(r, "Failed to add syscall filter item %s: %m", i->name);
         }
 
         STRV_FOREACH(p, syscall_allow_list) {
index 41d10357ede85d2fdfe449bb56d6c943483f896f..c8e65f183e51b70d44e9cd3bde8d3bf601b92f2f 100644 (file)
@@ -3288,17 +3288,15 @@ static int patch_sysctl(void) {
 
         STRV_FOREACH_PAIR(k, v, arg_sysctl) {
                 bool good = false;
-                size_t i;
 
-                for (i = 0; i < ELEMENTSOF(safe_sysctl); i++) {
-
-                        if (!FLAGS_SET(flags, safe_sysctl[i].clone_flags))
+                FOREACH_ELEMENT(i, safe_sysctl) {
+                        if (!FLAGS_SET(flags, i->clone_flags))
                                 continue;
 
-                        if (safe_sysctl[i].prefix)
-                                good = startswith(*k, safe_sysctl[i].key);
+                        if (i->prefix)
+                                good = startswith(*k, i->key);
                         else
-                                good = streq(*k, safe_sysctl[i].key);
+                                good = streq(*k, i->key);
 
                         if (good)
                                 break;
index bd0822e6dcbbda78d7c6d76463e3fe255ad0052d..40c51fe8ddbc4dfdcb2b1b0a9c00d7d053c458d8 100644 (file)
@@ -1376,23 +1376,23 @@ int manager_dns_stub_start(Manager *m) {
                           m->dns_stub_listener_mode == DNS_STUB_LISTENER_TCP ? "TCP" :
                           "UDP/TCP");
 
-                for (size_t i = 0; i < ELEMENTSOF(stub_sockets); i++) {
+                FOREACH_ELEMENT(s, stub_sockets) {
                         union in_addr_union a = {
-                                .in.s_addr = htobe32(stub_sockets[i].addr),
+                                .in.s_addr = htobe32(s->addr),
                         };
 
-                        if (m->dns_stub_listener_mode == DNS_STUB_LISTENER_UDP && stub_sockets[i].socket_type == SOCK_STREAM)
+                        if (m->dns_stub_listener_mode == DNS_STUB_LISTENER_UDP && s->socket_type == SOCK_STREAM)
                                 continue;
-                        if (m->dns_stub_listener_mode == DNS_STUB_LISTENER_TCP && stub_sockets[i].socket_type == SOCK_DGRAM)
+                        if (m->dns_stub_listener_mode == DNS_STUB_LISTENER_TCP && s->socket_type == SOCK_DGRAM)
                                 continue;
 
-                        r = manager_dns_stub_fd(m, AF_INET, &a, stub_sockets[i].socket_type);
+                        r = manager_dns_stub_fd(m, AF_INET, &a, s->socket_type);
                         if (r < 0) {
                                 _cleanup_free_ char *busy_socket = NULL;
 
                                 if (asprintf(&busy_socket,
                                              "%s socket " IPV4_ADDRESS_FMT_STR ":53",
-                                             stub_sockets[i].socket_type == SOCK_DGRAM ? "UDP" : "TCP",
+                                             s->socket_type == SOCK_DGRAM ? "UDP" : "TCP",
                                              IPV4_ADDRESS_FMT_VAL(a.in)) < 0)
                                         return log_oom();
 
index 7b053862139f1ef2d4478558bea5d0219de497f0..0044cd4d95ab760c4e55cbc199741da9eb04c02e 100644 (file)
@@ -235,12 +235,12 @@ static void strip_localhost(EtcHosts *hosts) {
          * This way our regular synthesizing can take over, but only if it would result in the exact same
          * mappings.  */
 
-        for (size_t j = 0; j < ELEMENTSOF(local_in_addrs); j++) {
+        FOREACH_ELEMENT(local_in_addr, local_in_addrs) {
                 bool all_localhost, all_local_address;
                 EtcHostsItemByAddress *item;
                 const char *name;
 
-                item = hashmap_get(hosts->by_address, local_in_addrs + j);
+                item = hashmap_get(hosts->by_address, local_in_addr);
                 if (!item)
                         continue;
 
@@ -284,7 +284,7 @@ static void strip_localhost(EtcHosts *hosts) {
                 SET_FOREACH(name, item->names)
                         etc_hosts_item_by_name_free(hashmap_remove(hosts->by_name, name));
 
-                assert_se(hashmap_remove(hosts->by_address, local_in_addrs + j) == item);
+                assert_se(hashmap_remove(hosts->by_address, local_in_addr) == item);
                 etc_hosts_item_by_address_free(item);
         }
 }
index c9963ade93087a351196769a5059d108e7b9985f..31f6db51375045871a16057b7f4dee1fb5990b85 100644 (file)
@@ -760,7 +760,7 @@ int dns_name_address(const char *p, int *ret_family, union in_addr_union *ret_ad
         if (r > 0) {
                 uint8_t a[4];
 
-                for (size_t i = 0; i < ELEMENTSOF(a); i++) {
+                FOREACH_ELEMENT(i, a) {
                         char label[DNS_LABEL_MAX+1];
 
                         r = dns_label_unescape(&p, label, sizeof label, 0);
@@ -771,7 +771,7 @@ int dns_name_address(const char *p, int *ret_family, union in_addr_union *ret_ad
                         if (r > 3)
                                 return -EINVAL;
 
-                        r = safe_atou8(label, &a[i]);
+                        r = safe_atou8(label, i);
                         if (r < 0)
                                 return r;
                 }
index 174614a43208f8be2e576837f79d30f16c51111b..51c75780cd6fee8c6f2aae518db4dc01c6666c9b 100644 (file)
@@ -48,9 +48,9 @@ int wol_options_to_string_alloc(uint32_t opts, char **ret) {
                 return 0;
         }
 
-        for (size_t i = 0; i < ELEMENTSOF(wol_option_map); i++)
-                if (opts & wol_option_map[i].opt &&
-                    !strextend_with_separator(&str, ",", wol_option_map[i].name))
+        FOREACH_ELEMENT(option, wol_option_map)
+                if (opts & option->opt &&
+                    !strextend_with_separator(&str, ",", option->name))
                         return -ENOMEM;
 
         if (!str) {
@@ -1317,9 +1317,9 @@ int config_parse_wol(
                 if (r == 0)
                         break;
 
-                for (size_t i = 0; i < ELEMENTSOF(wol_option_map); i++)
-                        if (streq(w, wol_option_map[i].name)) {
-                                new_opts |= wol_option_map[i].opt;
+                FOREACH_ELEMENT(option, wol_option_map)
+                        if (streq(w, option->name)) {
+                                new_opts |= option->opt;
                                 found = true;
                                 break;
                         }
index 93f8cf379ebc291e113722b0d2bbf0a04ee00a89..20b402e3d33d2bcfcce233c85ba98f9730096d9d 100644 (file)
@@ -665,11 +665,11 @@ int mount_flags_to_string(unsigned long flags, char **ret) {
 
         assert(ret);
 
-        for (size_t i = 0; i < ELEMENTSOF(map); i++)
-                if (flags & map[i].flag) {
-                        if (!strextend_with_separator(&str, "|", map[i].name))
+        FOREACH_ELEMENT(entry, map)
+                if (flags & entry->flag) {
+                        if (!strextend_with_separator(&str, "|", entry->name))
                                 return -ENOMEM;
-                        flags &= ~map[i].flag;
+                        flags &= ~entry->flag;
                 }
 
         if (!str || flags != 0)
index 5033566878437e260a641d825dfc2f422de6817d..67b7eb4d904d2df47203616e4cce4489f57276f9 100644 (file)
@@ -35,9 +35,9 @@ const NamingScheme* naming_scheme_from_name(const char *name) {
          * the table like any other name. After iterating through the table, we check for "latest" again,
          * which means that if not mapped explicitly, it maps to the last defined entry, whatever that is. */
 
-        for (size_t i = 0; i < ELEMENTSOF(naming_schemes); i++)
-                if (streq(naming_schemes[i].name, name))
-                        return naming_schemes + i;
+        FOREACH_ELEMENT(scheme, naming_schemes)
+                if (streq(scheme->name, name))
+                        return scheme;
 
         if (streq(name, "latest"))
                 return naming_schemes + ELEMENTSOF(naming_schemes) - 1;
index bd1bd5c0e3f24cd721417cc867c31e79f1e593bd..96d3085f6995449f93c5f47836a00d70a233880b 100644 (file)
@@ -1734,14 +1734,13 @@ int seccomp_restrict_realtime_full(int error_code) {
 
         int r, max_policy = 0;
         uint32_t arch;
-        unsigned i;
 
         assert(error_code > 0);
 
         /* Determine the highest policy constant we want to allow */
-        for (i = 0; i < ELEMENTSOF(permitted_policies); i++)
-                if (permitted_policies[i] > max_policy)
-                        max_policy = permitted_policies[i];
+        FOREACH_ELEMENT(policy, permitted_policies)
+                if (*policy > max_policy)
+                        max_policy = *policy;
 
         SECCOMP_FOREACH_LOCAL_ARCH(arch) {
                 _cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL;
@@ -1759,8 +1758,8 @@ int seccomp_restrict_realtime_full(int error_code) {
                         bool good = false;
 
                         /* Check if this is in the allow list. */
-                        for (i = 0; i < ELEMENTSOF(permitted_policies); i++)
-                                if (permitted_policies[i] == p) {
+                        FOREACH_ELEMENT(policy, permitted_policies)
+                                if (*policy == p) {
                                         good = true;
                                         break;
                                 }
index 12447a93379c708f713d00e2e05616cbe0f8139f..b03a38eb18a540ec6defef72af335a16f65e8280 100644 (file)
@@ -1483,34 +1483,34 @@ int user_group_record_mangle(
                 return json_log(v, json_flags, SYNTHETIC_ERRNO(EINVAL), "Stripping everything from record, refusing.");
 
         /* Check if we have the special sections and if they match our flags set */
-        for (size_t i = 0; i < ELEMENTSOF(mask_field); i++) {
+        FOREACH_ELEMENT(i, mask_field) {
                 sd_json_variant *e, *k;
 
-                if (FLAGS_SET(USER_RECORD_STRIP_MASK(load_flags), mask_field[i].mask)) {
+                if (FLAGS_SET(USER_RECORD_STRIP_MASK(load_flags), i->mask)) {
                         if (!w)
                                 w = sd_json_variant_ref(v);
 
-                        r = sd_json_variant_filter(&w, STRV_MAKE(mask_field[i].name));
+                        r = sd_json_variant_filter(&w, STRV_MAKE(i->name));
                         if (r < 0)
                                 return json_log(w, json_flags, r, "Failed to remove field from variant: %m");
 
                         continue;
                 }
 
-                e = sd_json_variant_by_key_full(v, mask_field[i].name, &k);
+                e = sd_json_variant_by_key_full(v, i->name, &k);
                 if (e) {
-                        if (!FLAGS_SET(USER_RECORD_ALLOW_MASK(load_flags), mask_field[i].mask))
-                                return json_log(e, json_flags, SYNTHETIC_ERRNO(EBADMSG), "Record contains '%s' field, which is not allowed.", mask_field[i].name);
+                        if (!FLAGS_SET(USER_RECORD_ALLOW_MASK(load_flags), i->mask))
+                                return json_log(e, json_flags, SYNTHETIC_ERRNO(EBADMSG), "Record contains '%s' field, which is not allowed.", i->name);
 
                         if (FLAGS_SET(load_flags, USER_RECORD_STRIP_REGULAR)) {
                                 array[n_retain++] = k;
                                 array[n_retain++] = e;
                         }
 
-                        m |= mask_field[i].mask;
+                        m |= i->mask;
                 } else {
-                        if (FLAGS_SET(USER_RECORD_REQUIRE_MASK(load_flags), mask_field[i].mask))
-                                return json_log(v, json_flags, SYNTHETIC_ERRNO(EBADMSG), "Record lacks '%s' field, which is required.", mask_field[i].name);
+                        if (FLAGS_SET(USER_RECORD_REQUIRE_MASK(load_flags), i->mask))
+                                return json_log(v, json_flags, SYNTHETIC_ERRNO(EBADMSG), "Record lacks '%s' field, which is required.", i->name);
                 }
         }
 
@@ -1530,8 +1530,8 @@ int user_group_record_mangle(
 
                         assert_se(f = sd_json_variant_string(sd_json_variant_by_index(v, i)));
 
-                        for (size_t j = 0; j < ELEMENTSOF(mask_field); j++)
-                                if (streq(f, mask_field[j].name)) { /* already covered in the loop above */
+                        FOREACH_ELEMENT(j, mask_field)
+                                if (streq(f, j->name)) { /* already covered in the loop above */
                                         special = true;
                                         continue;
                                 }
index 88faa1efb2bf4bcf65221e4c3b76d489abe2714d..1c3873114ae6f41dd08318ee462fea3f99520887 100644 (file)
@@ -65,13 +65,13 @@ TEST_RET(bootspec_sort) {
 
         ASSERT_OK(mkdtemp_malloc("/tmp/bootspec-testXXXXXX", &d));
 
-        for (size_t i = 0; i < ELEMENTSOF(entries); i++) {
+        FOREACH_ELEMENT(entry, entries) {
                 _cleanup_free_ char *j = NULL;
 
-                j = path_join(d, "/loader/entries/", entries[i].fname);
+                j = path_join(d, "/loader/entries/", entry->fname);
                 assert_se(j);
 
-                ASSERT_OK(write_string_file(j, entries[i].contents, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_MKDIR_0755));
+                ASSERT_OK(write_string_file(j, entry->contents, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_MKDIR_0755));
         }
 
         ASSERT_OK(boot_config_load(&config, d, NULL));
@@ -176,13 +176,13 @@ TEST_RET(bootspec_boot_config_find_entry) {
 
         assert_se(mkdtemp_malloc("/tmp/bootspec-testXXXXXX", &d) >= 0);
 
-        for (size_t i = 0; i < ELEMENTSOF(entries); i++) {
+        FOREACH_ELEMENT(entry, entries) {
                 _cleanup_free_ char *j = NULL;
 
-                j = path_join(d, "/loader/entries/", entries[i].fname);
+                j = path_join(d, "/loader/entries/", entry->fname);
                 assert_se(j);
 
-                assert_se(write_string_file(j, entries[i].contents, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_MKDIR_0755) >= 0);
+                assert_se(write_string_file(j, entry->contents, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_MKDIR_0755) >= 0);
         }
 
         assert_se(boot_config_load(&config, d, NULL) >= 0);
index 475f089fa847ae2accf0b1f90a97e706ae625788..03c0a245bd4b4c59fc75e0d00cca699e70800852 100644 (file)
@@ -46,8 +46,8 @@ static int parse_argv(int argc, char *argv[]) {
                                "  %s [OPTION...] path...\n"
                                "Options:\n"
                                , argv[0]);
-                        for (size_t i = 0; i < ELEMENTSOF(options) - 1; i++)
-                                printf("  --%s\n", options[i].name);
+                        FOREACH_ARRAY(option, options, ELEMENTSOF(options) - 1)
+                                printf("  --%s\n", option->name);
                         return 0;
 
                 case ARG_ROOT:
index d76ef679a180f4b717b15fdc1e31855e49a364fa..7c222b09327eaf54d82d22c628fbc978a93ab306 100644 (file)
@@ -892,14 +892,14 @@ TEST(read_line4) {
 
         int r;
 
-        for (size_t i = 0; i < ELEMENTSOF(eof_endings); i++) {
+        FOREACH_ELEMENT(ending, eof_endings) {
                 _cleanup_fclose_ FILE *f = NULL;
                 _cleanup_free_ char *s = NULL;
 
-                assert_se(f = fmemopen_unlocked((void*) eof_endings[i].string, eof_endings[i].length, "r"));
+                assert_se(f = fmemopen_unlocked((void*) ending->string, ending->length, "r"));
 
                 r = read_line(f, SIZE_MAX, &s);
-                assert_se((size_t) r == eof_endings[i].length);
+                assert_se((size_t) r == ending->length);
                 ASSERT_STREQ(s, "foo");
 
                 assert_se(read_line(f, SIZE_MAX, NULL) == 0); /* Ensure we hit EOF */
index cb45c150879a668e80af65af485f12127a97c9cb..e1485a84d4fc954b28c43f84a5c82525083726bd 100644 (file)
@@ -688,7 +688,7 @@ static const struct hash_ops crippled_hashmap_ops = {
 
 TEST(hashmap_many) {
         Hashmap *h;
-        unsigned i, j;
+        unsigned i;
         void *v, *k;
         bool slow = slow_tests_enabled();
         const struct {
@@ -702,24 +702,24 @@ TEST(hashmap_many) {
 
         log_info("/* %s (%s) */", __func__, slow ? "slow" : "fast");
 
-        for (j = 0; j < ELEMENTSOF(tests); j++) {
+        FOREACH_ELEMENT(test, tests) {
                 usec_t ts = now(CLOCK_MONOTONIC), n;
 
-                assert_se(h = hashmap_new(tests[j].ops));
+                assert_se(h = hashmap_new(test->ops));
 
-                for (i = 1; i < tests[j].n_entries*3; i+=3) {
+                for (i = 1; i < test->n_entries*3; i+=3) {
                         assert_se(hashmap_put(h, UINT_TO_PTR(i), UINT_TO_PTR(i)) >= 0);
                         assert_se(PTR_TO_UINT(hashmap_get(h, UINT_TO_PTR(i))) == i);
                 }
 
-                for (i = 1; i < tests[j].n_entries*3; i++)
+                for (i = 1; i < test->n_entries*3; i++)
                         assert_se(hashmap_contains(h, UINT_TO_PTR(i)) == (i % 3 == 1));
 
                 log_info("%s %u <= %u * 0.8 = %g",
-                         tests[j].title, hashmap_size(h), hashmap_buckets(h), hashmap_buckets(h) * 0.8);
+                         test->title, hashmap_size(h), hashmap_buckets(h), hashmap_buckets(h) * 0.8);
 
                 assert_se(hashmap_size(h) <= hashmap_buckets(h) * 0.8);
-                assert_se(hashmap_size(h) == tests[j].n_entries);
+                assert_se(hashmap_size(h) == test->n_entries);
 
                 while (!hashmap_isempty(h)) {
                         k = hashmap_first_key(h);
@@ -754,9 +754,9 @@ TEST(hashmap_free) {
 
         log_info("/* %s (%s, %u entries) */", __func__, slow ? "slow" : "fast", n_entries);
 
-        for (unsigned j = 0; j < ELEMENTSOF(tests); j++) {
+        FOREACH_ELEMENT(test, tests) {
                 ts = now(CLOCK_MONOTONIC);
-                assert_se(h = hashmap_new(tests[j].ops));
+                assert_se(h = hashmap_new(test->ops));
 
                 custom_counter = 0;
                 for (unsigned i = 0; i < n_entries; i++) {
@@ -774,9 +774,9 @@ TEST(hashmap_free) {
                 hashmap_free(h);
 
                 n = now(CLOCK_MONOTONIC);
-                log_info("%s test took %s", tests[j].title, FORMAT_TIMESPAN(n - ts, 0));
+                log_info("%s test took %s", test->title, FORMAT_TIMESPAN(n - ts, 0));
 
-                assert_se(custom_counter == tests[j].expect_counter);
+                assert_se(custom_counter == test->expect_counter);
         }
 }
 
index 519fff71d57bd95441b5f4d81a3970770aa3c2d3..1589b7e597d10ccffe651ea3fac64a3d2c42a841 100644 (file)
@@ -11,12 +11,12 @@ int main(int argc, char *argv[]) {
 
         test_setup_logging(LOG_DEBUG);
 
-        for (size_t i = 0; i < ELEMENTSOF(test_states); i++) {
+        FOREACH_ELEMENT(state, test_states) {
                 /* fake a unit */
                 Service s = {
                         .meta.load_state = UNIT_LOADED,
                         .type = SERVICE_SIMPLE,
-                        .state = test_states[i],
+                        .state = *state,
                 };
                 Unit *u = UNIT(&s);
 
index 6c6407028468b09e1779534b9b51ea553d98b987..c409e08ea478215dc48eefec87c717d047e948a9 100644 (file)
@@ -20,10 +20,10 @@ int main(int argc, const char *argv[]) {
         ASSERT_NULL(head);
         ASSERT_NULL(head2);
 
-        for (i = 0; i < ELEMENTSOF(items); i++) {
-                LIST_INIT(item_list, &items[i]);
-                assert_se(LIST_JUST_US(item_list, &items[i]));
-                assert_se(LIST_PREPEND(item_list, head, &items[i]) == &items[i]);
+        FOREACH_ELEMENT(item, items) {
+                LIST_INIT(item_list, item);
+                assert_se(LIST_JUST_US(item_list, item));
+                assert_se(LIST_PREPEND(item_list, head, item) == item);
         }
 
         i = 0;
@@ -183,9 +183,9 @@ int main(int argc, const char *argv[]) {
 
         ASSERT_NULL(head);
 
-        for (i = 0; i < ELEMENTSOF(items); i++) {
-                assert_se(LIST_JUST_US(item_list, &items[i]));
-                assert_se(LIST_APPEND(item_list, head, &items[i]) == &items[i]);
+        FOREACH_ELEMENT(item, items) {
+                assert_se(LIST_JUST_US(item_list, item));
+                assert_se(LIST_APPEND(item_list, head, item) == item);
         }
 
         assert_se(!LIST_JUST_US(item_list, head));
@@ -200,15 +200,15 @@ int main(int argc, const char *argv[]) {
         assert_se(items[2].item_list_prev == &items[1]);
         assert_se(items[3].item_list_prev == &items[2]);
 
-        for (i = 0; i < ELEMENTSOF(items); i++)
-                assert_se(LIST_REMOVE(item_list, head, &items[i]) == &items[i]);
+        FOREACH_ELEMENT(item, items)
+                assert_se(LIST_REMOVE(item_list, head, item) == item);
 
         ASSERT_NULL(head);
 
-        for (i = 0; i < ELEMENTSOF(items) / 2; i++) {
-                LIST_INIT(item_list, &items[i]);
-                assert_se(LIST_JUST_US(item_list, &items[i]));
-                assert_se(LIST_PREPEND(item_list, head, &items[i]) == &items[i]);
+        FOREACH_ARRAY(item, items, ELEMENTSOF(items) / 2) {
+                LIST_INIT(item_list, item);
+                assert_se(LIST_JUST_US(item_list, item));
+                assert_se(LIST_PREPEND(item_list, head, item) == item);
         }
 
         for (i = ELEMENTSOF(items) / 2; i < ELEMENTSOF(items); i++) {
@@ -244,8 +244,8 @@ int main(int argc, const char *argv[]) {
         ASSERT_NULL(head2);
         assert_se(head);
 
-        for (i = 0; i < ELEMENTSOF(items); i++)
-                assert_se(LIST_REMOVE(item_list, head, &items[i]) == &items[i]);
+        FOREACH_ELEMENT(item, items)
+                assert_se(LIST_REMOVE(item_list, head, item) == item);
 
         ASSERT_NULL(head);
 
index d1a51756ed3df5358509db3c6965e9dcdcc34002..038165aa176fbffe2ee53f0a69da8942efd74585 100644 (file)
@@ -912,22 +912,21 @@ TEST(config_parse_memory_limit) {
                 { "MemoryMax",  "10",           &c.memory_max,  10 },
                 { "MemoryMax",  "infinity",     &c.memory_max,  CGROUP_LIMIT_MAX },
         };
-        size_t i;
         int r;
 
-        for (i = 0; i < ELEMENTSOF(limit_tests); i++) {
+        FOREACH_ELEMENT(test, limit_tests) {
                 c.memory_min = CGROUP_LIMIT_DUMMY;
                 c.memory_low = CGROUP_LIMIT_DUMMY;
                 c.memory_high = CGROUP_LIMIT_DUMMY;
                 c.memory_max = CGROUP_LIMIT_DUMMY;
                 r = config_parse_memory_limit(NULL, "fake", 1, "section", 1,
-                                              limit_tests[i].limit, 1,
-                                              limit_tests[i].value, &c, NULL);
+                                              test->limit, 1,
+                                              test->value, &c, NULL);
                 log_info("%s=%s\t%"PRIu64"==%"PRIu64,
-                         limit_tests[i].limit, limit_tests[i].value,
-                         *limit_tests[i].result, limit_tests[i].expected);
+                         test->limit, test->value,
+                         *test->result, test->expected);
                 assert_se(r >= 0);
-                assert_se(*limit_tests[i].result == limit_tests[i].expected);
+                assert_se(*test->result == test->expected);
         }
 
 }
@@ -1029,12 +1028,12 @@ TEST(config_parse_log_filter_patterns) {
         if (ERRNO_IS_NOT_SUPPORTED(dlopen_pcre2()))
                 return (void) log_tests_skipped("PCRE2 support is not available");
 
-        for (size_t i = 0; i < ELEMENTSOF(regex_tests); i++) {
+        FOREACH_ELEMENT(test, regex_tests) {
                 assert_se(config_parse_log_filter_patterns(NULL, "fake", 1, "section", 1, "LogFilterPatterns", 1,
-                                                           regex_tests[i].regex, &c, NULL) >= 0);
+                                                           test->regex, &c, NULL) >= 0);
 
-                assert_se(set_size(c.log_filter_allowed_patterns) == regex_tests[i].allowed_patterns_count);
-                assert_se(set_size(c.log_filter_denied_patterns) == regex_tests[i].denied_patterns_count);
+                assert_se(set_size(c.log_filter_allowed_patterns) == test->allowed_patterns_count);
+                assert_se(set_size(c.log_filter_denied_patterns) == test->denied_patterns_count);
 
                 /* Ensure `~` is properly removed */
                 const char *p;
index bb1eefb08387b54f94a9ac0809335eb445e3c692..45e41572968ee008118988b3dafcca9e61f219fd 100644 (file)
@@ -46,11 +46,11 @@ TEST(set_free_with_hash_ops) {
 
         assert_se(m = ordered_set_new(&item_hash_ops));
 
-        for (size_t i = 0; i < ELEMENTSOF(items) - 1; i++)
-                assert_se(ordered_set_put(m, items + i) == 1);
+        FOREACH_ARRAY(item, items, ELEMENTSOF(items) - 1)
+                assert_se(ordered_set_put(m, item) == 1);
 
-        for (size_t i = 0; i < ELEMENTSOF(items) - 1; i++)
-                assert_se(ordered_set_put(m, items + i) == 0);  /* We get 0 here, because we use trivial hash
+        FOREACH_ARRAY(item, items, ELEMENTSOF(items) - 1)
+                assert_se(ordered_set_put(m, item) == 0);  /* We get 0 here, because we use trivial hash
                                                                  * ops. Also see below... */
 
         m = ordered_set_free(m);
index 92c22d3b20d78d346f82131ccfbecbee1253f61d..89c2b73e320ef699d73d060bdbc07ce6a8601b18 100644 (file)
@@ -17,15 +17,15 @@ static int unsigned_compare(const unsigned *a, const unsigned *b) {
 
 TEST(unsigned) {
         _cleanup_(prioq_freep) Prioq *q = NULL;
-        unsigned buffer[SET_SIZE], i, u, n;
+        unsigned buffer[SET_SIZE], u, n;
 
         srand(0);
 
         assert_se(q = prioq_new(trivial_compare_func));
 
-        for (i = 0; i < ELEMENTSOF(buffer); i++) {
+        FOREACH_ELEMENT(i, buffer) {
                 u = (unsigned) rand();
-                buffer[i] = u;
+                *i = u;
                 assert_se(prioq_put(q, UINT_TO_PTR(u), NULL) >= 0);
 
                 n = prioq_size(q);
@@ -34,7 +34,7 @@ TEST(unsigned) {
 
         typesafe_qsort(buffer, ELEMENTSOF(buffer), unsigned_compare);
 
-        for (i = 0; i < ELEMENTSOF(buffer); i++) {
+        for (unsigned i = 0; i < ELEMENTSOF(buffer); i++) {
                 assert_se(prioq_size(q) == ELEMENTSOF(buffer) - i);
 
                 u = PTR_TO_UINT(prioq_pop(q));
index 23bac0255a84ebd996579ca72ad805b49426a1af..f1e6e9314b534f1b113b388ab77683c14c7c7e22 100644 (file)
@@ -37,8 +37,8 @@ TEST(set_free_with_destructor) {
         struct Item items[4] = {};
 
         assert_se(m = set_new(NULL));
-        for (size_t i = 0; i < ELEMENTSOF(items) - 1; i++)
-                assert_se(set_put(m, items + i) == 1);
+        FOREACH_ARRAY(item, items, ELEMENTSOF(items) - 1)
+                assert_se(set_put(m, item) == 1);
 
         m = set_free_with_destructor(m, item_seen);
         assert_se(items[0].seen == 1);
@@ -54,8 +54,8 @@ TEST(set_free_with_hash_ops) {
         struct Item items[4] = {};
 
         assert_se(m = set_new(&item_hash_ops));
-        for (size_t i = 0; i < ELEMENTSOF(items) - 1; i++)
-                assert_se(set_put(m, items + i) == 1);
+        FOREACH_ARRAY(item, items, ELEMENTSOF(items) - 1)
+                assert_se(set_put(m, item) == 1);
 
         m = set_free(m);
         assert_se(items[0].seen == 1);
index 4bf7548ac1584453f8736fab5cc411cc6c9a18d1..999d3bacb8544efb2a9045b80dcce29b80bf2f30 100644 (file)
@@ -80,10 +80,10 @@ TEST(free_and_strndup) {
         _cleanup_free_ char *t = NULL;
         const char *prev_expected = t;
 
-        for (unsigned i = 0; i < ELEMENTSOF(cases); i++) {
+        FOREACH_ELEMENT(c, cases) {
                 test_free_and_strndup_one(&t,
-                                          cases[i].src, cases[i].len, cases[i].expected,
-                                          !streq_ptr(cases[i].expected, prev_expected));
+                                          c->src, c->len, c->expected,
+                                          !streq_ptr(c->expected, prev_expected));
                 prev_expected = t;
         }
 }
index 9e87c4d195f1f057857960c6774d9a4a04f225b4..ad800a41115bfb27e3b9bad71c6eb6c0fea2bb44 100644 (file)
@@ -126,8 +126,8 @@ static const Color colors[] = {
 };
 
 TEST(colors) {
-        for (size_t i = 0; i < ELEMENTSOF(colors); i++)
-                printf("<%s%s%s>\n", colors[i].func(), colors[i].name, ansi_normal());
+        FOREACH_ELEMENT(color, colors)
+                printf("<%s%s%s>\n", colors->func(), color->name, ansi_normal());
 }
 
 TEST(text) {
index 8caf51b260a662825f16f1f3d62037947754dd63..e4f1f861b4736ae24b468165783ec6f966098c2c 100644 (file)
@@ -152,8 +152,8 @@ static void _test_pcr_selection_mask_hash(uint32_t mask, TPMI_ALG_HASH hash) {
         uint32_t test_masks[] = {
                 0x0, 0x1, 0x100, 0x10000, 0xf0f0f0, 0xaaaaaa, 0xffffff,
         };
-        for (unsigned i = 0; i < ELEMENTSOF(test_masks); i++) {
-                uint32_t test_mask = test_masks[i];
+        FOREACH_ELEMENT(i, test_masks) {
+                uint32_t test_mask = *i;
 
                 TPMS_PCR_SELECTION a = POISON_TPMS, b = POISON_TPMS, test_s = POISON_TPMS;
                 tpm2_tpms_pcr_selection_from_mask(test_mask, hash, &test_s);
@@ -181,11 +181,11 @@ static void _test_pcr_selection_mask_hash(uint32_t mask, TPMI_ALG_HASH hash) {
 TEST(tpms_pcr_selection_mask_and_hash) {
         TPMI_ALG_HASH HASH_ALGS[] = { TPM2_ALG_SHA1, TPM2_ALG_SHA256, };
 
-        for (unsigned i = 0; i < ELEMENTSOF(HASH_ALGS); i++)
+        FOREACH_ELEMENT(hash, HASH_ALGS)
                 for (uint32_t m2 = 0; m2 <= 0xffffff; m2 += 0x50000)
                         for (uint32_t m1 = 0; m1 <= 0xffff; m1 += 0x500)
                                 for (uint32_t m0 = 0; m0 <= 0xff; m0 += 0x5)
-                                        _test_pcr_selection_mask_hash(m0 | m1 | m2, HASH_ALGS[i]);
+                                        _test_pcr_selection_mask_hash(m0 | m1 | m2, *hash);
 }
 
 static void _test_tpms_sw(
@@ -1191,11 +1191,9 @@ static int check_calculate_seal(Tpm2Context *c) {
         calculate_seal_and_unseal(c, TPM2_SRK_HANDLE, srk_public);
 
         TPMI_ALG_ASYM test_algs[] = { TPM2_ALG_RSA, TPM2_ALG_ECC, };
-        for (unsigned i = 0; i < ELEMENTSOF(test_algs); i++) {
-                TPMI_ALG_ASYM alg = test_algs[i];
-
+        FOREACH_ELEMENT(alg, test_algs) {
                 TPM2B_PUBLIC template = { .size = sizeof(TPMT_PUBLIC), };
-                assert_se(tpm2_get_srk_template(alg, &template.publicArea) >= 0);
+                assert_se(tpm2_get_srk_template(*alg, &template.publicArea) >= 0);
 
                 _cleanup_free_ TPM2B_PUBLIC *public = NULL;
                 _cleanup_(tpm2_handle_freep) Tpm2Handle *handle = NULL;
index 67b796a3355e5625cfea0bf7472f5aacf5d26411..8315169ba72eda1b5ff59d9af4b366e8b00a7e7c 100644 (file)
@@ -335,8 +335,8 @@ static bool manager_sample_spike_detection(Manager *m, double offset, double del
                         idx_min = i;
 
         j = 0;
-        for (i = 0; i < ELEMENTSOF(m->samples); i++)
-                j += pow(m->samples[i].offset - m->samples[idx_min].offset, 2);
+        FOREACH_ELEMENT(sample, m->samples)
+                j += pow(sample->offset - m->samples[idx_min].offset, 2);
         m->samples_jitter = sqrt(j / (ELEMENTSOF(m->samples) - 1));
 
         /* ignore samples when resyncing */
index edfbb38ad84b7b93b9c9117cf99febde79186bbd..7787f66981d24001260feee7a2f4b65daa76a860 100644 (file)
@@ -228,7 +228,6 @@ int link_load_one(LinkConfigContext *ctx, const char *filename) {
         _cleanup_hashmap_free_ Hashmap *stats_by_path = NULL;
         _cleanup_free_ char *name = NULL;
         const char *dropin_dirname;
-        size_t i;
         int r;
 
         assert(ctx);
@@ -267,8 +266,8 @@ int link_load_one(LinkConfigContext *ctx, const char *filename) {
                 .sr_iov_num_vfs = UINT32_MAX,
         };
 
-        for (i = 0; i < ELEMENTSOF(config->features); i++)
-                config->features[i] = -1;
+        FOREACH_ELEMENT(feature, config->features)
+                *feature = -1;
 
         dropin_dirname = strjoina(basename(filename), ".d");
         r = config_parse_many(
index 10da645d0df7a8623b8ce75214b07330c8cef3bc..db418135113b65efcab102eeefc2522912aafd2f 100644 (file)
@@ -76,10 +76,10 @@ static int fake_filesystems(void) {
         if (r < 0)
                 return log_error_errno(r, "Failed to detach mount namespace: %m");
 
-        for (size_t i = 0; i < ELEMENTSOF(fakefss); i++) {
-                r = mount_nofollow_verbose(fakefss[i].ignore_mount_error ? LOG_NOTICE : LOG_ERR,
-                                           fakefss[i].src, fakefss[i].target, NULL, MS_BIND, NULL);
-                if (r < 0 && !fakefss[i].ignore_mount_error)
+        FOREACH_ELEMENT(fakefs, fakefss) {
+                r = mount_nofollow_verbose(fakefs->ignore_mount_error ? LOG_NOTICE : LOG_ERR,
+                                           fakefs->src, fakefs->target, NULL, MS_BIND, NULL);
+                if (r < 0 && !fakefs->ignore_mount_error)
                         return r;
         }
 
index 1dec5f868ef4484fce59df5d7c0dae7a17a95006..876af195e22a5886fb7eb4dc6c0233f486362131 100644 (file)
@@ -295,8 +295,8 @@ static bool test_pointers(
                 size_t num_well_known_keys = 0;
 
                 if (has_keys)
-                        for (size_t i = 0; i < ELEMENTSOF(well_known_keyboard_keys); i++)
-                                if (test_bit(well_known_keyboard_keys[i], bitmask_key))
+                        FOREACH_ELEMENT(key, well_known_keyboard_keys)
+                                if (test_bit(*key, bitmask_key))
                                         num_well_known_keys++;
 
                 if (num_well_known_keys >= 4 || num_joystick_buttons + num_joystick_axes < 2) {
index bd74ab0a3a2234817c3667466e54b6ad796873d0..4bfa6d7a3e347d181afa3f3b1c3d3926c6593153 100644 (file)
@@ -58,16 +58,16 @@ static const struct subst_map_entry map[] = {
 };
 
 static const char *format_type_to_string(FormatSubstitutionType t) {
-        for (size_t i = 0; i < ELEMENTSOF(map); i++)
-                if (map[i].type == t)
-                        return map[i].name;
+        FOREACH_ELEMENT(entry, map)
+                if (entry->type == t)
+                        return entry->name;
         return NULL;
 }
 
 static char format_type_to_char(FormatSubstitutionType t) {
-        for (size_t i = 0; i < ELEMENTSOF(map); i++)
-                if (map[i].type == t)
-                        return map[i].fmt;
+        FOREACH_ELEMENT(entry, map)
+                if (entry->type == t)
+                        return entry->fmt;
         return '\0';
 }
 
index 899d4e6f7fb3fbc62a1ae7b1300f999c8446df78..30b6ddb7282ff178bc0c5d5fe8f3d26afe4e74f8 100644 (file)
@@ -31,7 +31,6 @@ static int help(void) {
         };
 
         _cleanup_free_ char *link = NULL;
-        size_t i;
         int r;
 
         r = terminal_urlify_man("udevadm", "8", &link);
@@ -43,8 +42,8 @@ static int help(void) {
                "Commands:\n",
                program_invocation_short_name);
 
-        for (i = 0; i < ELEMENTSOF(short_descriptions); i++)
-                printf("  %-12s  %s\n", short_descriptions[i][0], short_descriptions[i][1]);
+        FOREACH_ELEMENT(desc, short_descriptions)
+                printf("  %-12s  %s\n", (*desc)[0], (*desc)[1]);
 
         printf("\nSee the %s for details.\n", link);
         return 0;