594167
From 65aca6d552b69af81fe9588720194e0b86a160fb Mon Sep 17 00:00:00 2001
594167
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
594167
Date: Wed, 19 Jan 2022 09:51:19 +0100
594167
Subject: [PATCH] core/execute: use _cleanup_ in
594167
 exec_context_load_environment()
594167
594167
Also rename variables.
594167
594167
(cherry picked from commit 398a5009169fdc0c4eb147692c0cd929b9fe4c84)
594167
594167
Related: #2017035
594167
---
594167
 src/core/execute.c | 51 +++++++++++++++++++---------------------------
594167
 1 file changed, 21 insertions(+), 30 deletions(-)
594167
594167
diff --git a/src/core/execute.c b/src/core/execute.c
594167
index 16f346f339..2ab65e9cfe 100644
594167
--- a/src/core/execute.c
594167
+++ b/src/core/execute.c
594167
@@ -5363,20 +5363,18 @@ static int exec_context_named_iofds(
594167
         return targets == 0 ? 0 : -ENOENT;
594167
 }
594167
 
594167
-static int exec_context_load_environment(const Unit *unit, const ExecContext *c, char ***l) {
594167
-        char **i, **r = NULL;
594167
+static int exec_context_load_environment(const Unit *unit, const ExecContext *c, char ***ret) {
594167
+        _cleanup_strv_free_ char **v = NULL;
594167
+        char **i;
594167
+        int r;
594167
 
594167
         assert(c);
594167
-        assert(l);
594167
+        assert(ret);
594167
 
594167
         STRV_FOREACH(i, c->environment_files) {
594167
-                char *fn;
594167
-                int k;
594167
-                bool ignore = false;
594167
-                char **p;
594167
                 _cleanup_globfree_ glob_t pglob = {};
594167
-
594167
-                fn = *i;
594167
+                bool ignore = false;
594167
+                char *fn = *i;
594167
 
594167
                 if (fn[0] == '-') {
594167
                         ignore = true;
594167
@@ -5386,33 +5384,30 @@ static int exec_context_load_environment(const Unit *unit, const ExecContext *c,
594167
                 if (!path_is_absolute(fn)) {
594167
                         if (ignore)
594167
                                 continue;
594167
-
594167
-                        strv_free(r);
594167
                         return -EINVAL;
594167
                 }
594167
 
594167
                 /* Filename supports globbing, take all matching files */
594167
-                k = safe_glob(fn, 0, &pglob);
594167
-                if (k < 0) {
594167
+                r = safe_glob(fn, 0, &pglob);
594167
+                if (r < 0) {
594167
                         if (ignore)
594167
                                 continue;
594167
-
594167
-                        strv_free(r);
594167
-                        return k;
594167
+                        return r;
594167
                 }
594167
 
594167
                 /* When we don't match anything, -ENOENT should be returned */
594167
                 assert(pglob.gl_pathc > 0);
594167
 
594167
                 for (unsigned n = 0; n < pglob.gl_pathc; n++) {
594167
-                        k = load_env_file(NULL, pglob.gl_pathv[n], &p);
594167
-                        if (k < 0) {
594167
+                        _cleanup_strv_free_ char **p = NULL;
594167
+
594167
+                        r = load_env_file(NULL, pglob.gl_pathv[n], &p);
594167
+                        if (r < 0) {
594167
                                 if (ignore)
594167
                                         continue;
594167
-
594167
-                                strv_free(r);
594167
-                                return k;
594167
+                                return r;
594167
                         }
594167
+
594167
                         /* Log invalid environment variables with filename */
594167
                         if (p) {
594167
                                 InvalidEnvInfo info = {
594167
@@ -5423,23 +5418,19 @@ static int exec_context_load_environment(const Unit *unit, const ExecContext *c,
594167
                                 p = strv_env_clean_with_callback(p, invalid_env, &info;;
594167
                         }
594167
 
594167
-                        if (!r)
594167
-                                r = p;
594167
+                        if (!v)
594167
+                                v = TAKE_PTR(p);
594167
                         else {
594167
-                                char **m;
594167
-
594167
-                                m = strv_env_merge(r, p);
594167
-                                strv_free(r);
594167
-                                strv_free(p);
594167
+                                char **m = strv_env_merge(v, p);
594167
                                 if (!m)
594167
                                         return -ENOMEM;
594167
 
594167
-                                r = m;
594167
+                                strv_free_and_replace(v, m);
594167
                         }
594167
                 }
594167
         }
594167
 
594167
-        *l = r;
594167
+        *ret = TAKE_PTR(v);
594167
 
594167
         return 0;
594167
 }