mesh: Allow deleting non-existing net key
authorMichał Lowas-Rzechonek <michal.lowas-rzechonek@silvair.com>
Thu, 6 Aug 2020 14:21:08 +0000 (16:21 +0200)
committerAbhay Agarwal <ay.agarwal@samsung.com>
Mon, 28 Dec 2020 06:20:04 +0000 (11:50 +0530)
When deleting a non-existing key, first check that it doesn't exist (and
return success), then verify it's not the last key we have.

Change-Id: I70f686bb3ab175270d6d4cb4059d7408ff34ec49
Signed-off-by: anuj.bhumiya <anuj.bhumiya@samsung.com>
mesh/net.c

index 6d340b6..7a1dadd 100644 (file)
@@ -798,15 +798,15 @@ int mesh_net_del_key(struct mesh_net *net, uint16_t idx)
        if (!net)
                return MESH_STATUS_UNSPECIFIED_ERROR;
 
-       /* Cannot remove primary key */
-       if (l_queue_length(net->subnets) <= 1)
-               return MESH_STATUS_CANNOT_REMOVE;
-
        subnet = l_queue_find(net->subnets, match_key_index,
                                                        L_UINT_TO_PTR(idx));
        if (!subnet)
                return MESH_STATUS_SUCCESS;
 
+       /* Cannot remove primary key */
+       if (l_queue_length(net->subnets) <= 1)
+               return MESH_STATUS_CANNOT_REMOVE;
+
        /* Delete associated app keys */
        appkey_delete_bound_keys(net, idx);