ubus: fix fw3_ubus_device() to only return a pointer if a device was found
authorJo-Philipp Wich <jow@openwrt.org>
Thu, 10 Jul 2014 09:03:13 +0000 (11:03 +0200)
committerJo-Philipp Wich <jow@openwrt.org>
Thu, 10 Jul 2014 09:03:18 +0000 (11:03 +0200)
Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
ubus.c

diff --git a/ubus.c b/ubus.c
index 581f51c064ba58459789832ccf13c209eafb14c5..8e5e60e1818fe5249075a6a44e260362886b02ed 100644 (file)
--- a/ubus.c
+++ b/ubus.c
@@ -140,18 +140,13 @@ fw3_ubus_device(const char *net)
        struct fw3_device *dev = NULL;
        struct blob_attr *tb[__DEV_MAX];
        struct blob_attr *cur;
+       char *name = NULL;
        int rem;
 
        if (!net || !interfaces)
                return NULL;
 
-       dev = calloc(1, sizeof(*dev));
-       if (!dev)
-               return NULL;
-
        blobmsg_for_each_attr(cur, interfaces, rem) {
-               char *name;
-
                blobmsg_parse(policy, __DEV_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
                if (!tb[DEV_INTERFACE] ||
                    strcmp(blobmsg_data(tb[DEV_INTERFACE]), net) != 0)
@@ -164,12 +159,21 @@ fw3_ubus_device(const char *net)
                else
                        continue;
 
-               snprintf(dev->name, sizeof(dev->name), "%s", name);
-               dev->set = !!dev->name[0];
-               return dev;
+               break;
        }
 
-       return NULL;
+       if (!name)
+               return NULL;
+
+       dev = calloc(1, sizeof(*dev));
+
+       if (!dev)
+               return NULL;
+
+       snprintf(dev->name, sizeof(dev->name), "%s", name);
+       dev->set = true;
+
+       return dev;
 }
 
 void