Skip to content

Commit ad6c18d

Browse files
eriksandgrenrlubos
authored andcommitted
[nrf fromtree] Bluetooth: Host: Fix issue where uninitialized value was used
This change makes sure that when a call to `bt_id_set_scan_own_addr` is sucessful, i.e., the return value is 0, the `own_addr_type` will be set by the `bt_id_set_scan_own_addr`. Not setting the `own_addr_type` in a successful call to `bt_id_set_scan_own_addr` causes, for example, the `start_le_scan_ext` method in `scan.c` to use an uninitialized `own_addr_type`. Eventually this results in an unexpected failure further down in `start_le_scan_ext`, when sending HCI command to controller with an uninitialized `own_addr_type`. Signed-off-by: Erik Sandgren <[email protected]> (cherry picked from commit 5f59b35)
1 parent 5065d3b commit ad6c18d

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

subsys/bluetooth/host/id.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,6 +1785,13 @@ int bt_id_set_scan_own_addr(bool active_scan, uint8_t *own_addr_type)
17851785
}
17861786

17871787
if (IS_ENABLED(CONFIG_BT_PRIVACY)) {
1788+
1789+
if (BT_FEAT_LE_PRIVACY(bt_dev.le.features)) {
1790+
*own_addr_type = BT_HCI_OWN_ADDR_RPA_OR_RANDOM;
1791+
} else {
1792+
*own_addr_type = BT_ADDR_LE_RANDOM;
1793+
}
1794+
17881795
err = bt_id_set_private_addr(BT_ID_DEFAULT);
17891796
if (err == -EACCES && (atomic_test_bit(bt_dev.flags, BT_DEV_SCANNING) ||
17901797
atomic_test_bit(bt_dev.flags, BT_DEV_INITIATING))) {
@@ -1794,12 +1801,6 @@ int bt_id_set_scan_own_addr(bool active_scan, uint8_t *own_addr_type)
17941801
} else if (err) {
17951802
return err;
17961803
}
1797-
1798-
if (BT_FEAT_LE_PRIVACY(bt_dev.le.features)) {
1799-
*own_addr_type = BT_HCI_OWN_ADDR_RPA_OR_RANDOM;
1800-
} else {
1801-
*own_addr_type = BT_ADDR_LE_RANDOM;
1802-
}
18031804
} else {
18041805
*own_addr_type = bt_dev.id_addr[0].type;
18051806

tests/bluetooth/host/id/bt_id_set_scan_own_addr/src/test_suite_invalid_inputs.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ ZTEST(bt_id_set_scan_own_addr_invalid_inputs, test_set_random_address_fails)
110110
* Expected behaviour:
111111
* - bt_id_set_scan_own_addr() fails and returns the same error code returned by
112112
* bt_id_set_private_addr()
113-
* - Address type reference isn't set
114113
*/
115114
ZTEST(bt_id_set_scan_own_addr_invalid_inputs, test_bt_id_set_private_addr_fails_privacy_enabled)
116115
{
@@ -131,6 +130,4 @@ ZTEST(bt_id_set_scan_own_addr_invalid_inputs, test_bt_id_set_private_addr_fails_
131130
#endif
132131

133132
zassert_true(err < 0, "Unexpected error code '%d' was returned", err);
134-
zassert_true(own_addr_type == BT_ADDR_LE_ANONYMOUS,
135-
"Address type reference was unexpectedly modified");
136133
}

0 commit comments

Comments
 (0)