mesh: Remove redundant checks when adding a net key
authorInga Stotland <inga.stotland@intel.com>
Wed, 12 Jun 2019 15:04:37 +0000 (08:04 -0700)
committerAnupam Roy <anupam.r@samsung.com>
Tue, 17 Dec 2019 15:12:00 +0000 (20:42 +0530)
This patch cleans up redundant checks in add_key() and mesh_net_set_key():
no need to check the result of l_queue_push_tail() and no need to check
if subnet is valid after it was successfully created.

Change-Id: I5c9b0a7483d70db955b57d9c2d4587b5996934fe
Signed-off-by: Anupam Roy <anupam.r@samsung.com>
mesh/net.c

index 7ffdb98..771ae5d 100644 (file)
@@ -990,12 +990,13 @@ static struct mesh_subnet *add_key(struct mesh_net *net, uint16_t idx,
                return NULL;
        }
 
-       if (!create_secure_beacon(net, subnet, subnet->snb.beacon + 1) ||
-                               !l_queue_push_tail(net->subnets, subnet)) {
+       if (!create_secure_beacon(net, subnet, subnet->snb.beacon + 1)) {
                subnet_free(subnet);
                return NULL;
        }
 
+       l_queue_push_tail(net->subnets, subnet);
+
        return subnet;
 }
 
@@ -3016,11 +3017,6 @@ bool mesh_net_set_key(struct mesh_net *net, uint16_t idx, const uint8_t *key,
 {
        struct mesh_subnet *subnet;
 
-       subnet = l_queue_find(net->subnets, match_key_index,
-                                                       L_UINT_TO_PTR(idx));
-       if (subnet)
-               return false;
-
        /* Current key must be always present */
        if (!key)
                return false;
@@ -3029,12 +3025,13 @@ bool mesh_net_set_key(struct mesh_net *net, uint16_t idx, const uint8_t *key,
        if (phase != KEY_REFRESH_PHASE_NONE && !new_key)
                return false;
 
-       subnet = add_key(net, idx, key);
-       if (!subnet)
-               return false;
-
+       /* Check if the subnet with the specified index already exists */
        subnet = l_queue_find(net->subnets, match_key_index,
                                                        L_UINT_TO_PTR(idx));
+       if (subnet)
+               return false;
+
+       subnet = add_key(net, idx, key);
        if (!subnet)
                return false;