Fix coverity issuses. 42/174042/3
authorNiraj Kumar Goit <niraj.g@samsung.com>
Tue, 27 Mar 2018 13:41:40 +0000 (19:11 +0530)
committerSaurav Babu <saurav.babu@samsung.com>
Thu, 29 Mar 2018 08:45:36 +0000 (14:15 +0530)
CID: 105915, 107014, 107561, 110117

Change-Id: Iad5807e3a5587a80614df1aeb4f9ced0ce8bd267
Signed-off-by: Niraj Kumar Goit <niraj.g@samsung.com>
src/wifi-netlink-scan.c

index 4f9c61d..1e2356f 100755 (executable)
@@ -576,17 +576,24 @@ static int __netconfig_request_netlink_scan(struct nl_sock *socket,
        gchar *key;
        gboolean ssid_found = FALSE;
        int mcid = __netconfig_get_multicast_id(socket, "nl80211", "scan");
-       nl_socket_add_membership(socket, mcid);
+
+       ret = nl_socket_add_membership(socket, mcid);
+       if (ret < 0) {
+               DBG("Failed to add membership, error: (%s)", nl_geterror(-ret));
+               return ret;
+       }
 
        msg = nlmsg_alloc();
        if (!msg) {
                DBG("Failed to allocate msg");
+               nl_socket_drop_membership(socket, mcid);
                return -ENOMEM;
        }
        ssids = nlmsg_alloc();
        if (!ssids) {
                DBG("Failed to allocate ssids");
                nlmsg_free(msg);
+               nl_socket_drop_membership(socket, mcid);
                return -ENOMEM;
        }
        cb = nl_cb_alloc(NL_CB_DEFAULT);
@@ -594,12 +601,17 @@ static int __netconfig_request_netlink_scan(struct nl_sock *socket,
                DBG("Failed to allocate callbacks");
                nlmsg_free(msg);
                nlmsg_free(ssids);
+               nl_socket_drop_membership(socket, mcid);
                return -ENOMEM;
        }
 
        /** Set nl message and callback functions. */
        genlmsg_put(msg, 0, 0, id, 0, 0, NL80211_CMD_TRIGGER_SCAN, 0);
-       nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_index);
+       ret = nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_index);
+       if (ret < 0) {
+               DBG("Failed to add integer attribute to netlink message, error: (%s)", nl_geterror(-ret));
+               goto out;
+       }
 
        g_variant_get(params, "a{sv}", &iter);
        while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
@@ -609,8 +621,13 @@ static int __netconfig_request_netlink_scan(struct nl_sock *socket,
                                ssid_found = TRUE;
                                DBG("ssid [%s]", ssid);
 
-                               nla_put(ssids, 1, strlen(ssid), ssid);
+                               ret = nla_put(ssids, 1, strlen(ssid), ssid);
                                g_free(ssid);
+                               if (ret < 0) {
+                                       DBG("Failed to add ssid to netlink message, error: (%s)", nl_geterror(-ret));
+                                       g_variant_iter_free(iter);
+                                       goto out;
+                               }
                        }
                } else if (g_strcmp0(key, "VSIE") == 0) {
                        if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
@@ -626,15 +643,23 @@ static int __netconfig_request_netlink_scan(struct nl_sock *socket,
        }
        g_variant_iter_free(iter);
 
-       if (!ssid_found)
-               nla_put(ssids, 1, 0, "");
+       if (!ssid_found) {
+               ret = nla_put(ssids, 1, 0, "");
+               if (ret < 0) {
+                       DBG("nla_put error: (%s)", nl_geterror(-ret));
+                       goto out;
+               }
+       }
        nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids);
-       nlmsg_free(ssids);
 
        if (ies[0] == NETCONFIG_VENDOR_SPECIFIC_ID && ies[1] >= 4) {
                DBG("ies_len: %d ies: %02x %02x %02x %02x %02x %02x %02x", ies_len,
                                ies[0], ies[1], ies[2], ies[3], ies[4], ies[5], ies[6]);
-               nla_put(msg, NL80211_ATTR_IE, ies_len, ies);
+               ret = nla_put(msg, NL80211_ATTR_IE, ies_len, ies);
+               if (ret < 0) {
+                       DBG("Failed to add vsie data to netlink message, error: (%s)", nl_geterror(-ret));
+                       goto out;
+               }
        }
 
        err = 1;
@@ -646,6 +671,11 @@ static int __netconfig_request_netlink_scan(struct nl_sock *socket,
 
        /** Send NL80211_CMD_TRIGGER_SCAN to start the scan. */
        ret = nl_send_auto_complete(socket, msg);
+       if (ret < 0) {
+               DBG("nl_send_auto_complete() error: (%s)", nl_geterror(-ret));
+               goto out;
+       }
+
        DBG("Sent %d bytes to the kernel", ret);
        ssid_found = FALSE;
 
@@ -654,8 +684,7 @@ static int __netconfig_request_netlink_scan(struct nl_sock *socket,
 
        if (ret < 0) {
                DBG("nl_recvmsgs() ret: %d (%s)", ret, nl_geterror(-ret));
-               nl_cb_put(cb);
-               return ret;
+               goto out;
        }
 
        while (!results.done)
@@ -665,12 +694,18 @@ static int __netconfig_request_netlink_scan(struct nl_sock *socket,
                DBG("scan aborted");
                return 1;
        }
-       DBG("Scan done");
 
+out:
        /** Release memory */
+       nlmsg_free(ssids);
        nlmsg_free(msg);
        nl_cb_put(cb);
        nl_socket_drop_membership(socket, mcid);
+
+       if (ret < 0)
+               return ret;
+
+       DBG("Scan done");
        return 0;
 }
 
@@ -712,6 +747,8 @@ fail:
 
 static int __netconfig_initialize_nl_msg(netconfig_nl_global *global)
 {
+       int rv;
+
        if (global == NULL) {
                DBG("Invalid parameter.");
                return -EINVAL;
@@ -725,7 +762,12 @@ static int __netconfig_initialize_nl_msg(netconfig_nl_global *global)
 
        /* Set command into message */
        genlmsg_put(global->msg, 0, 0, global->id, 0, NLM_F_DUMP, NL80211_CMD_GET_SCAN, 0);
-       nla_put_u32(global->msg, NL80211_ATTR_IFINDEX, global->if_index);
+       rv = nla_put_u32(global->msg, NL80211_ATTR_IFINDEX, global->if_index);
+       if (rv < 0) {
+               DBG("Failed to add integer attribute to netlink message.");
+               nlmsg_free(global->msg);
+               return rv;
+       }
        nl_socket_modify_cb(global->socket, NL_CB_VALID, NL_CB_CUSTOM, __netconfig_netlink_scan_cb, NULL);
 
        return 0;