Commit 31ce57aa authored by Bailey Forrest's avatar Bailey Forrest Committed by Commit Bot

[chromecast] Fix bugs in bluetooth_util::ParseUuid

BUG=internal b/78234633
TEST=cast_bluetooth_unittests

Change-Id: I2672aee77de583bf8a4292dd9fabc89e3cdc1476
Reviewed-on: https://chromium-review.googlesource.com/1017778Reviewed-by: default avatarStephen Lanham <slan@chromium.org>
Commit-Queue: Bailey Forrest <bcf@chromium.org>
Cr-Commit-Position: refs/heads/master@{#551869}
parent 2ee39b4d
...@@ -15,6 +15,7 @@ namespace { ...@@ -15,6 +15,7 @@ namespace {
const int kMacAddrStrLen = 17; const int kMacAddrStrLen = 17;
const int kUuid16bitLen = 4; const int kUuid16bitLen = 4;
const int kUuidHexNumChars = 32; const int kUuidHexNumChars = 32;
const int kUuidNumDashes = 4;
const char kFmtUuid[] = const char kFmtUuid[] =
"%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx" "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx"
...@@ -59,18 +60,27 @@ std::string UuidToString(const bluetooth_v2_shlib::Uuid& uuid) { ...@@ -59,18 +60,27 @@ std::string UuidToString(const bluetooth_v2_shlib::Uuid& uuid) {
} }
bool ParseUuid(const std::string& str, bluetooth_v2_shlib::Uuid* uuid) { bool ParseUuid(const std::string& str, bluetooth_v2_shlib::Uuid* uuid) {
// sscanf will incorrectly succeed if all characters except the last one are if (str.empty()) {
// hex digits.
if (str.empty() || !std::isxdigit(str.back())) {
return false; return false;
} }
for (const auto& c : str) {
if (c != '-' && !std::isxdigit(c)) {
return false;
}
}
// Check for 16-bit UUID // Check for 16-bit UUID
if (str.size() == kUuid16bitLen) { if (str.size() == kUuid16bitLen) {
*uuid = kUuidBase; *uuid = kUuidBase;
return sscanf(str.c_str(), "%02hhx%02hhx", &(*uuid)[2], &(*uuid)[3]) == 2; return sscanf(str.c_str(), "%02hhx%02hhx", &(*uuid)[2], &(*uuid)[3]) == 2;
} }
if (str.size() > kUuidHexNumChars &&
str.size() != kUuidHexNumChars + kUuidNumDashes) {
return false;
}
std::string no_dashes = str; std::string no_dashes = str;
no_dashes.erase(std::remove(no_dashes.begin(), no_dashes.end(), '-'), no_dashes.erase(std::remove(no_dashes.begin(), no_dashes.end(), '-'),
no_dashes.end()); no_dashes.end());
......
...@@ -43,6 +43,10 @@ TEST(BluetoothUtilTest, AddrStringConversion) { ...@@ -43,6 +43,10 @@ TEST(BluetoothUtilTest, AddrStringConversion) {
TEST(BluetoothUtilTest, UuidStringConversion) { TEST(BluetoothUtilTest, UuidStringConversion) {
const char kBadUuid1[] = "hello"; const char kBadUuid1[] = "hello";
const char kBadUuid2[] = "a822c885-af02-c780-9d4d-bd9a1fa06d9z"; const char kBadUuid2[] = "a822c885-af02-c780-9d4d-bd9a1fa06d9z";
const char kBadUuid3[] = "00000000-0000-0000-0000-0x0000000000";
const char kBadUuid4[] = "123e-567-e89b-12d3-a456-426655440000";
const char kBadUuid5[] = "123e456--e89b-12d3-a456-426655440000";
const char kBadUuid6[] = "123e4567--e89b-12d3-a456-426655440000";
const char kUuid1[] = "123e4567-e89b-12d3-a456-426655440000"; const char kUuid1[] = "123e4567-e89b-12d3-a456-426655440000";
const char kUuid2[] = "123E4567-E89B-12D3-A456-426655440000"; const char kUuid2[] = "123E4567-E89B-12D3-A456-426655440000";
...@@ -64,6 +68,10 @@ TEST(BluetoothUtilTest, UuidStringConversion) { ...@@ -64,6 +68,10 @@ TEST(BluetoothUtilTest, UuidStringConversion) {
bluetooth_v2_shlib::Uuid uuid; bluetooth_v2_shlib::Uuid uuid;
EXPECT_FALSE(ParseUuid(kBadUuid1, &uuid)); EXPECT_FALSE(ParseUuid(kBadUuid1, &uuid));
EXPECT_FALSE(ParseUuid(kBadUuid2, &uuid)); EXPECT_FALSE(ParseUuid(kBadUuid2, &uuid));
EXPECT_FALSE(ParseUuid(kBadUuid3, &uuid));
EXPECT_FALSE(ParseUuid(kBadUuid4, &uuid));
EXPECT_FALSE(ParseUuid(kBadUuid5, &uuid));
EXPECT_FALSE(ParseUuid(kBadUuid6, &uuid));
EXPECT_TRUE(ParseUuid(kUuid1, &uuid)); EXPECT_TRUE(ParseUuid(kUuid1, &uuid));
EXPECT_EQ(kGoodBytes1, uuid); EXPECT_EQ(kGoodBytes1, uuid);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment