dbus: Cleanup and fix loops

Simpler to use for loops instead of handling next pointer selection
in all places. In addition, couple of functions could have ended up
in an infinite loop on error path since the pointer update was missed.
This commit is contained in:
Jouni Malinen 2010-01-01 14:25:25 +02:00
parent 4292b01322
commit de7a79ceb6

View file

@ -41,14 +41,15 @@ struct interfaces {
static struct interfaces * extract_interfaces( static struct interfaces * extract_interfaces(
struct wpa_dbus_object_desc *obj_dsc, xmlNodePtr root_node) struct wpa_dbus_object_desc *obj_dsc, xmlNodePtr root_node)
{ {
struct wpa_dbus_method_desc *method_dsc = obj_dsc->methods; struct wpa_dbus_method_desc *method_dsc;
struct wpa_dbus_signal_desc *signal_dsc = obj_dsc->signals; struct wpa_dbus_signal_desc *signal_dsc;
struct wpa_dbus_property_desc *property_dsc = obj_dsc->properties; struct wpa_dbus_property_desc *property_dsc;
struct interfaces *head = NULL; struct interfaces *head = NULL;
struct interfaces *iface, *last; struct interfaces *iface, *last;
/* extract interfaces from methods */ /* extract interfaces from methods */
while (method_dsc) { for (method_dsc = obj_dsc->methods; method_dsc;
method_dsc = method_dsc->next) {
iface = head; iface = head;
last = NULL; last = NULL;
@ -60,18 +61,12 @@ static struct interfaces * extract_interfaces(
last = iface; last = iface;
iface = iface->next; iface = iface->next;
} }
if (iface) { if (iface)
method_dsc = method_dsc->next;
continue; continue;
}
iface = os_zalloc(sizeof(struct interfaces)); iface = os_zalloc(sizeof(struct interfaces));
if (!iface) { if (!iface)
wpa_printf(MSG_ERROR, "Not enough memory to create "
"interface introspection data");
method_dsc = method_dsc->next;
continue; continue;
}
if (last) if (last)
last->next = iface; last->next = iface;
@ -79,25 +74,19 @@ static struct interfaces * extract_interfaces(
head = iface; head = iface;
iface->dbus_interface = os_strdup(method_dsc->dbus_interface); iface->dbus_interface = os_strdup(method_dsc->dbus_interface);
if (!iface->dbus_interface) { if (!iface->dbus_interface)
wpa_printf(MSG_ERROR, "Not enough memory to create "
"interface introspection data (interface "
"name)");
method_dsc = method_dsc->next;
continue; continue;
}
iface->interface_node = xmlNewChild(root_node, NULL, iface->interface_node = xmlNewChild(root_node, NULL,
BAD_CAST "interface", BAD_CAST "interface",
NULL); NULL);
xmlNewProp(iface->interface_node, BAD_CAST "name", xmlNewProp(iface->interface_node, BAD_CAST "name",
BAD_CAST method_dsc->dbus_interface); BAD_CAST method_dsc->dbus_interface);
method_dsc = method_dsc->next;
} }
/* extract interfaces from signals */ /* extract interfaces from signals */
while (signal_dsc) { for (signal_dsc = obj_dsc->signals; signal_dsc;
signal_dsc = signal_dsc->next) {
iface = head; iface = head;
last = NULL; last = NULL;
@ -109,18 +98,12 @@ static struct interfaces * extract_interfaces(
last = iface; last = iface;
iface = iface->next; iface = iface->next;
} }
if (iface) { if (iface)
signal_dsc = signal_dsc->next;
continue; continue;
}
iface = os_zalloc(sizeof(struct interfaces)); iface = os_zalloc(sizeof(struct interfaces));
if (!iface) { if (!iface)
wpa_printf(MSG_ERROR, "Not enough memory to create "
"interface introspection data");
signal_dsc = signal_dsc->next;
continue; continue;
}
if (last) if (last)
last->next = iface; last->next = iface;
@ -128,25 +111,19 @@ static struct interfaces * extract_interfaces(
head = iface; head = iface;
iface->dbus_interface = os_strdup(signal_dsc->dbus_interface); iface->dbus_interface = os_strdup(signal_dsc->dbus_interface);
if (!iface->dbus_interface) { if (!iface->dbus_interface)
wpa_printf(MSG_ERROR, "Not enough memory to create "
"interface introspection data (interface "
"name)");
signal_dsc = signal_dsc->next;
continue; continue;
}
iface->interface_node = xmlNewChild(root_node, NULL, iface->interface_node = xmlNewChild(root_node, NULL,
BAD_CAST "interface", BAD_CAST "interface",
NULL); NULL);
xmlNewProp(iface->interface_node, BAD_CAST "name", xmlNewProp(iface->interface_node, BAD_CAST "name",
BAD_CAST signal_dsc->dbus_interface); BAD_CAST signal_dsc->dbus_interface);
signal_dsc = signal_dsc->next;
} }
/* extract interfaces from properties */ /* extract interfaces from properties */
while (property_dsc) { for (property_dsc = obj_dsc->properties; property_dsc;
property_dsc = property_dsc->next) {
iface = head; iface = head;
last = NULL; last = NULL;
@ -158,18 +135,12 @@ static struct interfaces * extract_interfaces(
last = iface; last = iface;
iface = iface->next; iface = iface->next;
} }
if (iface) { if (iface)
property_dsc = property_dsc->next;
continue; continue;
}
iface = os_zalloc(sizeof(struct interfaces)); iface = os_zalloc(sizeof(struct interfaces));
if (!iface) { if (!iface)
wpa_printf(MSG_ERROR, "Not enough memory to create "
"interface introspection data");
property_dsc = property_dsc->next;
continue; continue;
}
if (last) if (last)
last->next = iface; last->next = iface;
@ -178,21 +149,14 @@ static struct interfaces * extract_interfaces(
iface->dbus_interface = iface->dbus_interface =
os_strdup(property_dsc->dbus_interface); os_strdup(property_dsc->dbus_interface);
if (!iface->dbus_interface) { if (!iface->dbus_interface)
wpa_printf(MSG_ERROR, "Not enough memory to create "
"interface introspection data (interface "
"name)");
property_dsc = property_dsc->next;
continue; continue;
}
iface->interface_node = xmlNewChild(root_node, NULL, iface->interface_node = xmlNewChild(root_node, NULL,
BAD_CAST "interface", BAD_CAST "interface",
NULL); NULL);
xmlNewProp(iface->interface_node, BAD_CAST "name", xmlNewProp(iface->interface_node, BAD_CAST "name",
BAD_CAST property_dsc->dbus_interface); BAD_CAST property_dsc->dbus_interface);
property_dsc = property_dsc->next;
} }
return head; return head;
@ -306,9 +270,8 @@ DBusMessage * wpa_dbus_introspect(DBusMessage *message,
ifaces = extract_interfaces(obj_dsc, root_node); ifaces = extract_interfaces(obj_dsc, root_node);
/* create methods' nodes */ /* create methods' nodes */
method_dsc = obj_dsc->methods; for (method_dsc = obj_dsc->methods; method_dsc;
while (method_dsc) { method_dsc = method_dsc->next) {
struct interfaces *iface = ifaces; struct interfaces *iface = ifaces;
while (iface) { while (iface) {
if (!os_strcmp(iface->dbus_interface, if (!os_strcmp(iface->dbus_interface,
@ -340,13 +303,11 @@ DBusMessage * wpa_dbus_introspect(DBusMessage *message,
BAD_CAST (arg.dir == ARG_IN ? BAD_CAST (arg.dir == ARG_IN ?
"in" : "out")); "in" : "out"));
} }
method_dsc = method_dsc->next;
} }
/* create signals' nodes */ /* create signals' nodes */
signal_dsc = obj_dsc->signals; for (signal_dsc = obj_dsc->signals; signal_dsc;
while (signal_dsc) { signal_dsc = signal_dsc->next) {
struct interfaces *iface = ifaces; struct interfaces *iface = ifaces;
while (iface) { while (iface) {
if (!os_strcmp(iface->dbus_interface, if (!os_strcmp(iface->dbus_interface,
@ -375,13 +336,11 @@ DBusMessage * wpa_dbus_introspect(DBusMessage *message,
xmlNewProp(arg_node, BAD_CAST "type", xmlNewProp(arg_node, BAD_CAST "type",
BAD_CAST arg.type); BAD_CAST arg.type);
} }
signal_dsc = signal_dsc->next;
} }
/* create properties' nodes */ /* create properties' nodes */
property_dsc = obj_dsc->properties; for (property_dsc = obj_dsc->properties; property_dsc;
while (property_dsc) { property_dsc = property_dsc->next) {
struct interfaces *iface = ifaces; struct interfaces *iface = ifaces;
while (iface) { while (iface) {
if (!os_strcmp(iface->dbus_interface, if (!os_strcmp(iface->dbus_interface,
@ -403,8 +362,6 @@ DBusMessage * wpa_dbus_introspect(DBusMessage *message,
(property_dsc->access == R ? "read" : (property_dsc->access == R ? "read" :
(property_dsc->access == W ? (property_dsc->access == W ?
"write" : "readwrite"))); "write" : "readwrite")));
property_dsc = property_dsc->next;
} }
/* add child nodes to introspection tree; */ /* add child nodes to introspection tree; */
@ -442,4 +399,3 @@ DBusMessage * wpa_dbus_introspect(DBusMessage *message,
return reply; return reply;
} }