[systemd-devel] [PATCH] dbus: use _cleanup_free_ instead of freeing ourself

Ronny Chevalier rchevalier at aldebaran-robotics.com
Thu Jul 25 08:36:01 PDT 2013


---
 src/core/dbus-execute.c |  3 +--
 src/core/dbus-job.c     | 11 ++---------
 src/core/dbus-manager.c | 14 +++-----------
 src/core/unit.c         | 48 ++++++++++++++++++++++--------------------------
 4 files changed, 28 insertions(+), 48 deletions(-)

diff --git a/src/core/dbus-execute.c b/src/core/dbus-execute.c
index 73590c8..2402e8c 100644
--- a/src/core/dbus-execute.c
+++ b/src/core/dbus-execute.c
@@ -77,12 +77,11 @@ static int bus_execute_append_oom_score_adjust(DBusMessageIter *i, const char *p
         if (c->oom_score_adjust_set)
                 n = c->oom_score_adjust;
         else {
-                char *t;
+                _cleanup_free_ char *t = NULL;
 
                 n = 0;
                 if (read_one_line_file("/proc/self/oom_score_adj", &t) >= 0) {
                         safe_atoi(t, &n);
-                        free(t);
                 }
         }
 
diff --git a/src/core/dbus-job.c b/src/core/dbus-job.c
index a85d318..4ab88d0 100644
--- a/src/core/dbus-job.c
+++ b/src/core/dbus-job.c
@@ -60,7 +60,7 @@ static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_job_append_type, job_type, JobType);
 static int bus_job_append_unit(DBusMessageIter *i, const char *property, void *data) {
         Job *j = data;
         DBusMessageIter sub;
-        char *p;
+        _cleanup_free_ char *p = NULL;
 
         assert(i);
         assert(property);
@@ -75,12 +75,9 @@ static int bus_job_append_unit(DBusMessageIter *i, const char *property, void *d
 
         if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &j->unit->id) ||
             !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
-                free(p);
                 return -ENOMEM;
         }
 
-        free(p);
-
         if (!dbus_message_iter_close_container(i, &sub))
                 return -ENOMEM;
 
@@ -136,7 +133,7 @@ static DBusHandlerResult bus_job_message_handler(DBusConnection *connection, DBu
                 /* Be nice to gdbus and return introspection data for our mid-level paths */
 
                 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
-                        char *introspection = NULL;
+                        _cleanup_free_ char *introspection = NULL;
                         FILE *f;
                         Iterator i;
                         size_t size;
@@ -169,7 +166,6 @@ static DBusHandlerResult bus_job_message_handler(DBusConnection *connection, DBu
 
                         if (ferror(f)) {
                                 fclose(f);
-                                free(introspection);
                                 goto oom;
                         }
 
@@ -179,12 +175,9 @@ static DBusHandlerResult bus_job_message_handler(DBusConnection *connection, DBu
                                 goto oom;
 
                         if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
-                                free(introspection);
                                 goto oom;
                         }
 
-                        free(introspection);
-
                         if (!bus_maybe_send_reply(connection, message, reply))
                                 goto oom;
 
diff --git a/src/core/dbus-manager.c b/src/core/dbus-manager.c
index e1a169c..75e2e45 100644
--- a/src/core/dbus-manager.c
+++ b/src/core/dbus-manager.c
@@ -413,7 +413,7 @@ static int bus_manager_set_log_target(DBusMessageIter *i, const char *property,
 }
 
 static int bus_manager_append_log_level(DBusMessageIter *i, const char *property, void *data) {
-        char *t;
+        _cleanup_free_ char *t = NULL;
         int r;
 
         assert(i);
@@ -426,7 +426,6 @@ static int bus_manager_append_log_level(DBusMessageIter *i, const char *property
         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
                 r = -ENOMEM;
 
-        free(t);
         return r;
 }
 
@@ -1191,7 +1190,7 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection,
                         goto oom;
 
         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
-                char *introspection = NULL;
+                _cleanup_free_ char *introspection = NULL;
                 FILE *f;
                 Iterator i;
                 Unit *u;
@@ -1217,7 +1216,7 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection,
                 fputs(INTROSPECTION_BEGIN, f);
 
                 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
-                        char *p;
+                        _cleanup_free_ char *p = NULL;
 
                         if (k != u->id)
                                 continue;
@@ -1225,12 +1224,10 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection,
                         p = bus_path_escape(k);
                         if (!p) {
                                 fclose(f);
-                                free(introspection);
                                 goto oom;
                         }
 
                         fprintf(f, "<node name=\"unit/%s\"/>", p);
-                        free(p);
                 }
 
                 HASHMAP_FOREACH(j, m->jobs, i)
@@ -1240,7 +1237,6 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection,
 
                 if (ferror(f)) {
                         fclose(f);
-                        free(introspection);
                         goto oom;
                 }
 
@@ -1250,12 +1246,8 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection,
                         goto oom;
 
                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
-                        free(introspection);
                         goto oom;
                 }
-
-                free(introspection);
-
         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
 
                 SELINUX_ACCESS_CHECK(connection, message, "reload");
diff --git a/src/core/unit.c b/src/core/unit.c
index 0e9329f..87ae2a7 100644
--- a/src/core/unit.c
+++ b/src/core/unit.c
@@ -189,7 +189,8 @@ fail:
 }
 
 int unit_choose_id(Unit *u, const char *name) {
-        char *s, *t = NULL, *i;
+        char *s, *i;
+        _cleanup_free_ char *t = NULL;
         int r;
 
         assert(u);
@@ -208,7 +209,6 @@ int unit_choose_id(Unit *u, const char *name) {
 
         /* Selects one of the names of this unit as the id */
         s = set_get(u->names, (char*) name);
-        free(t);
 
         if (!s)
                 return -ENOENT;
@@ -598,7 +598,7 @@ int unit_merge(Unit *u, Unit *other) {
 int unit_merge_by_name(Unit *u, const char *name) {
         Unit *other;
         int r;
-        char *s = NULL;
+        _cleanup_free_ char *s = NULL;
 
         assert(u);
         assert(name);
@@ -619,7 +619,6 @@ int unit_merge_by_name(Unit *u, const char *name) {
         else
                 r = unit_merge(u, other);
 
-        free(s);
         return r;
 }
 
@@ -677,7 +676,7 @@ void unit_dump(Unit *u, FILE *f, const char *prefix) {
         char *t, **j;
         UnitDependency d;
         Iterator i;
-        char *p2;
+        _cleanup_free_ char *p2 = NULL;
         const char *prefix2;
         char
                 timestamp1[FORMAT_TIMESTAMP_MAX],
@@ -811,7 +810,6 @@ void unit_dump(Unit *u, FILE *f, const char *prefix) {
         if (u->nop_job)
                 job_dump(u->nop_job, f, prefix2);
 
-        free(p2);
 }
 
 /* Common implementation for multiple backends */
@@ -1914,7 +1912,7 @@ int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, con
 int unit_add_two_dependencies_by_name(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference) {
         Unit *other;
         int r;
-        char *s;
+        _cleanup_free_ char *s = NULL;
 
         assert(u);
         assert(name || path);
@@ -1923,19 +1921,17 @@ int unit_add_two_dependencies_by_name(Unit *u, UnitDependency d, UnitDependency
                 return -ENOMEM;
 
         if ((r = manager_load_unit(u->manager, name, path, NULL, &other)) < 0)
-                goto finish;
+                return r;
 
         r = unit_add_two_dependencies(u, d, e, other, add_reference);
 
-finish:
-        free(s);
         return r;
 }
 
 int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) {
         Unit *other;
         int r;
-        char *s;
+        _cleanup_free_ char *s = NULL;
 
         assert(u);
         assert(name || path);
@@ -1944,19 +1940,17 @@ int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *n
                 return -ENOMEM;
 
         if ((r = manager_load_unit(u->manager, name, path, NULL, &other)) < 0)
-                goto finish;
+                return r;
 
         r = unit_add_dependency(other, d, u, add_reference);
 
-finish:
-        free(s);
         return r;
 }
 
 int unit_add_two_dependencies_by_name_inverse(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference) {
         Unit *other;
         int r;
-        char *s;
+        _cleanup_free_ char *s = NULL;
 
         assert(u);
         assert(name || path);
@@ -1965,13 +1959,11 @@ int unit_add_two_dependencies_by_name_inverse(Unit *u, UnitDependency d, UnitDep
                 return -ENOMEM;
 
         if ((r = manager_load_unit(u->manager, name, path, NULL, &other)) < 0)
-                goto finish;
+                return r;
 
         if ((r = unit_add_two_dependencies(other, d, e, u, add_reference)) < 0)
-                goto finish;
+                return r;
 
-finish:
-        free(s);
         return r;
 }
 
@@ -1996,7 +1988,8 @@ char *unit_dbus_path(Unit *u) {
 }
 
 char *unit_default_cgroup_path(Unit *u) {
-        _cleanup_free_ char *escaped = NULL, *slice = NULL;
+        _cleanup_free_ char *escaped = NULL;
+        _cleanup_free_ char *slice = NULL;
         int r;
 
         assert(u);
@@ -2035,8 +2028,9 @@ int unit_add_default_slice(Unit *u) {
                 return 0;
 
         if (u->instance) {
-                _cleanup_free_ char *prefix = NULL, *escaped = NULL;
-                        ;
+                _cleanup_free_ char *prefix = NULL;
+                _cleanup_free_ char *escaped = NULL;
+
                 /* Implicitly place all instantiated units in their
                  * own per-template slice */
 
@@ -2349,7 +2343,7 @@ int unit_deserialize(Unit *u, FILE *f, FDSet *fds) {
 
 int unit_add_node_link(Unit *u, const char *what, bool wants) {
         Unit *device;
-        char *e;
+        _cleanup_free_ char *e = NULL;
         int r;
 
         assert(u);
@@ -2367,7 +2361,7 @@ int unit_add_node_link(Unit *u, const char *what, bool wants) {
                 return -ENOMEM;
 
         r = manager_load_unit(u->manager, e, NULL, NULL, &device);
-        free(e);
+
         if (r < 0)
                 return r;
 
@@ -2784,7 +2778,8 @@ static int drop_in_file(Unit *u, UnitSetPropertiesMode mode, const char *name, c
 }
 
 int unit_write_drop_in(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *data) {
-        _cleanup_free_ char *p = NULL, *q = NULL;
+        _cleanup_free_ char *p = NULL;
+        _cleanup_free_ char *q = NULL;
         int r;
 
         assert(u);
@@ -2867,7 +2862,8 @@ int unit_write_drop_in_private_format(Unit *u, UnitSetPropertiesMode mode, const
 }
 
 int unit_remove_drop_in(Unit *u, UnitSetPropertiesMode mode, const char *name) {
-        _cleanup_free_ char *p = NULL, *q = NULL;
+        _cleanup_free_ char *p = NULL;
+        _cleanup_free_ char *q = NULL;
         int r;
 
         assert(u);
-- 
1.8.3.3



More information about the systemd-devel mailing list