• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

system/bt


Commit MetaInfo

Revision50bf4cf1bf756a75bfd9672946561d9c5b647b7e (tree)
Time2019-09-17 08:31:45
AuthorRahul Sabnis <rahulsabnis@goog...>
CommiterRahul Sabnis

Log Message

Use memcpy instead of casting to convert device_class to int

Bug: 140152619
Test: atest net_test_btcoreclear
Change-Id: Iee71ce35576e438317841d41a81fda6a87e1984a
Merged-In: Iee71ce35576e438317841d41a81fda6a87e1984a
(cherry picked from commit ec75f1efb6b9be4933225a4b724e7a3ef5e3d70b)

Change Summary

Incremental Difference

--- a/btcore/src/device_class.cc
+++ b/btcore/src/device_class.cc
@@ -88,7 +88,9 @@ void device_class_from_int(bt_device_class_t* dc, int data) {
8888 int device_class_to_int(const bt_device_class_t* dc) {
8989 CHECK(dc != NULL);
9090 // Careful with endianess.
91- return (int)(le32toh(*(int*)dc) & 0xffffff);
91+ int val = 0;
92+ memcpy(&val, dc, sizeof(*dc));
93+ return static_cast<int>(le32toh(val) & 0xffffff);
9294 }
9395
9496 bool device_class_equals(const bt_device_class_t* p1,
--- a/btcore/test/device_class_test.cc
+++ b/btcore/test/device_class_test.cc
@@ -22,9 +22,6 @@
2222
2323 #include "btcore/include/device_class.h"
2424
25-// Device Class is 3 bytes.
26-static const int DC_MASK = 0xffffff;
27-
2825 ::testing::AssertionResult check_bitfield(const char* m_expr,
2926 const char* n_expr, int m, int n) {
3027 if (m == n) return ::testing::AssertionSuccess();
@@ -84,8 +81,9 @@ TEST_F(DeviceClassTest, to_stream) {
8481 int rc = device_class_to_stream(&dc, dc_stream1, sizeof(dc_stream1));
8582 EXPECT_EQ(3, rc);
8683
87- uint32_t* val = (uint32_t*)&dc;
88- EXPECT_PRED_FORMAT2(check_bitfield, 0x00000000, *val & 0xffffff);
84+ uint32_t val = 0;
85+ memcpy(&val, &dc, sizeof(dc));
86+ EXPECT_PRED_FORMAT2(check_bitfield, 0x00000000, val);
8987
9088 EXPECT_PRED_FORMAT2(check_bitfield, 0x00, dc_stream1[0]);
9189 EXPECT_PRED_FORMAT2(check_bitfield, 0x00, dc_stream1[1]);
@@ -101,8 +99,9 @@ TEST_F(DeviceClassTest, to_stream) {
10199
102100 int rc = device_class_to_stream(&dc, dc_stream1, sizeof(dc_stream1));
103101 EXPECT_EQ(3, rc);
104- uint32_t* val = (uint32_t*)&dc;
105- EXPECT_PRED_FORMAT2(check_bitfield, 0x00aa55aa, *val & 0xffffff);
102+ uint32_t val = 0;
103+ memcpy(&val, &dc, sizeof(dc));
104+ EXPECT_PRED_FORMAT2(check_bitfield, 0x00aa55aa, val);
106105
107106 EXPECT_PRED_FORMAT2(check_bitfield, 0xaa, dc_stream1[0]);
108107 EXPECT_PRED_FORMAT2(check_bitfield, 0x55, dc_stream1[1]);
@@ -118,8 +117,9 @@ TEST_F(DeviceClassTest, to_stream) {
118117
119118 int rc = device_class_to_stream(&dc, dc_stream1, sizeof(dc_stream1));
120119 EXPECT_EQ(3, rc);
121- uint32_t* val = (uint32_t*)&dc;
122- EXPECT_PRED_FORMAT2(check_bitfield, 0x452301, *val & 0xffffff);
120+ uint32_t val = 0;
121+ memcpy(&val, &dc, sizeof(dc));
122+ EXPECT_PRED_FORMAT2(check_bitfield, 0x452301, val);
123123
124124 EXPECT_PRED_FORMAT2(check_bitfield, 0x01, dc_stream1[0]);
125125 EXPECT_PRED_FORMAT2(check_bitfield, 0x23, dc_stream1[1]);
@@ -131,24 +131,33 @@ TEST_F(DeviceClassTest, limited_discoverable_mode) {
131131 uint8_t dc_stream[] = {0x00, 0x00, 0x00};
132132 bt_device_class_t dc;
133133 device_class_from_stream(&dc, dc_stream);
134- uint32_t* test = (uint32_t*)&dc;
134+ uint32_t test = 0;
135+ memcpy(&test, &dc, sizeof(dc));
135136
136137 EXPECT_FALSE(device_class_get_limited(&dc));
137- EXPECT_EQ((unsigned)0x00000000, *test & DC_MASK);
138+ EXPECT_EQ((unsigned)0x00000000, test);
138139
139140 device_class_set_limited(&dc, true);
141+ test = 0;
142+ memcpy(&test, &dc, sizeof(dc));
140143 EXPECT_TRUE(device_class_get_limited(&dc));
141- EXPECT_EQ((unsigned)0x00002000, *test & DC_MASK);
144+ EXPECT_EQ((unsigned)0x00002000, test);
142145
143146 device_class_set_limited(&dc, false);
147+ test = 0;
148+ memcpy(&test, &dc, sizeof(dc));
144149 EXPECT_FALSE(device_class_get_limited(&dc));
145- EXPECT_EQ((unsigned)0x00000000, *test & DC_MASK);
150+ EXPECT_EQ((unsigned)0x00000000, test);
146151
147152 device_class_set_limited(&dc, true);
148- EXPECT_PRED_FORMAT2(check_bitfield, 0x00002000, *test & DC_MASK);
153+ test = 0;
154+ memcpy(&test, &dc, sizeof(dc));
155+ EXPECT_PRED_FORMAT2(check_bitfield, 0x00002000, test);
149156
150157 device_class_set_limited(&dc, false);
151- EXPECT_PRED_FORMAT2(check_bitfield, 0x00000000, *test & DC_MASK);
158+ test = 0;
159+ memcpy(&test, &dc, sizeof(dc));
160+ EXPECT_PRED_FORMAT2(check_bitfield, 0x00000000, test);
152161 }
153162
154163 TEST_F(DeviceClassTest, equals) {