Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(97)

Side by Side Diff: webrtc/modules/rtp_rtcp/source/byte_io_unittest.cc

Issue 1739753002: Fix ubsan warning in byteio_unittest (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 13 matching lines...) Expand all
24 enum { kAlignments = sizeof(uint64_t) - 1 }; 24 enum { kAlignments = sizeof(uint64_t) - 1 };
25 25
26 // Method to create a test value that is not the same when byte reversed. 26 // Method to create a test value that is not the same when byte reversed.
27 template <typename T> 27 template <typename T>
28 T CreateTestValue(bool negative, uint8_t num_bytes) { 28 T CreateTestValue(bool negative, uint8_t num_bytes) {
29 T val = 0; 29 T val = 0;
30 for (uint8_t i = 0; i != num_bytes; ++i) { 30 for (uint8_t i = 0; i != num_bytes; ++i) {
31 val = (val << 8) + (negative ? (0xFF - i) : (i + 1)); 31 val = (val << 8) + (negative ? (0xFF - i) : (i + 1));
32 } 32 }
33 if (negative && std::numeric_limits<T>::is_signed) { 33 if (negative && std::numeric_limits<T>::is_signed) {
34 val |= static_cast<T>(-1) << (8 * num_bytes); 34 T mask = static_cast<T>(-1);
35 const T neg_byte = static_cast<T>(0xFF);
36 for (int i = 0; i < num_bytes; ++i) {
37 mask &= ~(neg_byte << i);
38 }
stefan-webrtc 2016/02/26 09:30:34 Add a comment about what the mask you are construc
sprang_webrtc 2016/02/26 10:08:25 Done.
39 val |= mask;
35 } 40 }
36 return val; 41 return val;
37 } 42 }
38 43
39 // Populate byte buffer with value, in big endian format. 44 // Populate byte buffer with value, in big endian format.
40 template <typename T> 45 template <typename T>
41 void PopulateTestData(uint8_t* data, T value, int num_bytes, bool bigendian) { 46 void PopulateTestData(uint8_t* data, T value, int num_bytes, bool bigendian) {
42 if (bigendian) { 47 if (bigendian) {
43 for (int i = 0; i < num_bytes; ++i) { 48 for (int i = 0; i < num_bytes; ++i) {
44 data[i] = (value >> ((num_bytes - i - 1) * 8)) & 0xFF; 49 data[i] = (value >> ((num_bytes - i - 1) * 8)) & 0xFF;
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 sizeof(int16_t)>(true); 138 sizeof(int16_t)>(true);
134 TestWrite<int16_t, ByteWriter<int16_t>::WriteBigEndian, 139 TestWrite<int16_t, ByteWriter<int16_t>::WriteBigEndian,
135 sizeof(int16_t)>(true); 140 sizeof(int16_t)>(true);
136 } 141 }
137 142
138 TEST_F(ByteIoTest, Test24SBitBigEndian) { 143 TEST_F(ByteIoTest, Test24SBitBigEndian) {
139 TestRead<int32_t, ByteReader<int32_t, 3>::ReadBigEndian, 3>(true); 144 TestRead<int32_t, ByteReader<int32_t, 3>::ReadBigEndian, 3>(true);
140 TestWrite<int32_t, ByteWriter<int32_t, 3>::WriteBigEndian, 3>(true); 145 TestWrite<int32_t, ByteWriter<int32_t, 3>::WriteBigEndian, 3>(true);
141 } 146 }
142 147
143 // Disabled for UBSan: https://bugs.chromium.org/p/webrtc/issues/detail?id=5490 148 TEST_F(ByteIoTest, Test32SBitBigEndian) {
144 #ifdef UNDEFINED_SANITIZER
145 #define MAYBE_Test32SBitBigEndian DISABLED_Test32SBitBigEndian
146 #else
147 #define MAYBE_Test32SBitBigEndian Test32SBitBigEndian
148 #endif
149 TEST_F(ByteIoTest, MAYBE_Test32SBitBigEndian) {
150 TestRead<int32_t, ByteReader<int32_t>::ReadBigEndian, 149 TestRead<int32_t, ByteReader<int32_t>::ReadBigEndian,
151 sizeof(int32_t)>(true); 150 sizeof(int32_t)>(true);
152 TestWrite<int32_t, ByteWriter<int32_t>::WriteBigEndian, 151 TestWrite<int32_t, ByteWriter<int32_t>::WriteBigEndian,
153 sizeof(int32_t)>(true); 152 sizeof(int32_t)>(true);
154 } 153 }
155 154
156 // Disabled for UBSan: https://bugs.chromium.org/p/webrtc/issues/detail?id=5490 155 TEST_F(ByteIoTest, Test64SBitBigEndian) {
157 #ifdef UNDEFINED_SANITIZER
158 #define MAYBE_Test64SBitBigEndian DISABLED_Test64SBitBigEndian
159 #else
160 #define MAYBE_Test64SBitBigEndian Test64SBitBigEndian
161 #endif
162 TEST_F(ByteIoTest, MAYBE_Test64SBitBigEndian) {
163 TestRead<int64_t, ByteReader<int64_t>::ReadBigEndian, 156 TestRead<int64_t, ByteReader<int64_t>::ReadBigEndian,
164 sizeof(int64_t)>(true); 157 sizeof(int64_t)>(true);
165 TestWrite<int64_t, ByteWriter<int64_t>::WriteBigEndian, 158 TestWrite<int64_t, ByteWriter<int64_t>::WriteBigEndian,
166 sizeof(int64_t)>(true); 159 sizeof(int64_t)>(true);
167 } 160 }
168 161
169 TEST_F(ByteIoTest, Test16UBitLittleEndian) { 162 TEST_F(ByteIoTest, Test16UBitLittleEndian) {
170 TestRead<uint16_t, ByteReader<uint16_t>::ReadLittleEndian, 163 TestRead<uint16_t, ByteReader<uint16_t>::ReadLittleEndian,
171 sizeof(uint16_t)>(false); 164 sizeof(uint16_t)>(false);
172 TestWrite<uint16_t, ByteWriter<uint16_t>::WriteLittleEndian, 165 TestWrite<uint16_t, ByteWriter<uint16_t>::WriteLittleEndian,
(...skipping 24 matching lines...) Expand all
197 sizeof(int16_t)>(false); 190 sizeof(int16_t)>(false);
198 TestWrite<int16_t, ByteWriter<int16_t>::WriteLittleEndian, 191 TestWrite<int16_t, ByteWriter<int16_t>::WriteLittleEndian,
199 sizeof(int16_t)>(false); 192 sizeof(int16_t)>(false);
200 } 193 }
201 194
202 TEST_F(ByteIoTest, Test24SBitLittleEndian) { 195 TEST_F(ByteIoTest, Test24SBitLittleEndian) {
203 TestRead<int32_t, ByteReader<int32_t, 3>::ReadLittleEndian, 3>(false); 196 TestRead<int32_t, ByteReader<int32_t, 3>::ReadLittleEndian, 3>(false);
204 TestWrite<int32_t, ByteWriter<int32_t, 3>::WriteLittleEndian, 3>(false); 197 TestWrite<int32_t, ByteWriter<int32_t, 3>::WriteLittleEndian, 3>(false);
205 } 198 }
206 199
207 // Disabled for UBSan: https://bugs.chromium.org/p/webrtc/issues/detail?id=5490 200 TEST_F(ByteIoTest, Test32SBitLittleEndian) {
208 #ifdef UNDEFINED_SANITIZER
209 #define MAYBE_Test32SBitLittleEndian DISABLED_Test32SBitLittleEndian
210 #else
211 #define MAYBE_Test32SBitLittleEndian Test32SBitLittleEndian
212 #endif
213 TEST_F(ByteIoTest, MAYBE_Test32SBitLittleEndian) {
214 TestRead<int32_t, ByteReader<int32_t>::ReadLittleEndian, 201 TestRead<int32_t, ByteReader<int32_t>::ReadLittleEndian,
215 sizeof(int32_t)>(false); 202 sizeof(int32_t)>(false);
216 TestWrite<int32_t, ByteWriter<int32_t>::WriteLittleEndian, 203 TestWrite<int32_t, ByteWriter<int32_t>::WriteLittleEndian,
217 sizeof(int32_t)>(false); 204 sizeof(int32_t)>(false);
218 } 205 }
219 206
220 // Disabled for UBSan: https://bugs.chromium.org/p/webrtc/issues/detail?id=5490 207 TEST_F(ByteIoTest, Test64SBitLittleEndian) {
221 #ifdef UNDEFINED_SANITIZER
222 #define MAYBE_Test64SBitLittleEndian DISABLED_Test64SBitLittleEndian
223 #else
224 #define MAYBE_Test64SBitLittleEndian Test64SBitLittleEndian
225 #endif
226 TEST_F(ByteIoTest, MAYBE_Test64SBitLittleEndian) {
227 TestRead<int64_t, ByteReader<int64_t>::ReadLittleEndian, 208 TestRead<int64_t, ByteReader<int64_t>::ReadLittleEndian,
228 sizeof(int64_t)>(false); 209 sizeof(int64_t)>(false);
229 TestWrite<int64_t, ByteWriter<int64_t>::WriteLittleEndian, 210 TestWrite<int64_t, ByteWriter<int64_t>::WriteLittleEndian,
230 sizeof(int64_t)>(false); 211 sizeof(int64_t)>(false);
231 } 212 }
232 213
233 // Sets up a fixed byte array and converts N bytes from the array into a 214 // Sets up a fixed byte array and converts N bytes from the array into a
234 // uint64_t. Verifies the value with hard-coded reference. 215 // uint64_t. Verifies the value with hard-coded reference.
235 TEST(ByteIo, SanityCheckFixedByteArrayUnsignedReadBigEndian) { 216 TEST(ByteIo, SanityCheckFixedByteArrayUnsignedReadBigEndian) {
236 uint8_t data[8] = {0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88}; 217 uint8_t data[8] = {0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88};
(...skipping 26 matching lines...) Expand all
263 EXPECT_EQ(static_cast<uint64_t>(0xBBCCDDEEFF), value); 244 EXPECT_EQ(static_cast<uint64_t>(0xBBCCDDEEFF), value);
264 value = ByteReader<uint64_t, 6>::ReadLittleEndian(data); 245 value = ByteReader<uint64_t, 6>::ReadLittleEndian(data);
265 EXPECT_EQ(static_cast<uint64_t>(0xAABBCCDDEEFF), value); 246 EXPECT_EQ(static_cast<uint64_t>(0xAABBCCDDEEFF), value);
266 value = ByteReader<uint64_t, 7>::ReadLittleEndian(data); 247 value = ByteReader<uint64_t, 7>::ReadLittleEndian(data);
267 EXPECT_EQ(static_cast<uint64_t>(0x99AABBCCDDEEFF), value); 248 EXPECT_EQ(static_cast<uint64_t>(0x99AABBCCDDEEFF), value);
268 value = ByteReader<uint64_t, 8>::ReadLittleEndian(data); 249 value = ByteReader<uint64_t, 8>::ReadLittleEndian(data);
269 EXPECT_EQ(static_cast<uint64_t>(0x8899AABBCCDDEEFF), value); 250 EXPECT_EQ(static_cast<uint64_t>(0x8899AABBCCDDEEFF), value);
270 } 251 }
271 } // namespace 252 } // namespace
272 } // namespace webrtc 253 } // namespace webrtc
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698