1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-02 10:51:20 +03:00

env-util: rework strv_env_merge()

Let's clean up the failure codepaths, by using _cleanup_.

This relies on the new behaviour of env_append() introduced in the
previous commit that guarantess the list always remains properly NULL
terminated
This commit is contained in:
Lennart Poettering 2018-10-17 20:10:09 +02:00
parent 2a13184a43
commit 2d3ff1decb

View File

@ -195,10 +195,10 @@ static int env_append(char **r, char ***k, char **a) {
}
char **strv_env_merge(size_t n_lists, ...) {
size_t n = 0;
char **l, **k, **r;
_cleanup_strv_free_ char **ret = NULL;
size_t n = 0, i;
char **l, **k;
va_list ap;
size_t i;
/* Merges an arbitrary number of environment sets */
@ -209,29 +209,24 @@ char **strv_env_merge(size_t n_lists, ...) {
}
va_end(ap);
r = new(char*, n+1);
if (!r)
ret = new(char*, n+1);
if (!ret)
return NULL;
k = r;
*ret = NULL;
k = ret;
va_start(ap, n_lists);
for (i = 0; i < n_lists; i++) {
l = va_arg(ap, char**);
if (env_append(r, &k, l) < 0)
goto fail;
if (env_append(ret, &k, l) < 0) {
va_end(ap);
return NULL;
}
}
va_end(ap);
*k = NULL;
return r;
fail:
va_end(ap);
strv_free(r);
return NULL;
return TAKE_PTR(ret);
}
static bool env_match(const char *t, const char *pattern) {