system/bt
Revision | 50bf4cf1bf756a75bfd9672946561d9c5b647b7e (tree) |
---|---|
Time | 2019-09-17 08:31:45 |
Author | Rahul Sabnis <rahulsabnis@goog...> |
Commiter | Rahul Sabnis |
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)
@@ -88,7 +88,9 @@ void device_class_from_int(bt_device_class_t* dc, int data) { | ||
88 | 88 | int device_class_to_int(const bt_device_class_t* dc) { |
89 | 89 | CHECK(dc != NULL); |
90 | 90 | // 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); | |
92 | 94 | } |
93 | 95 | |
94 | 96 | bool device_class_equals(const bt_device_class_t* p1, |
@@ -22,9 +22,6 @@ | ||
22 | 22 | |
23 | 23 | #include "btcore/include/device_class.h" |
24 | 24 | |
25 | -// Device Class is 3 bytes. | |
26 | -static const int DC_MASK = 0xffffff; | |
27 | - | |
28 | 25 | ::testing::AssertionResult check_bitfield(const char* m_expr, |
29 | 26 | const char* n_expr, int m, int n) { |
30 | 27 | if (m == n) return ::testing::AssertionSuccess(); |
@@ -84,8 +81,9 @@ TEST_F(DeviceClassTest, to_stream) { | ||
84 | 81 | int rc = device_class_to_stream(&dc, dc_stream1, sizeof(dc_stream1)); |
85 | 82 | EXPECT_EQ(3, rc); |
86 | 83 | |
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); | |
89 | 87 | |
90 | 88 | EXPECT_PRED_FORMAT2(check_bitfield, 0x00, dc_stream1[0]); |
91 | 89 | EXPECT_PRED_FORMAT2(check_bitfield, 0x00, dc_stream1[1]); |
@@ -101,8 +99,9 @@ TEST_F(DeviceClassTest, to_stream) { | ||
101 | 99 | |
102 | 100 | int rc = device_class_to_stream(&dc, dc_stream1, sizeof(dc_stream1)); |
103 | 101 | 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); | |
106 | 105 | |
107 | 106 | EXPECT_PRED_FORMAT2(check_bitfield, 0xaa, dc_stream1[0]); |
108 | 107 | EXPECT_PRED_FORMAT2(check_bitfield, 0x55, dc_stream1[1]); |
@@ -118,8 +117,9 @@ TEST_F(DeviceClassTest, to_stream) { | ||
118 | 117 | |
119 | 118 | int rc = device_class_to_stream(&dc, dc_stream1, sizeof(dc_stream1)); |
120 | 119 | 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); | |
123 | 123 | |
124 | 124 | EXPECT_PRED_FORMAT2(check_bitfield, 0x01, dc_stream1[0]); |
125 | 125 | EXPECT_PRED_FORMAT2(check_bitfield, 0x23, dc_stream1[1]); |
@@ -131,24 +131,33 @@ TEST_F(DeviceClassTest, limited_discoverable_mode) { | ||
131 | 131 | uint8_t dc_stream[] = {0x00, 0x00, 0x00}; |
132 | 132 | bt_device_class_t dc; |
133 | 133 | 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)); | |
135 | 136 | |
136 | 137 | EXPECT_FALSE(device_class_get_limited(&dc)); |
137 | - EXPECT_EQ((unsigned)0x00000000, *test & DC_MASK); | |
138 | + EXPECT_EQ((unsigned)0x00000000, test); | |
138 | 139 | |
139 | 140 | device_class_set_limited(&dc, true); |
141 | + test = 0; | |
142 | + memcpy(&test, &dc, sizeof(dc)); | |
140 | 143 | EXPECT_TRUE(device_class_get_limited(&dc)); |
141 | - EXPECT_EQ((unsigned)0x00002000, *test & DC_MASK); | |
144 | + EXPECT_EQ((unsigned)0x00002000, test); | |
142 | 145 | |
143 | 146 | device_class_set_limited(&dc, false); |
147 | + test = 0; | |
148 | + memcpy(&test, &dc, sizeof(dc)); | |
144 | 149 | EXPECT_FALSE(device_class_get_limited(&dc)); |
145 | - EXPECT_EQ((unsigned)0x00000000, *test & DC_MASK); | |
150 | + EXPECT_EQ((unsigned)0x00000000, test); | |
146 | 151 | |
147 | 152 | 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); | |
149 | 156 | |
150 | 157 | 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); | |
152 | 161 | } |
153 | 162 | |
154 | 163 | TEST_F(DeviceClassTest, equals) { |