From 2e05f0dd4f3b33a0fdeb9a1c72a53cf14851e734 Mon Sep 17 00:00:00 2001 From: Jakub Pawlowski Date: Wed, 16 Aug 2017 06:41:02 -0700 Subject: [PATCH] Cleanup RawAddress usage Use RawAddress::kLength instead of sizeof(RawAddress). When copying value using memcpy, use "->address" instead of direct instance address. Bug: 64726342 Change-Id: Iac7e5674f7e32b53162ab734c2251e65e9d4554c --- btif/include/btif_debug_conn.h | 2 +- btif/src/btif_core.cc | 4 ++-- btif/src/btif_debug_conn.cc | 4 ++-- btif/src/btif_hf_client.cc | 1 - btif/src/btif_storage.cc | 16 +++++++--------- device/include/interop.h | 2 +- device/src/interop.cc | 2 +- hci/src/hci_packet_parser.cc | 2 +- service/hal/fake_bluetooth_interface.cc | 4 ++-- 9 files changed, 17 insertions(+), 20 deletions(-) diff --git a/btif/include/btif_debug_conn.h b/btif/include/btif_debug_conn.h index 532a09ab0..6ee374ee1 100644 --- a/btif/include/btif_debug_conn.h +++ b/btif/include/btif_debug_conn.h @@ -28,7 +28,7 @@ typedef enum { } btif_debug_conn_state_t; // Report a connection state change -void btif_debug_conn_state(const RawAddress bda, +void btif_debug_conn_state(const RawAddress& bda, const btif_debug_conn_state_t state, const tGATT_DISCONN_REASON disconnect_reason); diff --git a/btif/src/btif_core.cc b/btif/src/btif_core.cc index 1ac2ae62f..8cc043056 100644 --- a/btif/src/btif_core.cc +++ b/btif/src/btif_core.cc @@ -890,7 +890,7 @@ bt_status_t btif_get_adapter_property(bt_property_type_t type) { (type != BT_PROPERTY_BDNAME)) return BT_STATUS_NOT_READY; - memset(&(req.read_req.bd_addr), 0, sizeof(RawAddress)); + req.read_req.bd_addr = RawAddress::kEmpty; req.read_req.type = type; return btif_transfer_context(execute_storage_request, @@ -1063,7 +1063,7 @@ bt_status_t btif_set_remote_device_property(RawAddress* remote_addr, if (!btif_is_enabled()) return BT_STATUS_NOT_READY; - memcpy(&(req.write_req.bd_addr), remote_addr, sizeof(RawAddress)); + req.write_req.bd_addr = *remote_addr; memcpy(&(req.write_req.prop), property, sizeof(bt_property_t)); return btif_transfer_context(execute_storage_remote_request, diff --git a/btif/src/btif_debug_conn.cc b/btif/src/btif_debug_conn.cc index 869782f73..9252a023f 100644 --- a/btif/src/btif_debug_conn.cc +++ b/btif/src/btif_debug_conn.cc @@ -63,7 +63,7 @@ static void next_event() { if (current_event == NUM_CONNECTION_EVENTS) current_event = 0; } -void btif_debug_conn_state(const RawAddress bda, +void btif_debug_conn_state(const RawAddress& bda, const btif_debug_conn_state_t state, const tGATT_DISCONN_REASON disconnect_reason) { next_event(); @@ -72,7 +72,7 @@ void btif_debug_conn_state(const RawAddress bda, evt->ts = time_gettimeofday_us(); evt->state = state; evt->disconnect_reason = disconnect_reason; - memcpy(&evt->bda, &bda, sizeof(RawAddress)); + evt->bda = bda; } void btif_debug_conn_dump(int fd) { diff --git a/btif/src/btif_hf_client.cc b/btif/src/btif_hf_client.cc index 80a8d298a..16af6ff7e 100644 --- a/btif/src/btif_hf_client.cc +++ b/btif/src/btif_hf_client.cc @@ -249,7 +249,6 @@ btif_hf_client_cb_t* btif_hf_client_allocate_cb() { return NULL; } - /***************************************************************************** * * btif hf api functions (no context switch) diff --git a/btif/src/btif_storage.cc b/btif/src/btif_storage.cc index 63a2c84a7..2c1aed337 100644 --- a/btif/src/btif_storage.cc +++ b/btif/src/btif_storage.cc @@ -456,8 +456,7 @@ static bt_status_t btif_in_fetch_bonded_devices( } } bt_linkkey_file_found = true; - memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], - &bd_addr, sizeof(RawAddress)); + p_bonded_devices->devices[p_bonded_devices->num_devices++] = bd_addr; } else { bt_linkkey_file_found = false; } @@ -557,13 +556,13 @@ bt_status_t btif_storage_get_adapter_property(bt_property_t* property) { LOG_ERROR(LOG_TAG, "%s: Controller not ready! Unable to return Bluetooth Address", __func__); - memset(bd_addr, 0, sizeof(RawAddress)); + *bd_addr = RawAddress::kEmpty; return BT_STATUS_FAIL; } else { LOG_ERROR(LOG_TAG, "%s: Controller ready!", __func__); - memcpy(bd_addr, controller->get_address(), sizeof(RawAddress)); + *bd_addr = *controller->get_address(); } - property->len = sizeof(RawAddress); + property->len = RawAddress::kLength; return BT_STATUS_SUCCESS; } else if (property->type == BT_PROPERTY_ADAPTER_BONDED_DEVICES) { btif_bonded_devices_t bonded_devices; @@ -576,7 +575,7 @@ bt_status_t btif_storage_get_adapter_property(bt_property_t* property) { __func__, bonded_devices.num_devices); if (bonded_devices.num_devices > 0) { - property->len = bonded_devices.num_devices * sizeof(RawAddress); + property->len = bonded_devices.num_devices * RawAddress::kLength; memcpy(property->val, bonded_devices.devices, property->len); } @@ -852,7 +851,7 @@ bt_status_t btif_storage_load_bonded_devices(void) { bonded_devices.num_devices * sizeof(RawAddress); adapter_props[num_props].val = devices_list; for (i = 0; i < bonded_devices.num_devices; i++) { - memcpy(devices_list + i, &bonded_devices.devices[i], sizeof(RawAddress)); + devices_list[i] = bonded_devices.devices[i]; } num_props++; @@ -1166,8 +1165,7 @@ static bt_status_t btif_in_fetch_bonded_ble_device( // Fill in the bonded devices if (device_added) { - memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], - &bd_addr, sizeof(RawAddress)); + p_bonded_devices->devices[p_bonded_devices->num_devices++] = bd_addr; btif_gatts_add_bonded_dev_from_nv(bd_addr); } diff --git a/device/include/interop.h b/device/include/interop.h index cb7faed0c..96a3b9a18 100644 --- a/device/include/interop.h +++ b/device/include/interop.h @@ -110,7 +110,7 @@ bool interop_match_name(const interop_feature_t feature, const char* name); // Add a dynamic interop database entry for a device matching the first |length| // bytes of |addr|, implementing the workaround identified by |feature|. // |addr| may not be null. -// |length| must be greater than 0 and less than sizeof(RawAddress). +// |length| must be greater than 0 and less than RawAddress::kLength. // As |interop_feature_t| is not exposed in the public API, feature must be a // valid integer representing an option in the enum. void interop_database_add(const uint16_t feature, const RawAddress* addr, diff --git a/device/src/interop.cc b/device/src/interop.cc index 78eb99724..7b1fe59c9 100644 --- a/device/src/interop.cc +++ b/device/src/interop.cc @@ -80,7 +80,7 @@ void interop_database_add(const uint16_t feature, const RawAddress* addr, size_t length) { CHECK(addr); CHECK(length > 0); - CHECK(length < sizeof(RawAddress)); + CHECK(length < RawAddress::kLength); interop_addr_entry_t* entry = static_cast( osi_calloc(sizeof(interop_addr_entry_t))); diff --git a/hci/src/hci_packet_parser.cc b/hci/src/hci_packet_parser.cc index 38f20119c..e7ace27fc 100644 --- a/hci/src/hci_packet_parser.cc +++ b/hci/src/hci_packet_parser.cc @@ -88,7 +88,7 @@ static void parse_read_local_supported_codecs_response( static void parse_read_bd_addr_response(BT_HDR* response, RawAddress* address_ptr) { uint8_t* stream = read_command_complete_header( - response, HCI_READ_BD_ADDR, sizeof(RawAddress) /* bytes after */); + response, HCI_READ_BD_ADDR, RawAddress::kLength /* bytes after */); CHECK(stream != NULL); STREAM_TO_BDADDR(*address_ptr, stream); diff --git a/service/hal/fake_bluetooth_interface.cc b/service/hal/fake_bluetooth_interface.cc index 0ce61196d..45fdca080 100644 --- a/service/hal/fake_bluetooth_interface.cc +++ b/service/hal/fake_bluetooth_interface.cc @@ -115,8 +115,8 @@ void FakeBluetoothInterface::NotifyAdapterNamePropertyChanged( void FakeBluetoothInterface::NotifyAdapterAddressPropertyChanged( const RawAddress* address) { bt_property_t property; - property.len = sizeof(RawAddress); - property.val = (void*)address; + property.len = RawAddress::kLength; + property.val = (void*)address->address; property.type = BT_PROPERTY_BDADDR; NotifyAdapterPropertiesChanged(1, &property); -- 2.11.0