summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRahul Sabnis <rahulsabnis@google.com>2019-09-27 17:57:56 -0700
committerandroid-build-merger <android-build-merger@google.com>2019-09-27 17:57:56 -0700
commit92b5515436fbbdbd9dc0e6acaa7cbc7aa5bb804e (patch)
treeb931fe60dd4b4fc06b4fe6d0b94101ab7aa5611b
parent325d1539ba82a57bf8c7998d4813f9ff7940bfa5 (diff)
parentca7cd4593ac80a9433ec0692d1f0a8875a984231 (diff)
downloadandroid_system_bt-92b5515436fbbdbd9dc0e6acaa7cbc7aa5bb804e.tar.gz
android_system_bt-92b5515436fbbdbd9dc0e6acaa7cbc7aa5bb804e.tar.bz2
android_system_bt-92b5515436fbbdbd9dc0e6acaa7cbc7aa5bb804e.zip
Merge "Use memcpy instead of casting to convert device_class to int" into oc-dev
am: ca7cd4593a Change-Id: I2b44586f43a2249034a286338c2ba68b78339cb7
-rw-r--r--btcore/src/device_class.cc4
-rw-r--r--btcore/test/device_class_test.cc39
2 files changed, 27 insertions, 16 deletions
diff --git a/btcore/src/device_class.cc b/btcore/src/device_class.cc
index d421b6dd4..1630d462a 100644
--- a/btcore/src/device_class.cc
+++ b/btcore/src/device_class.cc
@@ -87,7 +87,9 @@ void device_class_from_int(bt_device_class_t* dc, int data) {
int device_class_to_int(const bt_device_class_t* dc) {
CHECK(dc != NULL);
// Careful with endianess.
- return (int)(le32toh(*(int*)dc) & 0xffffff);
+ int val = 0;
+ memcpy(&val, dc, sizeof(*dc));
+ return static_cast<int>(le32toh(val) & 0xffffff);
}
bool device_class_equals(const bt_device_class_t* p1,
diff --git a/btcore/test/device_class_test.cc b/btcore/test/device_class_test.cc
index 434f9ca4c..82a68f00d 100644
--- a/btcore/test/device_class_test.cc
+++ b/btcore/test/device_class_test.cc
@@ -22,9 +22,6 @@
#include "btcore/include/device_class.h"
-// Device Class is 3 bytes.
-static const int DC_MASK = 0xffffff;
-
::testing::AssertionResult check_bitfield(const char* m_expr,
const char* n_expr, int m, int n) {
if (m == n) return ::testing::AssertionSuccess();
@@ -84,8 +81,9 @@ TEST_F(DeviceClassTest, to_stream) {
int rc = device_class_to_stream(&dc, dc_stream1, sizeof(dc_stream1));
EXPECT_EQ(3, rc);
- uint32_t* val = (uint32_t*)&dc;
- EXPECT_PRED_FORMAT2(check_bitfield, 0x00000000, *val & 0xffffff);
+ uint32_t val = 0;
+ memcpy(&val, &dc, sizeof(dc));
+ EXPECT_PRED_FORMAT2(check_bitfield, 0x00000000, val);
EXPECT_PRED_FORMAT2(check_bitfield, 0x00, dc_stream1[0]);
EXPECT_PRED_FORMAT2(check_bitfield, 0x00, dc_stream1[1]);
@@ -101,8 +99,9 @@ TEST_F(DeviceClassTest, to_stream) {
int rc = device_class_to_stream(&dc, dc_stream1, sizeof(dc_stream1));
EXPECT_EQ(3, rc);
- uint32_t* val = (uint32_t*)&dc;
- EXPECT_PRED_FORMAT2(check_bitfield, 0x00aa55aa, *val & 0xffffff);
+ uint32_t val = 0;
+ memcpy(&val, &dc, sizeof(dc));
+ EXPECT_PRED_FORMAT2(check_bitfield, 0x00aa55aa, val);
EXPECT_PRED_FORMAT2(check_bitfield, 0xaa, dc_stream1[0]);
EXPECT_PRED_FORMAT2(check_bitfield, 0x55, dc_stream1[1]);
@@ -118,8 +117,9 @@ TEST_F(DeviceClassTest, to_stream) {
int rc = device_class_to_stream(&dc, dc_stream1, sizeof(dc_stream1));
EXPECT_EQ(3, rc);
- uint32_t* val = (uint32_t*)&dc;
- EXPECT_PRED_FORMAT2(check_bitfield, 0x452301, *val & 0xffffff);
+ uint32_t val = 0;
+ memcpy(&val, &dc, sizeof(dc));
+ EXPECT_PRED_FORMAT2(check_bitfield, 0x452301, val);
EXPECT_PRED_FORMAT2(check_bitfield, 0x01, dc_stream1[0]);
EXPECT_PRED_FORMAT2(check_bitfield, 0x23, dc_stream1[1]);
@@ -131,24 +131,33 @@ TEST_F(DeviceClassTest, limited_discoverable_mode) {
uint8_t dc_stream[] = {0x00, 0x00, 0x00};
bt_device_class_t dc;
device_class_from_stream(&dc, dc_stream);
- uint32_t* test = (uint32_t*)&dc;
+ uint32_t test = 0;
+ memcpy(&test, &dc, sizeof(dc));
EXPECT_FALSE(device_class_get_limited(&dc));
- EXPECT_EQ((unsigned)0x00000000, *test & DC_MASK);
+ EXPECT_EQ((unsigned)0x00000000, test);
device_class_set_limited(&dc, true);
+ test = 0;
+ memcpy(&test, &dc, sizeof(dc));
EXPECT_TRUE(device_class_get_limited(&dc));
- EXPECT_EQ((unsigned)0x00002000, *test & DC_MASK);
+ EXPECT_EQ((unsigned)0x00002000, test);
device_class_set_limited(&dc, false);
+ test = 0;
+ memcpy(&test, &dc, sizeof(dc));
EXPECT_FALSE(device_class_get_limited(&dc));
- EXPECT_EQ((unsigned)0x00000000, *test & DC_MASK);
+ EXPECT_EQ((unsigned)0x00000000, test);
device_class_set_limited(&dc, true);
- EXPECT_PRED_FORMAT2(check_bitfield, 0x00002000, *test & DC_MASK);
+ test = 0;
+ memcpy(&test, &dc, sizeof(dc));
+ EXPECT_PRED_FORMAT2(check_bitfield, 0x00002000, test);
device_class_set_limited(&dc, false);
- EXPECT_PRED_FORMAT2(check_bitfield, 0x00000000, *test & DC_MASK);
+ test = 0;
+ memcpy(&test, &dc, sizeof(dc));
+ EXPECT_PRED_FORMAT2(check_bitfield, 0x00000000, test);
}
TEST_F(DeviceClassTest, equals) {