resource-dbus: better error handling.
authorIsmo Puustinen <ismo.puustinen@intel.com>
Fri, 2 May 2014 10:38:44 +0000 (13:38 +0300)
committerIsmo Puustinen <ismo.puustinen@intel.com>
Fri, 2 May 2014 10:39:41 +0000 (13:39 +0300)
src/plugins/plugin-resource-dbus.c

index 28ac7d8..1f49ce0 100644 (file)
@@ -482,7 +482,8 @@ static bool get_property_dict_entry(property_o_t *prop, mrp_dbus_msg_t *reply)
 {
     bool ret;
 
-    mrp_dbus_msg_open_container(reply, MRP_DBUS_TYPE_DICT_ENTRY, NULL);
+    if (!mrp_dbus_msg_open_container(reply, MRP_DBUS_TYPE_DICT_ENTRY, NULL))
+        return FALSE;
 
     ret = get_property_entry(prop, reply);
 
@@ -1271,8 +1272,7 @@ static int resource_cb(mrp_dbus_t *dbus, mrp_dbus_msg_t *msg, void *data)
     const char *iface = mrp_dbus_msg_interface(msg);
     const char *path = mrp_dbus_msg_path(msg);
     char *error_msg = "Received invalid message";
-
-    mrp_dbus_msg_t *reply;
+    mrp_dbus_msg_t *reply = NULL;
     char buf[MAX_PATH_LENGTH];
 
     dbus_data_t *ctx = data;
@@ -1320,12 +1320,14 @@ static int resource_cb(mrp_dbus_t *dbus, mrp_dbus_msg_t *msg, void *data)
 
         mrp_dbus_msg_open_container(reply, MRP_DBUS_TYPE_ARRAY, "{sv}");
 
-        get_property_dict_entry(resource->name_prop, reply);
-        get_property_dict_entry(resource->status_prop, reply);
-        get_property_dict_entry(resource->mandatory_prop, reply);
-        get_property_dict_entry(resource->shared_prop, reply);
-        get_property_dict_entry(resource->arguments_prop, reply);
-        get_property_dict_entry(resource->conf_prop, reply);
+        if (!(get_property_dict_entry(resource->name_prop, reply) &&
+                get_property_dict_entry(resource->status_prop, reply) &&
+                get_property_dict_entry(resource->mandatory_prop, reply) &&
+                get_property_dict_entry(resource->shared_prop, reply) &&
+                get_property_dict_entry(resource->arguments_prop, reply) &&
+                get_property_dict_entry(resource->conf_prop, reply))) {
+            goto error_reply;
+        }
 
         mrp_dbus_msg_close_container(reply);
 
@@ -1606,6 +1608,11 @@ error_reply:
         mrp_dbus_error_init(&err);
         mrp_dbus_error_set(&err, "org.freedesktop.DBus.Error.Failed", error_msg);
 
+        if (reply) {
+            /* something was already done -- free some memory */
+            mrp_dbus_msg_unref(reply);
+        }
+
         reply = mrp_dbus_msg_error(dbus, msg, &err);
 
         if (reply) {