chiark / gitweb /
sd-login: fix memleak when output argument is NULL
[elogind.git] / src / libelogind / sd-login / sd-login.c
index 7630e6265334f3f7d60e4248bd0b728648650faa..80b8f8fefc7f91f1a817d2936655d51e1dc998ab 100644 (file)
@@ -942,14 +942,14 @@ _public_ int sd_get_uids(uid_t **users) {
 }
 
 _public_ int sd_get_machine_names(char ***machines) {
-        char **l = NULL, **a, **b;
+        _cleanup_strv_free_ char **l = NULL;
+        char **a, **b;
         int r;
 
-        assert_return(machines, -EINVAL);
-
         r = get_files_in_directory("/run/systemd/machines/", &l);
         if (r == -ENOENT) {
-                *machines = NULL;
+                if (machines)
+                        *machines = NULL;
                 return 0;
         }
         if (r < 0)
@@ -959,7 +959,7 @@ _public_ int sd_get_machine_names(char ***machines) {
                 r = 0;
 
                 /* Filter out the unit: symlinks */
-                for (a = l, b = l; *a; a++) {
+                for (a = b = l; *a; a++) {
                         if (startswith(*a, "unit:") || !machine_name_is_valid(*a))
                                 free(*a);
                         else {
@@ -972,7 +972,10 @@ _public_ int sd_get_machine_names(char ***machines) {
                 *b = NULL;
         }
 
-        *machines = l;
+        if (machines) {
+                *machines = l;
+                l = NULL;
+        }
         return r;
 }