Skip to content

Commit

Permalink
Bluetooth: host: Fix bt_keys_get_addr()
Browse files Browse the repository at this point in the history
Change fixes issue causing multiple keys creation for given address
and identity.

Fixes: #16478

Signed-off-by: Marek Pieta <Marek.Pieta@nordicsemi.no>
  • Loading branch information
MarekPieta authored and carlescufi committed May 30, 2019
1 parent f032729 commit c8be3e8
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions subsys/bluetooth/host/keys.c
Expand Up @@ -35,6 +35,7 @@ struct bt_keys *bt_keys_get_addr(u8_t id, const bt_addr_le_t *addr)
{
struct bt_keys *keys;
int i;
size_t first_free_slot = ARRAY_SIZE(key_pool);

BT_DBG("%s", bt_addr_le_str(addr));

Expand All @@ -45,14 +46,19 @@ struct bt_keys *bt_keys_get_addr(u8_t id, const bt_addr_le_t *addr)
return keys;
}

if (!bt_addr_le_cmp(&keys->addr, BT_ADDR_LE_ANY)) {
keys->id = id;
bt_addr_le_copy(&keys->addr, addr);
BT_DBG("created %p for %s", keys, bt_addr_le_str(addr));
return keys;
if (first_free_slot == ARRAY_SIZE(key_pool) &&
!bt_addr_le_cmp(&keys->addr, BT_ADDR_LE_ANY)) {
first_free_slot = i;
}
}

if (first_free_slot < ARRAY_SIZE(key_pool)) {
keys = &key_pool[first_free_slot];
keys->id = id;
bt_addr_le_copy(&keys->addr, addr);
BT_DBG("created %p for %s", keys, bt_addr_le_str(addr));
return keys;
}
BT_DBG("unable to create keys for %s", bt_addr_le_str(addr));

return NULL;
Expand Down

0 comments on commit c8be3e8

Please sign in to comment.