chiark / gitweb /
systemctl: fix invalid free when enabling sysv services fails
[elogind.git] / src / systemctl / systemctl.c
index 6e48671ee61ce277c40db5b1bc05b61db03abdf9..17dfff7887abca4f22b450447d00cb62d773bec9 100644 (file)
@@ -5143,7 +5143,7 @@ static int enable_sysv_units(const char *verb, char **args) {
         int r = 0;
 
 #if defined(HAVE_SYSV_COMPAT) && defined(HAVE_CHKCONFIG)
         int r = 0;
 
 #if defined(HAVE_SYSV_COMPAT) && defined(HAVE_CHKCONFIG)
-        unsigned f = 1, t = 1;
+        unsigned f = 0;
         _cleanup_lookup_paths_free_ LookupPaths paths = {};
 
         if (arg_scope != UNIT_FILE_SYSTEM)
         _cleanup_lookup_paths_free_ LookupPaths paths = {};
 
         if (arg_scope != UNIT_FILE_SYSTEM)
@@ -5162,7 +5162,7 @@ static int enable_sysv_units(const char *verb, char **args) {
                 return r;
 
         r = 0;
                 return r;
 
         r = 0;
-        for (f = 0; args[f]; f++) {
+        while (args[f]) {
                 const char *name;
                 _cleanup_free_ char *p = NULL, *q = NULL, *l = NULL;
                 bool found_native = false, found_sysv;
                 const char *name;
                 _cleanup_free_ char *p = NULL, *q = NULL, *l = NULL;
                 bool found_native = false, found_sysv;
@@ -5173,7 +5173,7 @@ static int enable_sysv_units(const char *verb, char **args) {
                 pid_t pid;
                 siginfo_t status;
 
                 pid_t pid;
                 siginfo_t status;
 
-                name = args[f];
+                name = args[f++];
 
                 if (!endswith(name, ".service"))
                         continue;
 
                 if (!endswith(name, ".service"))
                         continue;
@@ -5205,9 +5205,6 @@ static int enable_sysv_units(const char *verb, char **args) {
                 if (!found_sysv)
                         continue;
 
                 if (!found_sysv)
                         continue;
 
-                /* Mark this entry, so that we don't try enabling it as native unit */
-                args[f] = (char*) "";
-
                 log_info("%s is not a native service, redirecting to /sbin/chkconfig.", name);
 
                 if (!isempty(arg_root))
                 log_info("%s is not a native service, redirecting to /sbin/chkconfig.", name);
 
                 if (!isempty(arg_root))
@@ -5256,19 +5253,12 @@ static int enable_sysv_units(const char *verb, char **args) {
                                 return -EINVAL;
                 } else
                         return -EPROTO;
                                 return -EINVAL;
                 } else
                         return -EPROTO;
-        }
-
-        /* Drop all SysV units */
-        for (f = 0, t = 0; args[f]; f++) {
 
 
-                if (isempty(args[f]))
-                        continue;
-
-                args[t++] = args[f];
+                /* Remove this entry, so that we don't try enabling it as native unit */
+                assert(f > 0 && streq(args[f-1], name));
+                assert_se(strv_remove(args + f - 1, name));
         }
 
         }
 
-        args[t] = NULL;
-
 #endif
         return r;
 }
 #endif
         return r;
 }