strv: wrap strv_new() in a macro so that NULL sentinel is implicit

This commit is contained in:
Lennart Poettering 2018-10-31 17:03:50 +01:00
parent fc03c53653
commit bea1a01310
21 changed files with 76 additions and 83 deletions

View File

@ -169,7 +169,7 @@ char **strv_new_ap(const char *x, va_list ap) {
return TAKE_PTR(a);
}
char **strv_new(const char *x, ...) {
char **strv_new_internal(const char *x, ...) {
char **r;
va_list ap;
@ -658,7 +658,7 @@ char **strv_split_nulstr(const char *s) {
}
if (!r)
return strv_new(NULL, NULL);
return strv_new(NULL);
return r;
}

View File

@ -54,8 +54,9 @@ bool strv_equal(char **a, char **b);
#define strv_contains(l, s) (!!strv_find((l), (s)))
char **strv_new(const char *x, ...) _sentinel_;
char **strv_new_internal(const char *x, ...) _sentinel_;
char **strv_new_ap(const char *x, va_list ap);
#define strv_new(...) strv_new_internal(__VA_ARGS__, NULL)
#define STRV_IGNORE ((const char *) -1)

View File

@ -1190,7 +1190,7 @@ int get_timezones(char ***ret) {
assert(ret);
zones = strv_new("UTC", NULL);
zones = strv_new("UTC");
if (!zones)
return -ENOMEM;

View File

@ -1225,7 +1225,7 @@ static int service_collect_fds(
return -ENOMEM;
rfds[0] = s->socket_fd;
rfd_names = strv_new("connection", NULL);
rfd_names = strv_new("connection");
if (!rfd_names)
return -ENOMEM;

View File

@ -124,7 +124,7 @@ static int slice_load_root_slice(Unit *u) {
if (!u->description)
u->description = strdup("Root Slice");
if (!u->documentation)
u->documentation = strv_new("man:systemd.special(7)", NULL);
u->documentation = strv_new("man:systemd.special(7)");
return 1;
}
@ -147,7 +147,7 @@ static int slice_load_system_slice(Unit *u) {
if (!u->description)
u->description = strdup("System Slice");
if (!u->documentation)
u->documentation = strv_new("man:systemd.special(7)", NULL);
u->documentation = strv_new("man:systemd.special(7)");
return 1;
}

View File

@ -207,7 +207,7 @@ static const sd_bus_vtable vtable2[] = {
static int enumerator_callback(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
if (object_path_startswith("/value", path))
assert_se(*nodes = strv_new("/value/a", "/value/b", "/value/c", NULL));
assert_se(*nodes = strv_new("/value/a", "/value/b", "/value/c"));
return 1;
}
@ -215,7 +215,7 @@ static int enumerator_callback(sd_bus *bus, const char *path, void *userdata, ch
static int enumerator2_callback(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
if (object_path_startswith("/value/a", path))
assert_se(*nodes = strv_new("/value/a/x", "/value/a/y", "/value/a/z", NULL));
assert_se(*nodes = strv_new("/value/a/x", "/value/a/y", "/value/a/z"));
return 1;
}

View File

@ -605,7 +605,7 @@ int bus_machine_method_open_shell(sd_bus_message *message, void *userdata, sd_bu
if (strv_isempty(args)) {
args = strv_free(args);
args = strv_new(path, NULL);
args = strv_new(path);
if (!args)
return -ENOMEM;
}

View File

@ -678,7 +678,7 @@ int ask_password_agent(
if (passphrase[0] == '+') {
/* An empty message refers to the empty password */
if (n == 1)
l = strv_new("", NULL);
l = strv_new("");
else
l = strv_parse_nulstr(passphrase+1, n-1);
explicit_bzero_safe(passphrase, n);

View File

@ -1113,7 +1113,7 @@ int config_parse_join_controllers(
controllers = new(char**, 2);
if (!controllers)
return log_oom();
controllers[0] = strv_new(NULL, NULL);
controllers[0] = strv_new(NULL);
if (!controllers[0])
return log_oom();
controllers[1] = NULL;

View File

@ -2446,7 +2446,7 @@ int unit_file_add_dependency(
l = &i->required_by;
strv_free(*l);
*l = strv_new(target_info->name, NULL);
*l = strv_new(target_info->name);
if (!*l)
return -ENOMEM;
}

View File

@ -137,8 +137,7 @@ int xdg_user_dirs(char ***ret_config_dirs, char ***ret_data_dirs) {
data_dirs = strv_split(e, ":");
else
data_dirs = strv_new("/usr/local/share",
"/usr/share",
NULL);
"/usr/share");
if (!data_dirs)
return -ENOMEM;
@ -616,8 +615,7 @@ int lookup_paths_init(
SYSTEM_DATA_UNIT_PATH,
"/usr/lib/systemd/system",
STRV_IFNOTNULL(flags & LOOKUP_PATHS_SPLIT_USR ? "/lib/systemd/system" : NULL),
STRV_IFNOTNULL(generator_late),
NULL);
STRV_IFNOTNULL(generator_late));
break;
case UNIT_FILE_GLOBAL:
@ -640,8 +638,7 @@ int lookup_paths_init(
"/usr/local/lib/systemd/user",
USER_DATA_UNIT_PATH,
"/usr/lib/systemd/user",
STRV_IFNOTNULL(generator_late),
NULL);
STRV_IFNOTNULL(generator_late));
break;
case UNIT_FILE_USER:
@ -891,16 +888,14 @@ char **generator_binary_paths(UnitFileScope scope) {
return strv_new("/run/systemd/system-generators",
"/etc/systemd/system-generators",
"/usr/local/lib/systemd/system-generators",
SYSTEM_GENERATOR_PATH,
NULL);
SYSTEM_GENERATOR_PATH);
case UNIT_FILE_GLOBAL:
case UNIT_FILE_USER:
return strv_new("/run/systemd/user-generators",
"/etc/systemd/user-generators",
"/usr/local/lib/systemd/user-generators",
USER_GENERATOR_PATH,
NULL);
USER_GENERATOR_PATH);
default:
assert_not_reached("Hmm, unexpected scope.");

View File

@ -73,7 +73,7 @@ int parse_sleep_config(const char *verb, bool *ret_allow, char ***ret_modes, cha
if (suspend_state)
states = TAKE_PTR(suspend_state);
else
states = strv_new("mem", "standby", "freeze", NULL);
states = strv_new("mem", "standby", "freeze");
} else if (streq(verb, "hibernate")) {
allow = allow_hibernate != 0;
@ -81,12 +81,12 @@ int parse_sleep_config(const char *verb, bool *ret_allow, char ***ret_modes, cha
if (hibernate_mode)
modes = TAKE_PTR(hibernate_mode);
else
modes = strv_new("platform", "shutdown", NULL);
modes = strv_new("platform", "shutdown");
if (hibernate_state)
states = TAKE_PTR(hibernate_state);
else
states = strv_new("disk", NULL);
states = strv_new("disk");
} else if (streq(verb, "hybrid-sleep")) {
allow = allow_hybrid_sleep > 0 ||
@ -95,12 +95,12 @@ int parse_sleep_config(const char *verb, bool *ret_allow, char ***ret_modes, cha
if (hybrid_mode)
modes = TAKE_PTR(hybrid_mode);
else
modes = strv_new("suspend", "platform", "shutdown", NULL);
modes = strv_new("suspend", "platform", "shutdown");
if (hybrid_state)
states = TAKE_PTR(hybrid_state);
else
states = strv_new("disk", NULL);
states = strv_new("disk");
} else if (streq(verb, "suspend-then-hibernate")) {
allow = allow_s2h > 0 ||

View File

@ -3071,7 +3071,7 @@ static int start_unit(int argc, char *argv[], void *userdata) {
}
if (one_name) {
names = strv_new(one_name, NULL);
names = strv_new(one_name);
if (!names)
return log_oom();
} else {

View File

@ -717,7 +717,7 @@ static int acquire_search_path(const char *def, const char *envvar, char ***ret)
if (strv_isempty(l)) {
strv_free(l);
l = strv_new(def, NULL);
l = strv_new(def);
if (!l)
return log_oom();
}

View File

@ -15,13 +15,13 @@
static void test_strv_env_delete(void) {
_cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL, **d = NULL;
a = strv_new("FOO=BAR", "WALDO=WALDO", "WALDO=", "PIEP", "SCHLUMPF=SMURF", NULL);
a = strv_new("FOO=BAR", "WALDO=WALDO", "WALDO=", "PIEP", "SCHLUMPF=SMURF");
assert_se(a);
b = strv_new("PIEP", "FOO", NULL);
b = strv_new("PIEP", "FOO");
assert_se(b);
c = strv_new("SCHLUMPF", NULL);
c = strv_new("SCHLUMPF");
assert_se(c);
d = strv_env_delete(a, 2, b, c);
@ -45,7 +45,7 @@ static void test_strv_env_get(void) {
static void test_strv_env_unset(void) {
_cleanup_strv_free_ char **l = NULL;
l = strv_new("PIEP", "SCHLUMPF=SMURFF", "NANANANA=YES", NULL);
l = strv_new("PIEP", "SCHLUMPF=SMURFF", "NANANANA=YES");
assert_se(l);
assert_se(strv_env_unset(l, "SCHLUMPF") == l);
@ -58,7 +58,7 @@ static void test_strv_env_unset(void) {
static void test_strv_env_set(void) {
_cleanup_strv_free_ char **l = NULL, **r = NULL;
l = strv_new("PIEP", "SCHLUMPF=SMURFF", "NANANANA=YES", NULL);
l = strv_new("PIEP", "SCHLUMPF=SMURFF", "NANANANA=YES");
assert_se(l);
r = strv_env_set(l, "WALDO=WALDO");
@ -74,10 +74,10 @@ static void test_strv_env_set(void) {
static void test_strv_env_merge(void) {
_cleanup_strv_free_ char **a = NULL, **b = NULL, **r = NULL;
a = strv_new("FOO=BAR", "WALDO=WALDO", "WALDO=", "PIEP", "SCHLUMPF=SMURF", NULL);
a = strv_new("FOO=BAR", "WALDO=WALDO", "WALDO=", "PIEP", "SCHLUMPF=SMURF");
assert_se(a);
b = strv_new("FOO=KKK", "FOO=", "PIEP=", "SCHLUMPF=SMURFF", "NANANANA=YES", NULL);
b = strv_new("FOO=KKK", "FOO=", "PIEP=", "SCHLUMPF=SMURFF", "NANANANA=YES");
assert_se(b);
r = strv_env_merge(2, a, b);
@ -250,8 +250,7 @@ static void test_env_clean(void) {
"xyz\n=xyz",
"xyz=xyz\n",
"another=one",
"another=final one",
NULL);
"another=final one");
assert_se(e);
assert_se(!strv_env_is_valid(e));
assert_se(strv_env_clean(e) == e);

View File

@ -346,7 +346,8 @@ static void test_environment_gathering(void) {
/* now retest with "default" path passed in, as created by
* manager_default_environment */
env = strv_free(env);
env = strv_new("PATH=" DEFAULT_PATH, NULL);
env = strv_new("PATH=" DEFAULT_PATH);
assert_se(env);
r = execute_directories(dirs, DEFAULT_TIMEOUT_USEC, gather_environment, args, NULL, env);
assert_se(r >= 0);

View File

@ -424,7 +424,7 @@ static int parse_argv(int argc, char **argv,
size_t n_allocated = 0;
if (argc > 1)
modules = strv_new(argv[1], NULL);
modules = strv_new(argv[1]);
else
modules = strv_new(
#if ENABLE_NSS_MYHOSTNAME
@ -436,8 +436,7 @@ static int parse_argv(int argc, char **argv,
#if ENABLE_NSS_MYMACHINES
"mymachines",
#endif
"dns",
NULL);
"dns");
if (!modules)
return -ENOMEM;
@ -468,7 +467,7 @@ static int parse_argv(int argc, char **argv,
if (!hostname)
return -ENOMEM;
names = strv_new("localhost", "_gateway", "foo_no_such_host", hostname, NULL);
names = strv_new("localhost", "_gateway", "foo_no_such_host", hostname);
if (!names)
return -ENOMEM;

View File

@ -293,7 +293,7 @@ static void test_strv_resolve(void) {
assert_se(mkdtemp(tmp_dir) != NULL);
search_dirs = strv_new("/dir1", "/dir2", "/dir3", NULL);
search_dirs = strv_new("/dir1", "/dir2", "/dir3");
assert_se(search_dirs);
STRV_FOREACH(d, search_dirs) {
char *p = strappend(tmp_dir, *d);

View File

@ -138,7 +138,7 @@ static void test_deserialize_environment(void) {
log_info("/* %s */", __func__);
assert_se(env = strv_new("A=1", NULL));
assert_se(env = strv_new("A=1"));
assert_se(deserialize_environment("B=2", &env) >= 0);
assert_se(deserialize_environment("FOO%%=a\\177b\\nc\\td e", &env) >= 0);
@ -162,8 +162,7 @@ static void test_serialize_environment(void) {
"B=2",
"C=ąęółń",
"D=D=a\\x0Ab",
"FOO%%=a\177b\nc\td e",
NULL));
"FOO%%=a\177b\nc\td e"));
assert_se(serialize_strv(f, "env", env) == 1);
assert_se(fflush_and_check(f) == 0);

View File

@ -50,14 +50,14 @@ static int test_fiemap(const char *path) {
static void test_sleep(void) {
_cleanup_strv_free_ char
**standby = strv_new("standby", NULL),
**mem = strv_new("mem", NULL),
**disk = strv_new("disk", NULL),
**suspend = strv_new("suspend", NULL),
**reboot = strv_new("reboot", NULL),
**platform = strv_new("platform", NULL),
**shutdown = strv_new("shutdown", NULL),
**freez = strv_new("freeze", NULL);
**standby = strv_new("standby"),
**mem = strv_new("mem"),
**disk = strv_new("disk"),
**suspend = strv_new("suspend"),
**reboot = strv_new("reboot"),
**platform = strv_new("platform"),
**shutdown = strv_new("shutdown"),
**freez = strv_new("freeze");
int r;
log_info("/* %s */", __func__);

View File

@ -441,8 +441,8 @@ static void test_strv_sort(void) {
static void test_strv_extend_strv_concat(void) {
_cleanup_strv_free_ char **a = NULL, **b = NULL;
a = strv_new("without", "suffix", NULL);
b = strv_new("with", "suffix", NULL);
a = strv_new("without", "suffix");
b = strv_new("with", "suffix");
assert_se(a);
assert_se(b);
@ -457,8 +457,8 @@ static void test_strv_extend_strv_concat(void) {
static void test_strv_extend_strv(void) {
_cleanup_strv_free_ char **a = NULL, **b = NULL, **n = NULL;
a = strv_new("abc", "def", "ghi", NULL);
b = strv_new("jkl", "mno", "abc", "pqr", NULL);
a = strv_new("abc", "def", "ghi");
b = strv_new("jkl", "mno", "abc", "pqr");
assert_se(a);
assert_se(b);
@ -483,7 +483,7 @@ static void test_strv_extend_strv(void) {
static void test_strv_extend(void) {
_cleanup_strv_free_ char **a = NULL, **b = NULL;
a = strv_new("test", "test1", NULL);
a = strv_new("test", "test1");
assert_se(a);
assert_se(strv_extend(&a, "test2") >= 0);
assert_se(strv_extend(&b, "test3") >= 0);
@ -497,7 +497,7 @@ static void test_strv_extend(void) {
static void test_strv_extendf(void) {
_cleanup_strv_free_ char **a = NULL, **b = NULL;
a = strv_new("test", "test1", NULL);
a = strv_new("test", "test1");
assert_se(a);
assert_se(strv_extendf(&a, "test2 %s %d %s", "foo", 128, "bar") >= 0);
assert_se(strv_extendf(&b, "test3 %s %s %d", "bar", "foo", 128) >= 0);
@ -513,7 +513,7 @@ static void test_strv_foreach(void) {
unsigned i = 0;
char **check;
a = strv_new("one", "two", "three", NULL);
a = strv_new("one", "two", "three");
assert_se(a);
@ -527,7 +527,7 @@ static void test_strv_foreach_backwards(void) {
unsigned i = 2;
char **check;
a = strv_new("one", "two", "three", NULL);
a = strv_new("one", "two", "three");
assert_se(a);
@ -547,8 +547,7 @@ static void test_strv_foreach_pair(void) {
a = strv_new("pair_one", "pair_one",
"pair_two", "pair_two",
"pair_three", "pair_three",
NULL);
"pair_three", "pair_three");
STRV_FOREACH_PAIR(x, y, a) {
assert_se(streq(*x, *y));
@ -608,7 +607,7 @@ static void test_strv_insert(void) {
static void test_strv_push_prepend(void) {
_cleanup_strv_free_ char **a = NULL;
a = strv_new("foo", "bar", "three", NULL);
a = strv_new("foo", "bar", "three");
assert_se(strv_push_prepend(&a, strdup("first")) >= 0);
assert_se(streq(a[0], "first"));
@ -648,11 +647,11 @@ static void test_strv_equal(void) {
_cleanup_strv_free_ char **b = NULL;
_cleanup_strv_free_ char **c = NULL;
a = strv_new("one", "two", "three", NULL);
a = strv_new("one", "two", "three");
assert_se(a);
b = strv_new("one", "two", "three", NULL);
b = strv_new("one", "two", "three");
assert_se(a);
c = strv_new("one", "two", "three", "four", NULL);
c = strv_new("one", "two", "three", "four");
assert_se(a);
assert_se(strv_equal(a, a));
@ -667,19 +666,19 @@ static void test_strv_equal(void) {
static void test_strv_is_uniq(void) {
_cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL, **d = NULL;
a = strv_new(NULL, NULL);
a = strv_new(NULL);
assert_se(a);
assert_se(strv_is_uniq(a));
b = strv_new("foo", NULL);
b = strv_new("foo");
assert_se(b);
assert_se(strv_is_uniq(b));
c = strv_new("foo", "bar", NULL);
c = strv_new("foo", "bar");
assert_se(c);
assert_se(strv_is_uniq(c));
d = strv_new("foo", "bar", "waldo", "bar", "piep", NULL);
d = strv_new("foo", "bar", "waldo", "bar", "piep");
assert_se(d);
assert_se(!strv_is_uniq(d));
}
@ -687,26 +686,26 @@ static void test_strv_is_uniq(void) {
static void test_strv_reverse(void) {
_cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL, **d = NULL;
a = strv_new(NULL, NULL);
a = strv_new(NULL);
assert_se(a);
strv_reverse(a);
assert_se(strv_isempty(a));
b = strv_new("foo", NULL);
b = strv_new("foo");
assert_se(b);
strv_reverse(b);
assert_se(streq_ptr(b[0], "foo"));
assert_se(streq_ptr(b[1], NULL));
c = strv_new("foo", "bar", NULL);
c = strv_new("foo", "bar");
assert_se(c);
strv_reverse(c);
assert_se(streq_ptr(c[0], "bar"));
assert_se(streq_ptr(c[1], "foo"));
assert_se(streq_ptr(c[2], NULL));
d = strv_new("foo", "bar", "waldo", NULL);
d = strv_new("foo", "bar", "waldo");
assert_se(d);
strv_reverse(d);
assert_se(streq_ptr(d[0], "waldo"));
@ -718,7 +717,7 @@ static void test_strv_reverse(void) {
static void test_strv_shell_escape(void) {
_cleanup_strv_free_ char **v = NULL;
v = strv_new("foo:bar", "bar,baz", "wal\\do", NULL);
v = strv_new("foo:bar", "bar,baz", "wal\\do");
assert_se(v);
assert_se(strv_shell_escape(v, ",:"));
assert_se(streq_ptr(v[0], "foo\\:bar"));
@ -752,7 +751,7 @@ static void test_strv_skip(void) {
static void test_strv_extend_n(void) {
_cleanup_strv_free_ char **v = NULL;
v = strv_new("foo", "bar", NULL);
v = strv_new("foo", "bar");
assert_se(v);
assert_se(strv_extend_n(&v, "waldo", 3) >= 0);
@ -808,8 +807,8 @@ static void test_strv_free_free(void) {
char ***t;
assert_se(t = new(char**, 3));
assert_se(t[0] = strv_new("a", "b", NULL));
assert_se(t[1] = strv_new("c", "d", "e", NULL));
assert_se(t[0] = strv_new("a", "b"));
assert_se(t[1] = strv_new("c", "d", "e"));
t[2] = NULL;
t = strv_free_free(t);
@ -839,7 +838,7 @@ static void test_strv_fnmatch(void) {
assert_se(!strv_fnmatch(STRV_MAKE_EMPTY, "a", 0));
v = strv_new("*\\*", NULL);
v = strv_new("*\\*");
assert_se(!strv_fnmatch(v, "\\", 0));
assert_se(strv_fnmatch(v, "\\", FNM_NOESCAPE));
}