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

2013-08-07 Thread Zbigniew Jędrzejewski-Szmek
On Thu, Jul 25, 2013 at 05:36:01PM +0200, Ronny Chevalier wrote:
 ---
  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(-)
Applied...

 @@ -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;
  }
... but you could think about also applying _cleanup_fclose_ here.
This would completely kill the cleanup in error handling blocks,
making things simpler.

  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;
... and I removed those parts which split variable declarations into
separate lines. They were fine as they were.

Zbyszek
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/systemd-devel


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

2013-07-25 Thread Ronny Chevalier
---
 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) {