Skip to content

Commit

Permalink
tree-wide: replace FOREACH_POINTER with FOREACH_ARGUMENT
Browse files Browse the repository at this point in the history
The latter is more generic and while being compatible with
the former.
  • Loading branch information
YHNdnzj committed Jan 24, 2024
1 parent e9a4666 commit 2d70878
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 146 deletions.
6 changes: 0 additions & 6 deletions src/basic/macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,6 @@ static inline int __coverity_check_and_return__(int condition) {
/* Pointers range from NULL to POINTER_MAX */
#define POINTER_MAX ((void*) UINTPTR_MAX)

/* Iterates through a specified list of pointers. Accepts NULL pointers, but uses POINTER_MAX as internal marker for EOL. */
#define FOREACH_POINTER(p, x, ...) \
for (typeof(p) *_l = (typeof(p)[]) { ({ p = x; }), ##__VA_ARGS__, POINTER_MAX }; \
p != (typeof(p)) POINTER_MAX; \
p = *(++_l))

#define _FOREACH_ARRAY(i, array, num, m, end) \
for (typeof(array[0]) *i = (array), *end = ({ \
typeof(num) m = (num); \
Expand Down
15 changes: 8 additions & 7 deletions src/basic/mountpoint-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -446,14 +446,15 @@ bool fstype_needs_quota(const char *fstype) {
}

bool fstype_is_api_vfs(const char *fstype) {
const FilesystemSet *fs;
assert(fstype);

FOREACH_POINTER(fs,
filesystem_sets + FILESYSTEM_SET_BASIC_API,
filesystem_sets + FILESYSTEM_SET_AUXILIARY_API,
filesystem_sets + FILESYSTEM_SET_PRIVILEGED_API,
filesystem_sets + FILESYSTEM_SET_TEMPORARY)
if (nulstr_contains(fs->value, fstype))
const FilesystemSet *fs;
FOREACH_ARGUMENT(fs,
filesystem_sets + FILESYSTEM_SET_BASIC_API,
filesystem_sets + FILESYSTEM_SET_AUXILIARY_API,
filesystem_sets + FILESYSTEM_SET_PRIVILEGED_API,
filesystem_sets + FILESYSTEM_SET_TEMPORARY)
if (nulstr_contains(fs->value, fstype))
return true;

/* Filesystems not present in the internal database */
Expand Down
2 changes: 1 addition & 1 deletion src/core/unit.c
Original file line number Diff line number Diff line change
Expand Up @@ -3959,7 +3959,7 @@ static int unit_pid_set(Unit *u, Set **pid_set) {
/* Exclude the main/control pids from being killed via the cgroup */

PidRef *pid;
FOREACH_POINTER(pid, unit_main_pid(u), unit_control_pid(u))
FOREACH_ARGUMENT(pid, unit_main_pid(u), unit_control_pid(u))
if (pidref_is_set(pid)) {
r = set_ensure_put(pid_set, NULL, PID_TO_PTR(pid->pid));
if (r < 0)
Expand Down
13 changes: 5 additions & 8 deletions src/home/homework-fscrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ static int fscrypt_setup(
_cleanup_free_ char *value = NULL;
size_t salt_size, encrypted_size;
const char *nr, *e;
char **list;
int n;

/* Check if this xattr has the format 'trusted.fscrypt_slot<nr>' where '<nr>' is a 32-bit unsigned integer */
Expand Down Expand Up @@ -248,21 +247,19 @@ static int fscrypt_setup(
return log_error_errno(r, "Failed to decode encrypted key of %s: %m", xa);

r = -ENOANO;
FOREACH_POINTER(list, cache->pkcs11_passwords, cache->fido2_passwords, password) {
char **list;
FOREACH_ARGUMENT(list, cache->pkcs11_passwords, cache->fido2_passwords, password) {
r = fscrypt_slot_try_many(
list,
salt, salt_size,
encrypted, encrypted_size,
setup->fscrypt_key_descriptor,
ret_volume_key, ret_volume_key_size);
if (r != -ENOANO)
break;
}
if (r < 0) {
if (r >= 0)
return 0;
if (r != -ENOANO)
return r;
} else
return 0;
}
}

return log_error_errno(SYNTHETIC_ERRNO(ENOKEY), "Failed to set up home directory with provided passwords.");
Expand Down
49 changes: 26 additions & 23 deletions src/home/homework-luks.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,6 @@ static int luks_setup(
_cleanup_(erase_and_freep) void *vk = NULL;
sd_id128_t p;
size_t vks;
char **list;
int r;

assert(h);
Expand Down Expand Up @@ -419,11 +418,13 @@ static int luks_setup(
return log_oom();

r = -ENOKEY;
FOREACH_POINTER(list,
cache ? cache->keyring_passswords : NULL,
cache ? cache->pkcs11_passwords : NULL,
cache ? cache->fido2_passwords : NULL,
passwords) {
char **list;
FOREACH_ARGUMENT(list,
cache ? cache->keyring_passswords : NULL,
cache ? cache->pkcs11_passwords : NULL,
cache ? cache->fido2_passwords : NULL,
passwords) {

r = luks_try_passwords(h, cd, list, vk, &vks, ret_key_serial ? &key_serial : NULL);
if (r != -ENOKEY)
break;
Expand Down Expand Up @@ -519,7 +520,6 @@ static int luks_open(

_cleanup_(erase_and_freep) void *vk = NULL;
sd_id128_t p;
char **list;
size_t vks;
int r;

Expand Down Expand Up @@ -560,11 +560,13 @@ static int luks_open(
return log_oom();

r = -ENOKEY;
FOREACH_POINTER(list,
cache ? cache->keyring_passswords : NULL,
cache ? cache->pkcs11_passwords : NULL,
cache ? cache->fido2_passwords : NULL,
h->password) {
char **list;
FOREACH_ARGUMENT(list,
cache ? cache->keyring_passswords : NULL,
cache ? cache->pkcs11_passwords : NULL,
cache ? cache->fido2_passwords : NULL,
h->password) {

r = luks_try_passwords(h, setup->crypt_device, list, vk, &vks, NULL);
if (r != -ENOKEY)
break;
Expand Down Expand Up @@ -3583,7 +3585,6 @@ int home_passwd_luks(
_cleanup_(erase_and_freep) void *volume_key = NULL;
struct crypt_pbkdf_type good_pbkdf, minimal_pbkdf;
const char *type;
char **list;
int r;

assert(h);
Expand Down Expand Up @@ -3613,11 +3614,12 @@ int home_passwd_luks(
return log_oom();

r = -ENOKEY;
FOREACH_POINTER(list,
cache ? cache->keyring_passswords : NULL,
cache ? cache->pkcs11_passwords : NULL,
cache ? cache->fido2_passwords : NULL,
h->password) {
char **list;
FOREACH_ARGUMENT(list,
cache ? cache->keyring_passswords : NULL,
cache ? cache->pkcs11_passwords : NULL,
cache ? cache->fido2_passwords : NULL,
h->password) {

r = luks_try_passwords(h, setup->crypt_device, list, volume_key, &volume_key_size, NULL);
if (r != -ENOKEY)
Expand Down Expand Up @@ -3736,7 +3738,6 @@ static int luks_try_resume(
}

int home_unlock_luks(UserRecord *h, HomeSetup *setup, const PasswordCache *cache) {
char **list;
int r;

assert(h);
Expand All @@ -3750,10 +3751,12 @@ int home_unlock_luks(UserRecord *h, HomeSetup *setup, const PasswordCache *cache
log_info("Discovered used LUKS device %s.", setup->dm_node);

r = -ENOKEY;
FOREACH_POINTER(list,
cache ? cache->pkcs11_passwords : NULL,
cache ? cache->fido2_passwords : NULL,
h->password) {
char **list;
FOREACH_ARGUMENT(list,
cache ? cache->pkcs11_passwords : NULL,
cache ? cache->fido2_passwords : NULL,
h->password) {

r = luks_try_resume(setup->crypt_device, setup->dm_name, list);
if (r != -ENOKEY)
break;
Expand Down
4 changes: 2 additions & 2 deletions src/import/pull-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,6 @@ int pull_verify(ImportVerify verify,
log_debug("Main download is a checksum file, can't validate its checksum with itself, skipping.");
verify_job = main_job;
} else {
PullJob *j;
assert(main_job->calc_checksum);
assert(main_job->checksum);
assert(checksum_job);
Expand All @@ -560,7 +559,8 @@ int pull_verify(ImportVerify verify,
return log_error_errno(SYNTHETIC_ERRNO(EBADMSG),
"Checksum is empty, cannot verify.");

FOREACH_POINTER(j, main_job, settings_job, roothash_job, roothash_signature_job, verity_job) {
PullJob *j;
FOREACH_ARGUMENT(j, main_job, settings_job, roothash_job, roothash_signature_job, verity_job) {
r = verify_one(checksum_job, j);
if (r < 0)
return r;
Expand Down
22 changes: 11 additions & 11 deletions src/import/pull-raw.c
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,6 @@ static int raw_pull_rename_auxiliary_file(

static void raw_pull_job_on_finished(PullJob *j) {
RawPull *i;
PullJob *jj;
int r;

assert(j);
Expand Down Expand Up @@ -568,8 +567,9 @@ static void raw_pull_job_on_finished(PullJob *j) {
}
}

PullJob *jj;
/* Let's close these auxiliary files now, we don't need access to them anymore. */
FOREACH_POINTER(jj, i->settings_job, i->roothash_job, i->roothash_signature_job, i->verity_job)
FOREACH_ARGUMENT(jj, i->settings_job, i->roothash_job, i->roothash_signature_job, i->verity_job)
pull_job_close_disk_fd(jj);

if (!i->raw_job->etag_exists) {
Expand Down Expand Up @@ -820,7 +820,6 @@ int raw_pull_start(
ImportVerify verify,
const char *checksum) {

PullJob *j;
int r;

assert(i);
Expand Down Expand Up @@ -959,14 +958,15 @@ int raw_pull_start(
return r;
}

FOREACH_POINTER(j,
i->raw_job,
i->checksum_job,
i->signature_job,
i->settings_job,
i->roothash_job,
i->roothash_signature_job,
i->verity_job) {
PullJob *j;
FOREACH_ARGUMENT(j,
i->raw_job,
i->checksum_job,
i->signature_job,
i->settings_job,
i->roothash_job,
i->roothash_signature_job,
i->verity_job) {

if (!j)
continue;
Expand Down
12 changes: 6 additions & 6 deletions src/import/pull-tar.c
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,6 @@ int tar_pull_start(
ImportVerify verify,
const char *checksum) {

PullJob *j;
int r;

assert(i);
Expand Down Expand Up @@ -656,11 +655,12 @@ int tar_pull_start(
return r;
}

FOREACH_POINTER(j,
i->tar_job,
i->checksum_job,
i->signature_job,
i->settings_job) {
PullJob *j;
FOREACH_ARGUMENT(j,
i->tar_job,
i->checksum_job,
i->signature_job,
i->settings_job) {

if (!j)
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/network/wait-online/manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ static int manager_link_is_online(Manager *m, Link *l, const LinkOperationalStat
l->state);

const LinkOperationalStateRange *range;
FOREACH_POINTER(range, state_range, &m->required_operstate, &l->required_operstate)
FOREACH_ARGUMENT(range, state_range, &m->required_operstate, &l->required_operstate)
if (operational_state_range_is_valid(range))
break;
assert(range != POINTER_MAX);
Expand Down
4 changes: 2 additions & 2 deletions src/shared/condition.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ static int condition_test_kernel_command_line(Condition *c, char **env) {
}

static int condition_test_credential(Condition *c, char **env) {
int (*gd)(const char **ret);
int r;

assert(c);
Expand All @@ -155,7 +154,8 @@ static int condition_test_credential(Condition *c, char **env) {
if (!credential_name_valid(c->parameter)) /* credentials with invalid names do not exist */
return false;

FOREACH_POINTER(gd, get_credentials_dir, get_encrypted_credentials_dir) {
int (*gd)(const char **ret);
FOREACH_ARGUMENT(gd, get_credentials_dir, get_encrypted_credentials_dir) {
_cleanup_free_ char *j = NULL;
const char *cd;

Expand Down
79 changes: 0 additions & 79 deletions src/test/test-macro.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,85 +221,6 @@ TEST(IN_SET) {
assert_se(!IN_SET(t.x, 2, 3, 4));
}

TEST(FOREACH_POINTER) {
int a, b, c, *i;
size_t k = 0;

FOREACH_POINTER(i, &a, &b, &c) {
switch (k) {

case 0:
assert_se(i == &a);
break;

case 1:
assert_se(i == &b);
break;

case 2:
assert_se(i == &c);
break;

default:
assert_not_reached();
break;
}

k++;
}

assert_se(k == 3);

FOREACH_POINTER(i, &b) {
assert_se(k == 3);
assert_se(i == &b);
k = 4;
}

assert_se(k == 4);

FOREACH_POINTER(i, NULL, &c, NULL, &b, NULL, &a, NULL) {
switch (k) {

case 4:
assert_se(i == NULL);
break;

case 5:
assert_se(i == &c);
break;

case 6:
assert_se(i == NULL);
break;

case 7:
assert_se(i == &b);
break;

case 8:
assert_se(i == NULL);
break;

case 9:
assert_se(i == &a);
break;

case 10:
assert_se(i == NULL);
break;

default:
assert_not_reached();
break;
}

k++;
}

assert_se(k == 11);
}

TEST(FOREACH_ARGUMENT) {
size_t i;

Expand Down

0 comments on commit 2d70878

Please sign in to comment.