]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
systemctl: fix potential memleak on failure in determine_default()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 18 Oct 2022 14:08:24 +0000 (23:08 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 24 Oct 2022 09:55:11 +0000 (18:55 +0900)
And make verb_set_default() return zero on success.

src/systemctl/systemctl-set-default.c

index 06845be4011df7b272b34f1cce5f1924e7899677..c2dbf97649e06f2dfda6ee9c5f8a06935ac568d5 100644 (file)
@@ -109,9 +109,8 @@ int verb_set_default(int argc, char *argv[], void *userdata) {
         if (install_client_side()) {
                 r = unit_file_set_default(arg_scope, UNIT_FILE_FORCE, arg_root, unit, &changes, &n_changes);
                 install_changes_dump(r, "set default", changes, n_changes, arg_quiet);
-
-                if (r > 0)
-                        r = 0;
+                if (r < 0)
+                        goto finish;
         } else {
                 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
                 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
@@ -134,10 +133,9 @@ int verb_set_default(int argc, char *argv[], void *userdata) {
                 /* Try to reload if enabled */
                 if (!arg_no_reload) {
                         r = daemon_reload(ACTION_RELOAD, /* graceful= */ false);
-                        if (r > 0)
-                                r = 0;
-                } else
-                        r = 0;
+                        if (r < 0)
+                                goto finish;
+                }
         }
 
         emit_cmdline_warning();
@@ -147,7 +145,7 @@ int verb_set_default(int argc, char *argv[], void *userdata) {
 
                 r = determine_default(&final);
                 if (r < 0)
-                        return r;
+                        goto finish;
 
                 if (!streq(final, unit))
                         log_notice("Note: \"%s\" is the default unit (possibly a runtime override).", final);
@@ -156,5 +154,5 @@ int verb_set_default(int argc, char *argv[], void *userdata) {
 finish:
         install_changes_free(changes, n_changes);
 
-        return r;
+        return r < 0 ? r : 0;
 }