]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
various: use strdup_to() after getenv()
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 19 Mar 2024 19:34:42 +0000 (20:34 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 20 Mar 2024 14:18:21 +0000 (15:18 +0100)
src/basic/efivars.c
src/basic/path-lookup.c
src/basic/proc-cmdline.c
src/basic/process-util.c

index 9011ae29a3a82d8bebf2b1f28a799e07c5a927de..fdc6c439bb85cb563080b0c7c2e947b77fb5a3a7 100644 (file)
@@ -398,16 +398,8 @@ int systemd_efi_options_variable(char **ret) {
 
         /* For testing purposes it is sometimes useful to be able to override this */
         e = secure_getenv("SYSTEMD_EFI_OPTIONS");
-        if (e) {
-                char *m;
-
-                m = strdup(e);
-                if (!m)
-                        return -ENOMEM;
-
-                *ret = m;
-                return 0;
-        }
+        if (e)
+                return strdup_to(ret, e);
 
         r = read_one_line_file(EFIVAR_CACHE_PATH(EFI_SYSTEMD_VARIABLE(SystemdOptions)), ret);
         if (r == -ENOENT)
index e7fc4a7f06922d0be04210bc3577b8f5dcd14022..540256b73b84300e39db52bc81e5ffcebb01c370 100644 (file)
@@ -92,7 +92,6 @@ int xdg_user_data_dir(char **ret, const char *suffix) {
 }
 
 int runtime_directory(char **ret, RuntimeScope scope, const char *suffix) {
-        _cleanup_free_ char *d = NULL;
         int r;
 
         assert(ret);
@@ -106,26 +105,20 @@ int runtime_directory(char **ret, RuntimeScope scope, const char *suffix) {
          * Return value indicates whether the suffix was applied or not */
 
         const char *e = secure_getenv("RUNTIME_DIRECTORY");
-        if (e) {
-                d = strdup(e);
-                if (!d)
-                        return -ENOMEM;
-
-                *ret = TAKE_PTR(d);
-                return false;
-        }
+        if (e)
+                return strdup_to(ret, e);
 
         if (scope == RUNTIME_SCOPE_USER) {
-                r = xdg_user_runtime_dir(&d, suffix);
+                r = xdg_user_runtime_dir(ret, suffix);
                 if (r < 0)
                         return r;
         } else {
-                d = path_join("/run", suffix);
+                char *d = path_join("/run", suffix);
                 if (!d)
                         return -ENOMEM;
+                *ret = d;
         }
 
-        *ret = TAKE_PTR(d);
         return true;
 }
 
index 522d8de1f4fe65196f60ca81e74f70eace011337..ce1ba3a1eabe00a1485bf3a829cbe3fd2369759b 100644 (file)
@@ -116,16 +116,8 @@ int proc_cmdline(char **ret) {
 
         /* For testing purposes it is sometimes useful to be able to override what we consider /proc/cmdline to be */
         e = secure_getenv("SYSTEMD_PROC_CMDLINE");
-        if (e) {
-                char *m;
-
-                m = strdup(e);
-                if (!m)
-                        return -ENOMEM;
-
-                *ret = m;
-                return 0;
-        }
+        if (e)
+                return strdup_to(ret, e);
 
         if (detect_container() > 0)
                 return pid_get_cmdline(1, SIZE_MAX, 0, ret);
index 69635e65f81f9bb727e8de0f254441141c889a43..351b5e4095924fd4b63505c358940f91e8e839ca 100644 (file)
@@ -1022,7 +1022,6 @@ int kill_and_sigcont(pid_t pid, int sig) {
 
 int getenv_for_pid(pid_t pid, const char *field, char **ret) {
         _cleanup_fclose_ FILE *f = NULL;
-        char *value = NULL;
         const char *path;
         size_t sum = 0;
         int r;
@@ -1031,22 +1030,8 @@ int getenv_for_pid(pid_t pid, const char *field, char **ret) {
         assert(field);
         assert(ret);
 
-        if (pid == 0 || pid == getpid_cached()) {
-                const char *e;
-
-                e = getenv(field);
-                if (!e) {
-                        *ret = NULL;
-                        return 0;
-                }
-
-                value = strdup(e);
-                if (!value)
-                        return -ENOMEM;
-
-                *ret = value;
-                return 1;
-        }
+        if (pid == 0 || pid == getpid_cached())
+                return strdup_to_full(ret, getenv(field));
 
         if (!pid_is_valid(pid))
                 return -EINVAL;
@@ -1075,14 +1060,8 @@ int getenv_for_pid(pid_t pid, const char *field, char **ret) {
                 sum += r;
 
                 match = startswith(line, field);
-                if (match && *match == '=') {
-                        value = strdup(match + 1);
-                        if (!value)
-                                return -ENOMEM;
-
-                        *ret = value;
-                        return 1;
-                }
+                if (match && *match == '=')
+                        return strdup_to_full(ret, match + 1);
         }
 
         *ret = NULL;