nulstr-util: Declare NULSTR_FOREACH() iterator inline

This commit is contained in:
Daan De Meyer 2022-11-11 12:08:26 +01:00
parent 5ea173a91b
commit 12e2b70f9b
44 changed files with 26 additions and 78 deletions

View file

@ -23,8 +23,6 @@ int verb_cat_config(int argc, char *argv[], void *userdata) {
print_separator();
if (path_is_absolute(*arg)) {
const char *dir;
NULSTR_FOREACH(dir, CONF_PATHS_NULSTR("")) {
t = path_startswith(*arg, dir);
if (t)

View file

@ -50,8 +50,6 @@ static int load_available_kernel_filesystems(Set **ret) {
}
static void filesystem_set_remove(Set *s, const FilesystemSet *set) {
const char *filesystem;
NULSTR_FOREACH(filesystem, set->value) {
if (filesystem[0] == '@')
continue;
@ -61,7 +59,6 @@ static void filesystem_set_remove(Set *s, const FilesystemSet *set) {
}
static void dump_filesystem_set(const FilesystemSet *set) {
const char *filesystem;
int r;
if (!set)
@ -119,7 +116,6 @@ int verb_filesystems(int argc, char *argv[], void *userdata) {
if (strv_isempty(strv_skip(argv, 1))) {
_cleanup_set_free_ Set *kernel = NULL, *known = NULL;
const char *fs;
int k;
NULSTR_FOREACH(fs, filesystem_sets[FILESYSTEM_SET_KNOWN].value)

View file

@ -567,8 +567,6 @@ static int assess_system_call_architectures(
}
static bool syscall_names_in_filter(Set *s, bool allow_list, const SyscallFilterSet *f, const char **ret_offending_syscall) {
const char *syscall;
NULSTR_FOREACH(syscall, f->value) {
if (syscall[0] == '@') {
const SyscallFilterSet *g;

View file

@ -59,8 +59,6 @@ static int load_kernel_syscalls(Set **ret) {
}
static void syscall_set_remove(Set *s, const SyscallFilterSet *set) {
const char *syscall;
if (!set)
return;
@ -73,8 +71,6 @@ static void syscall_set_remove(Set *s, const SyscallFilterSet *set) {
}
static void dump_syscall_filter(const SyscallFilterSet *set) {
const char *syscall;
printf("%s%s%s\n"
" # %s\n",
ansi_highlight(),
@ -93,7 +89,6 @@ int verb_syscall_filters(int argc, char *argv[], void *userdata) {
if (strv_isempty(strv_skip(argv, 1))) {
_cleanup_set_free_ Set *kernel = NULL, *known = NULL;
const char *sys;
int k = 0; /* explicit initialization to appease gcc */
NULSTR_FOREACH(sys, syscall_filter_sets[SYSCALL_FILTER_SET_KNOWN].value)

View file

@ -28,7 +28,6 @@ int fs_type_from_string(const char *name, const statfs_f_type_t **ret) {
}
bool fs_in_group(const struct statfs *s, FilesystemGroups fs_group) {
const char *fs;
int r;
NULSTR_FOREACH(fs, filesystem_sets[fs_group].value) {

View file

@ -4,8 +4,6 @@
#include "string-util.h"
const char* nulstr_get(const char *nulstr, const char *needle) {
const char *i;
if (!nulstr)
return NULL;

View file

@ -5,10 +5,10 @@
#include <string.h>
#define NULSTR_FOREACH(i, l) \
for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
for (typeof(*(l)) *(i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
#define NULSTR_FOREACH_PAIR(i, j, l) \
for ((i) = (l), (j) = strchr((i), 0)+1; (i) && *(i); (i) = strchr((j), 0)+1, (j) = *(i) ? strchr((i), 0)+1 : (i))
for (typeof(*(l)) *(i) = (l), *(j) = strchr((i), 0)+1; (i) && *(i); (i) = strchr((j), 0)+1, (j) = *(i) ? strchr((i), 0)+1 : (i))
const char* nulstr_get(const char *nulstr, const char *needle);

View file

@ -880,7 +880,7 @@ char **env_generator_binary_paths(bool is_system) {
}
int find_portable_profile(const char *name, const char *unit, char **ret_path) {
const char *p, *dot;
const char *dot;
assert(name);
assert(ret_path);

View file

@ -679,7 +679,6 @@ char** strv_parse_nulstr(const char *s, size_t l) {
}
char** strv_split_nulstr(const char *s) {
const char *i;
char **r = NULL;
NULSTR_FOREACH(i, s)

View file

@ -515,7 +515,6 @@ int bpf_devices_allow_list_static(
"/run/systemd/inaccessible/blk\0" "rwm\0";
int r = 0, k;
const char *node, *acc;
NULSTR_FOREACH_PAIR(node, acc, auto_devices) {
k = bpf_devices_allow_list_device(prog, path, node, acc);
if (r >= 0 && k < 0)

View file

@ -326,7 +326,6 @@ int lsm_bpf_parse_filesystem(
if (name[0] == '@') {
const FilesystemSet *set;
const char *i;
set = filesystem_set_find(name);
if (!set) {

View file

@ -6226,7 +6226,6 @@ void unit_dump_config_items(FILE *f) {
};
const char *prev = NULL;
const char *i;
assert(f);

View file

@ -916,7 +916,7 @@ static int mount_private_dev(MountEntry *m) {
"/dev/tty\0";
char temporary_mount[] = "/tmp/namespace-dev-XXXXXX";
const char *d, *dev = NULL, *devpts = NULL, *devshm = NULL, *devhugepages = NULL, *devmqueue = NULL, *devlog = NULL, *devptmx = NULL;
const char *dev = NULL, *devpts = NULL, *devshm = NULL, *devhugepages = NULL, *devmqueue = NULL, *devlog = NULL, *devptmx = NULL;
bool can_mknod = true;
int r;

View file

@ -494,7 +494,7 @@ static char* disk_description(const char *path) {
"ID_MODEL\0";
_cleanup_(sd_device_unrefp) sd_device *device = NULL;
const char *i, *name;
const char *name;
struct stat st;
assert(path);

View file

@ -371,7 +371,6 @@ static int enumerate_dir(
static int should_skip_path(const char *prefix, const char *suffix) {
#if HAVE_SPLIT_USR
_cleanup_free_ char *target = NULL, *dirname = NULL;
const char *p;
dirname = path_join(prefix, suffix);
if (!dirname)
@ -400,7 +399,6 @@ static int should_skip_path(const char *prefix, const char *suffix) {
}
static int process_suffix(const char *suffix, const char *onlyprefix) {
const char *p;
char *f, *key;
OrderedHashmap *top, *bottom, *drops, *h;
int r = 0, k, n_found = 0;
@ -477,7 +475,6 @@ finish:
}
static int process_suffixes(const char *onlyprefix) {
const char *n;
int n_found = 0, r;
NULSTR_FOREACH(n, suffixes) {
@ -492,8 +489,6 @@ static int process_suffixes(const char *onlyprefix) {
}
static int process_suffix_chop(const char *arg) {
const char *p;
assert(arg);
if (!path_is_absolute(arg))

View file

@ -216,7 +216,6 @@ static int fscrypt_setup(
size_t *ret_volume_key_size) {
_cleanup_free_ char *xattr_buf = NULL;
const char *xa;
int r;
assert(setup);
@ -646,7 +645,6 @@ int home_passwd_fscrypt(
_cleanup_free_ char *xattr_buf = NULL;
size_t volume_key_size = 0;
uint32_t slot = 0;
const char *xa;
int r;
assert(h);

View file

@ -1512,7 +1512,6 @@ static int get_possible_units(
Set **units) {
_cleanup_set_free_free_ Set *found = NULL;
const char *field;
int r;
found = set_new(&string_hash_ops);

View file

@ -559,7 +559,6 @@ static int device_update_properties_bufs(sd_device *device) {
return -ENOMEM;
size_t i = 0;
char *p;
NULSTR_FOREACH(p, buf_nulstr)
buf_strv[i++] = p;
assert(i == num);

View file

@ -534,7 +534,7 @@ TEST(sd_device_new_from_nulstr) {
_cleanup_(sd_device_unrefp) sd_device *device = NULL, *from_nulstr = NULL;
_cleanup_free_ char *nulstr_copy = NULL;
const char *devlink, *nulstr;
const char *nulstr;
size_t len;
assert_se(sd_device_new_from_syspath(&device, "/sys/class/net/lo") >= 0);

View file

@ -300,13 +300,15 @@ static int hwdb_new(const char *path, sd_hwdb **ret) {
if (!hwdb->f)
return log_debug_errno(errno, "Failed to open %s: %m", path);
} else {
NULSTR_FOREACH(path, hwdb_bin_paths) {
log_debug("Trying to open \"%s\"...", path);
hwdb->f = fopen(path, "re");
if (hwdb->f)
NULSTR_FOREACH(p, hwdb_bin_paths) {
log_debug("Trying to open \"%s\"...", p);
hwdb->f = fopen(p, "re");
if (hwdb->f) {
path = p;
break;
}
if (errno != ENOENT)
return log_debug_errno(errno, "Failed to open %s: %m", path);
return log_debug_errno(errno, "Failed to open %s: %m", p);
}
if (!hwdb->f)

View file

@ -1828,7 +1828,6 @@ static int add_search_paths(sd_journal *j) {
static const char search_paths[] =
"/run/log/journal\0"
"/var/log/journal\0";
const char *p;
assert(j);

View file

@ -432,7 +432,6 @@ int vconsole_convert_to_x11(Context *c) {
}
int find_converted_keymap(const char *x11_layout, const char *x11_variant, char **new_keymap) {
const char *dir;
_cleanup_free_ char *n = NULL;
if (x11_variant)

View file

@ -98,7 +98,7 @@ static OutputFlags get_output_flags(void) {
static int call_get_os_release(sd_bus *bus, const char *method, const char *name, const char *query, ...) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
const char *k, *v, *iter, **query_res = NULL;
const char *k, *v, **query_res = NULL;
size_t count = 0, awaited_args = 0;
va_list ap;
int r;

View file

@ -2219,7 +2219,6 @@ static int copy_devnodes(const char *dest) {
"tty\0"
"net/tun\0";
const char *d;
int r = 0;
assert(dest);

View file

@ -165,7 +165,6 @@ static int dns_trust_anchor_add_builtin_negative(DnsTrustAnchor *d) {
/* Defined by RFC 8375. The most official choice. */
"home.arpa\0";
const char *name;
int r;
assert(d);

View file

@ -135,7 +135,7 @@ int base_filesystem_create(const char *root, uid_t uid, gid_t gid) {
continue;
if (table[i].target) {
const char *target = NULL, *s;
const char *target = NULL;
/* check if one of the targets exists */
NULSTR_FOREACH(s, table[i].target) {

View file

@ -211,7 +211,6 @@ static int show_cgroup_name(
if (FLAGS_SET(flags, OUTPUT_CGROUP_XATTRS) && fd >= 0) {
_cleanup_free_ char *nl = NULL;
char *xa;
r = flistxattr_malloc(fd, &nl);
if (r < 0)

View file

@ -211,7 +211,6 @@ static int parse_line(
if (sections && !nulstr_contains(sections, n)) {
bool ignore;
const char *t;
ignore = (flags & CONFIG_PARSE_RELAXED) || startswith(n, "X-");

View file

@ -1555,7 +1555,6 @@ int copy_rights_with_fallback(int fdf, int fdt, const char *patht) {
int copy_xattr(int fdf, int fdt, CopyFlags copy_flags) {
_cleanup_free_ char *names = NULL;
int ret = 0, r;
const char *p;
r = flistxattr_malloc(fdf, &names);
if (r < 0)

View file

@ -22,7 +22,6 @@ int dev_setup(const char *prefix, uid_t uid, gid_t gid) {
"/proc/self/fd/1\0" "/dev/stdout\0"
"/proc/self/fd/2\0" "/dev/stderr\0";
const char *j, *k;
int r;
NULSTR_FOREACH_PAIR(j, k, symlinks) {

View file

@ -429,7 +429,6 @@ int image_find(ImageClass class,
const char *root,
Image **ret) {
const char *path;
int r;
assert(class >= 0);
@ -533,7 +532,6 @@ int image_discover(
const char *root,
Hashmap *h) {
const char *path;
int r;
assert(class >= 0);
@ -1262,8 +1260,6 @@ bool image_in_search_path(
const char *root,
const char *image) {
const char *path;
assert(image);
NULSTR_FOREACH(path, image_search_path[class]) {

View file

@ -2869,7 +2869,6 @@ int dissected_image_acquire_metadata(DissectedImage *m, DissectImageFlags extra_
for (unsigned k = 0; k < _META_MAX; k++) {
_cleanup_close_ int fd = -ENOENT;
const char *p;
if (!paths[k])
continue;

View file

@ -88,7 +88,7 @@ int fstab_filter_options(
char ***ret_values,
char **ret_filtered) {
const char *name, *namefound = NULL, *x;
const char *namefound = NULL, *x;
_cleanup_strv_free_ char **stor = NULL, **values = NULL;
_cleanup_free_ char *value = NULL, **filtered = NULL;
int r;
@ -127,17 +127,17 @@ int fstab_filter_options(
if (!x)
continue;
/* Match name, but when ret_values, only when followed by assignment. */
if (*x == '=' || (!ret_values && *x == '\0'))
if (*x == '=' || (!ret_values && *x == '\0')) {
/* Keep the last occurrence found */
namefound = name;
goto found;
}
}
*t = *s;
t++;
continue;
found:
/* Keep the last occurrence found */
namefound = name;
if (ret_value || ret_values) {
assert(IN_SET(*x, '=', '\0'));

View file

@ -652,7 +652,7 @@ int hwdb_update(const char *root, const char *hwdb_bin_dir, bool strict, bool co
int hwdb_query(const char *modalias, const char *root) {
_cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL;
const char *key, *value, *p;
const char *key, *value;
int r;
assert(modalias);
@ -682,7 +682,6 @@ int hwdb_query(const char *modalias, const char *root) {
bool hwdb_should_reload(sd_hwdb *hwdb) {
bool found = false;
const char* p;
struct stat st;
if (!hwdb)

View file

@ -179,7 +179,6 @@ int raw_strip_suffixes(const char *p, char **ret) {
return -ENOMEM;
for (;;) {
const char *sfx;
bool changed = false;
NULSTR_FOREACH(sfx, suffixes) {

View file

@ -71,7 +71,6 @@ int get_keymaps(char ***ret) {
if (!keymaps)
return -ENOMEM;
const char *dir;
NULSTR_FOREACH(dir, KBD_KEYMAP_DIRS) {
r = recurse_dir_at(
AT_FDCWD,
@ -135,7 +134,6 @@ int keymap_exists(const char *name) {
if (!keymap_is_valid(name))
return -EINVAL;
const char *dir;
NULSTR_FOREACH(dir, KBD_KEYMAP_DIRS) {
r = recurse_dir_at(
AT_FDCWD,

View file

@ -1039,7 +1039,6 @@ static int add_syscall_filter_set(
bool log_missing,
char ***added) {
const char *sys;
int r;
/* Any syscalls that are handled are added to the *added strv. It needs to be initialized. */
@ -1169,7 +1168,6 @@ int seccomp_parse_syscall_filter(
if (name[0] == '@') {
const SyscallFilterSet *set;
const char *i;
set = syscall_filter_set_find(name);
if (!set) {
@ -1909,7 +1907,6 @@ int parse_syscall_archs(char **l, Set **ret_archs) {
}
int seccomp_filter_set_add(Hashmap *filter, bool add, const SyscallFilterSet *set) {
const char *i;
int r;
assert(set);
@ -2308,7 +2305,6 @@ int seccomp_suppress_sync(void) {
SECCOMP_FOREACH_LOCAL_ARCH(arch) {
_cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL;
const char *c;
r = seccomp_init_for_arch(&seccomp, arch, SCMP_ACT_ALLOW);
if (r < 0)

View file

@ -158,7 +158,6 @@ int main(int argc, char *argv[]) {
else
arg_start = getpid_cached();
const char *i;
NULSTR_FOREACH(i, "zeros\0simple\0random\0") {
#if HAVE_XZ
test_compress_decompress("XZ", i, compress_blob_xz, decompress_blob_xz);

View file

@ -800,7 +800,6 @@ TEST(print_securities) {
TEST(condition_test_virtualization) {
Condition *condition;
const char *virt;
int r;
condition = condition_new(CONDITION_VIRTUALIZATION, "garbage oifdsjfoidsjoj", false, false);

View file

@ -452,8 +452,8 @@ TEST(hashmap_foreach_key) {
m = hashmap_new(&string_hash_ops);
NULSTR_FOREACH(key, key_table)
hashmap_put(m, key, (void*) (const char*) "my dummy val");
NULSTR_FOREACH(k, key_table)
hashmap_put(m, k, (void*) (const char*) "my dummy val");
HASHMAP_FOREACH_KEY(s, key, m) {
assert_se(s);

View file

@ -56,7 +56,6 @@ TEST(basic_enumerate) {
TEST(sd_hwdb_new_from_path) {
_cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL;
const char *hwdb_bin_path = NULL;
int r;
assert_se(sd_hwdb_new_from_path(NULL, &hwdb) == -EINVAL);

View file

@ -117,7 +117,7 @@ TEST(seccomp_arch_to_string) {
}
TEST(architecture_table) {
const char *n, *n2;
const char *n2;
NULSTR_FOREACH(n,
"native\0"
@ -234,7 +234,7 @@ TEST(filter_sets_ordered) {
assert_se(streq(syscall_filter_sets[SYSCALL_FILTER_SET_KNOWN].name, "@known"));
for (size_t i = 0; i < _SYSCALL_FILTER_SET_MAX; i++) {
const char *k, *p = NULL;
const char *p = NULL;
/* Make sure each group has a description */
assert_se(!isempty(syscall_filter_sets[0].help));

View file

@ -948,7 +948,6 @@ TEST(strv_extend_n) {
static void test_strv_make_nulstr_one(char **l) {
_cleanup_free_ char *b = NULL, *c = NULL;
_cleanup_strv_free_ char **q = NULL;
const char *s = NULL;
size_t n, m;
unsigned i = 0;

View file

@ -1341,7 +1341,7 @@ bool udev_rules_should_reload(UdevRules *rules) {
}
static bool token_match_string(UdevRuleToken *token, const char *str) {
const char *i, *value;
const char *value;
bool match = false;
assert(token);