]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shared/bus-unit-util: tweak error handling in bus_append_exec_command
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 4 Jul 2025 12:06:52 +0000 (14:06 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 6 Jul 2025 15:47:11 +0000 (17:47 +0200)
exec_command_flags_to_strv() should not fail, unless we screwed up, so assert
instead of returning an error. Also, no need to strdup constant _PATH_BSHELL;
drop that so that we can get rid of the oom error handling. Finally, rename
l → cmdline for clarity.

src/shared/bus-unit-util.c

index e053cd31a610c4c50f40afc028836b6407dd30c3..d98550dd0b5a2d41aa3ed543654178852392b8a6 100644 (file)
@@ -561,8 +561,8 @@ static int bus_append_socket_filter(sd_bus_message *m, const char *field, const
 
 static int bus_append_exec_command(sd_bus_message *m, const char *field, const char *eq) {
         bool explicit_path = false, done = false, ambient_hack = false;
-        _cleanup_strv_free_ char **l = NULL, **ex_opts = NULL;
-        _cleanup_free_ char *path = NULL, *upgraded_name = NULL;
+        _cleanup_strv_free_ char **cmdline = NULL, **ex_opts = NULL;
+        _cleanup_free_ char *_path = NULL, *upgraded_name = NULL;
         ExecCommandFlags flags = 0;
         bool is_ex_prop = endswith(field, "Ex");
         int r;
@@ -655,27 +655,26 @@ static int bus_append_exec_command(sd_bus_message *m, const char *field, const c
                         return log_error_errno(r, "Failed to convert ExecCommandFlags to strv: %m");
         }
 
-        if (FLAGS_SET(flags, EXEC_COMMAND_VIA_SHELL)) {
-                path = strdup(_PATH_BSHELL);
-                if (!path)
-                        return log_oom();
-
-        } else if (explicit_path) {
-                r = extract_first_word(&eq, &path, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE);
+        const char *path = NULL;
+        if (FLAGS_SET(flags, EXEC_COMMAND_VIA_SHELL))
+                path = _PATH_BSHELL;
+        else if (explicit_path) {
+                r = extract_first_word(&eq, &_path, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE);
                 if (r < 0)
                         return log_error_errno(r, "Failed to parse path: %m");
                 if (r == 0)
                         return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No executable path specified, refusing.");
                 if (isempty(eq))
                         return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Got empty command line, refusing.");
+                path = _path;
         }
 
-        r = strv_split_full(&l, eq, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE);
+        r = strv_split_full(&cmdline, eq, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE);
         if (r < 0)
                 return log_error_errno(r, "Failed to parse command line: %m");
 
         if (FLAGS_SET(flags, EXEC_COMMAND_VIA_SHELL)) {
-                r = strv_prepend(&l, explicit_path ? "-sh" : "sh");
+                r = strv_prepend(&cmdline, explicit_path ? "-sh" : "sh");
                 if (r < 0)
                         return log_oom();
         }
@@ -696,21 +695,21 @@ static int bus_append_exec_command(sd_bus_message *m, const char *field, const c
         if (r < 0)
                 return bus_log_create_error(r);
 
-        if (!strv_isempty(l)) {
-
+        if (!strv_isempty(cmdline)) {
                 r = sd_bus_message_open_container(m, 'r', is_ex_prop ? "sasas" : "sasb");
                 if (r < 0)
                         return bus_log_create_error(r);
 
-                r = sd_bus_message_append(m, "s", path ?: l[0]);
+                r = sd_bus_message_append(m, "s", path ?: cmdline[0]);
                 if (r < 0)
                         return bus_log_create_error(r);
 
-                r = sd_bus_message_append_strv(m, l);
+                r = sd_bus_message_append_strv(m, cmdline);
                 if (r < 0)
                         return bus_log_create_error(r);
 
-                r = is_ex_prop ? sd_bus_message_append_strv(m, ex_opts) : sd_bus_message_append(m, "b", FLAGS_SET(flags, EXEC_COMMAND_IGNORE_FAILURE));
+                r = is_ex_prop ? sd_bus_message_append_strv(m, ex_opts) :
+                                 sd_bus_message_append(m, "b", FLAGS_SET(flags, EXEC_COMMAND_IGNORE_FAILURE));
                 if (r < 0)
                         return bus_log_create_error(r);