diff --git a/internal/platform/bluetooth_utils.cc b/internal/platform/bluetooth_utils.cc index cb512acf..b5d26c72 100644 --- a/internal/platform/bluetooth_utils.cc +++ b/internal/platform/bluetooth_utils.cc @@ -14,6 +14,8 @@ #include "internal/platform/bluetooth_utils.h" +#include + #include "absl/strings/escaping.h" #include "absl/strings/str_format.h" @@ -39,17 +41,15 @@ std::string BluetoothUtils::ToString(const ByteArray& bluetooth_mac_address) { ByteArray BluetoothUtils::FromString(absl::string_view bluetooth_mac_address) { std::string bt_mac_address(bluetooth_mac_address); - // Remove the colon delimiters. bt_mac_address.erase( std::remove(bt_mac_address.begin(), bt_mac_address.end(), ':'), bt_mac_address.end()); - // If the bluetooth mac address is invalid (wrong size), return a null byte - // array. - if (bt_mac_address.length() != kBluetoothMacAddressLength * 2) { + if (bt_mac_address.length() != kBluetoothMacAddressLength * 2 || + !std::all_of(bt_mac_address.begin(), bt_mac_address.end(), + absl::ascii_isxdigit)) { return ByteArray(); } - // Convert to bytes. If MAC Address bytes are unset, return a null byte array. auto bt_mac_address_string(absl::HexStringToBytes(bt_mac_address)); auto bt_mac_address_bytes = ByteArray(bt_mac_address_string.data(), bt_mac_address_string.size()); diff --git a/internal/platform/bluetooth_utils_test.cc b/internal/platform/bluetooth_utils_test.cc index f2fbab63..82ffea5e 100644 --- a/internal/platform/bluetooth_utils_test.cc +++ b/internal/platform/bluetooth_utils_test.cc @@ -82,6 +82,10 @@ TEST(BluetoothUtilsTest, InvalidStringReturnsEmptyByteArray) { std::string bad_bt_mac_address_4 = "BLUETOOTHCHIP"; bytes_result = BluetoothUtils::FromString(bad_bt_mac_address_4); EXPECT_TRUE(bytes_result.Empty()); + + std::string bad_bt_mac_address_5 = "G1:F2:F3:F4:F5:F6"; + bytes_result = BluetoothUtils::FromString(bad_bt_mac_address_5); + EXPECT_TRUE(bytes_result.Empty()); } TEST(BluetoothUtilsTest, FromNumber) {