chiark / gitweb /
Introduce strv_consume which takes ownership
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 4 Mar 2014 14:20:51 +0000 (09:20 -0500)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 4 Mar 2014 15:04:50 +0000 (10:04 -0500)
This mirrors set_consume and makes the common use a bit nicer.

15 files changed:
src/bus-driverd/bus-driverd.c
src/core/dbus-execute.c
src/core/locale-setup.c
src/core/unit.c
src/libsystemd/sd-bus/bus-control.c
src/login/logind-seat-dbus.c
src/login/logind-session-dbus.c
src/login/logind-user-dbus.c
src/machine/machine-dbus.c
src/shared/acl-util.c
src/shared/conf-parser.c
src/shared/fileio.c
src/shared/strv.c
src/shared/strv.h
src/systemctl/systemctl.c

index 5405960..3284d62 100644 (file)
@@ -562,11 +562,9 @@ static int driver_list_queued_owners(sd_bus *bus, sd_bus_message *m, void *userd
                 if (asprintf(&n, ":1.%llu", (unsigned long long) name->owner_id) < 0)
                         return -ENOMEM;
 
-                r = strv_push(&owners, n);
-                if (r < 0) {
-                        free(n);
-                        return -ENOMEM;
-                }
+                r = strv_consume(&owners, n);
+                if (r < 0)
+                        return r;
         }
 
         r = ioctl(bus->input_fd, KDBUS_CMD_FREE, &cmd.offset);
index 6d0bdce..3a05303 100644 (file)
@@ -385,11 +385,9 @@ static int property_get_syscall_filter(
                 if (!name)
                         continue;
 
-                r = strv_push(&l, name);
-                if (r < 0) {
-                        free(name);
-                        return -ENOMEM;
-                }
+                r = strv_consume(&l, name);
+                if (r < 0)
+                        return r;
         }
 #endif
 
index 276deb9..7a41035 100644 (file)
@@ -133,8 +133,7 @@ int locale_setup(char ***environment) {
                         goto finish;
                 }
 
-                if (strv_push(&add, s) < 0) {
-                        free(s);
+                if (strv_consume(&add, s) < 0) {
                         r = -ENOMEM;
                         goto finish;
                 }
index 1c0b0c7..d0e9159 100644 (file)
@@ -3181,11 +3181,9 @@ int unit_require_mounts_for(Unit *u, const char *path) {
                 return 0;
         }
 
-        r = strv_push(&u->requires_mounts_for, p);
-        if (r < 0) {
-                free(p);
+        r = strv_consume(&u->requires_mounts_for, p);
+        if (r < 0)
                 return r;
-        }
 
         PATH_FOREACH_PREFIX_MORE(prefix, p) {
                 Set *x;
index 22b95d5..db0c11b 100644 (file)
@@ -237,11 +237,9 @@ static int kernel_get_list(sd_bus *bus, uint64_t flags, char ***x) {
                         if (asprintf(&n, ":1.%llu", (unsigned long long) name->owner_id) < 0)
                                 return -ENOMEM;
 
-                        r = strv_push(x, n);
-                        if (r < 0) {
-                                free(n);
-                                return -ENOMEM;
-                        }
+                        r = strv_consume(x, n);
+                        if (r < 0)
+                                return r;
 
                         previous_id = name->owner_id;
                 }
index 26cddfe..315e6ba 100644 (file)
@@ -399,11 +399,9 @@ int seat_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***
                 if (!p)
                         return -ENOMEM;
 
-                r = strv_push(&l, p);
-                if (r < 0) {
-                        free(p);
+                r = strv_consume(&l, p);
+                if (r < 0)
                         return r;
-                }
         }
 
         *nodes = l;
index f9305dd..fc728bb 100644 (file)
@@ -558,11 +558,9 @@ int session_node_enumerator(sd_bus *bus, const char *path, void *userdata, char
                 if (!p)
                         return -ENOMEM;
 
-                r = strv_push(&l, p);
-                if (r < 0) {
-                        free(p);
+                r = strv_consume(&l, p);
+                if (r < 0)
                         return r;
-                }
         }
 
         *nodes = l;
index 18eea89..b5d27e7 100644 (file)
@@ -315,11 +315,9 @@ int user_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***
                 if (!p)
                         return -ENOMEM;
 
-                r = strv_push(&l, p);
-                if (r < 0) {
-                        free(p);
+                r = strv_consume(&l, p);
+                if (r < 0)
                         return r;
-                }
         }
 
         *nodes = l;
index df96ccf..c9e3bb7 100644 (file)
@@ -225,11 +225,9 @@ int machine_node_enumerator(sd_bus *bus, const char *path, void *userdata, char
                 if (!p)
                         return -ENOMEM;
 
-                r = strv_push(&l, p);
-                if (r < 0) {
-                        free(p);
+                r = strv_consume(&l, p);
+                if (r < 0)
                         return r;
-                }
         }
 
         *nodes = l;
index fb04e49..c93f58a 100644 (file)
@@ -136,9 +136,8 @@ int search_acl_groups(char*** dst, const char* path, bool* belong) {
                                 return log_oom();
                         }
 
-                        r = strv_push(dst, name);
+                        r = strv_consume(dst, name);
                         if (r < 0) {
-                                free(name);
                                 acl_free(acl);
                                 return log_oom();
                         }
index 86435d3..0a87a71 100644 (file)
@@ -681,7 +681,7 @@ int config_parse_strv(const char *unit,
         }
 
         FOREACH_WORD_QUOTED(w, l, rvalue, state) {
-                _cleanup_free_ char *n;
+                char *n;
 
                 n = cunescape_length(w, l);
                 if (!n)
@@ -693,7 +693,7 @@ int config_parse_strv(const char *unit,
                         continue;
                 }
 
-                r = strv_extend(sv, n);
+                r = strv_consume(sv, n);
                 if (r < 0)
                         return log_oom();
         }
index 0d3f2e9..d591567 100644 (file)
@@ -614,11 +614,9 @@ static int load_env_file_push(const char *filename, unsigned line,
         if (!p)
                 return -ENOMEM;
 
-        r = strv_push(m, p);
-        if (r < 0) {
-                free(p);
+        r = strv_consume(m, p);
+        if (r < 0)
                 return r;
-        }
 
         free(value);
         return 0;
index 13deba7..67706dc 100644 (file)
@@ -378,9 +378,18 @@ int strv_push(char ***l, char *value) {
         return 0;
 }
 
+int strv_consume(char ***l, char *value) {
+        int r;
+
+        r = strv_push(l, value);
+        if (r < 0)
+                free(value);
+
+        return r;
+}
+
 int strv_extend(char ***l, const char *value) {
         char *v;
-        int r;
 
         if (!value)
                 return 0;
@@ -389,11 +398,7 @@ int strv_extend(char ***l, const char *value) {
         if (!v)
                 return -ENOMEM;
 
-        r = strv_push(l, v);
-        if (r < 0)
-                free(v);
-
-        return r;
+        return strv_consume(l, v);
 }
 
 char **strv_uniq(char **l) {
index 715bc54..13a4bb6 100644 (file)
@@ -40,6 +40,7 @@ int strv_extend_strv(char ***a, char **b);
 int strv_extend_strv_concat(char ***a, char **b, const char *suffix);
 int strv_extend(char ***l, const char *value);
 int strv_push(char ***l, char *value);
+int strv_consume(char ***l, char *value);
 
 char **strv_remove(char **l, const char *s);
 char **strv_uniq(char **l);
index 8692716..952d2f8 100644 (file)
@@ -2167,13 +2167,11 @@ static int expand_names(sd_bus *bus, char **names, const char* suffix, char ***r
                         return log_oom();
 
                 if (string_is_glob(t))
-                        r = strv_push(&globs, t);
+                        r = strv_consume(&globs, t);
                 else
-                        r = strv_push(&mangled, t);
-                if (r < 0) {
-                        free(t);
+                        r = strv_consume(&mangled, t);
+                if (r < 0)
                         return log_oom();
-                }
         }
 
         /* Query the manager only if any of the names are a glob, since
@@ -5346,10 +5344,8 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
                                         if (!prop)
                                                 return log_oom();
 
-                                        if (strv_push(&arg_properties, prop) < 0) {
-                                                free(prop);
+                                        if (strv_consume(&arg_properties, prop) < 0)
                                                 return log_oom();
-                                        }
                                 }
                         }
 
@@ -5518,10 +5514,8 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
                                 if (!s)
                                         return log_oom();
 
-                                if (strv_push(&arg_states, s) < 0) {
-                                        free(s);
+                                if (strv_consume(&arg_states, s) < 0)
                                         return log_oom();
-                                }
                         }
                         break;
                 }