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

Side by Side Diff: webrtc/modules/rtp_rtcp/source/byte_io.h

Issue 1226993003: Refactor byte_io to avoid potentially undefined behavior (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 5 years, 5 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) 2013 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2013 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 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 } 81 }
82 82
83 // If number of bytes is less than native data type (eg 24 bit, in int32_t), 83 // If number of bytes is less than native data type (eg 24 bit, in int32_t),
84 // and the most significant bit of the actual data is set, we must sign 84 // and the most significant bit of the actual data is set, we must sign
85 // extend the remaining byte(s) with ones so that the correct negative 85 // extend the remaining byte(s) with ones so that the correct negative
86 // number is retained. 86 // number is retained.
87 // Ex: 0x810A0B -> 0xFF810A0B, but 0x710A0B -> 0x00710A0B 87 // Ex: 0x810A0B -> 0xFF810A0B, but 0x710A0B -> 0x00710A0B
88 static T SignExtend(const T val) { 88 static T SignExtend(const T val) {
89 uint8_t msb = static_cast<uint8_t>(val >> ((B - 1) * 8)); 89 uint8_t msb = static_cast<uint8_t>(val >> ((B - 1) * 8));
90 if (msb & 0x80) { 90 if (msb & 0x80) {
91 // Sign extension is -1 (all ones) shifted left B bytes. 91 T used_bits_mask = (1 << (B * 8)) - 1;
92 // The "B % sizeof(T)"-part is there to avoid compiler warning for 92 return ~used_bits_mask | val;
stefan-webrtc 2015/07/08 12:01:50 I think this deserves a comment. :)
sprang_webrtc 2015/07/08 12:57:03 Done.
93 // shifting the whole size of the data type.
94 T sign_extend = (sizeof(T) == B ? 0 :
95 (static_cast<T>(-1L) << ((B % sizeof(T)) * 8)));
96
97 return val | sign_extend;
98 } 93 }
99 return val; 94 return val;
100 } 95 }
101 }; 96 };
102 97
103 // Class for writing integers to a sequence of bytes 98 // Class for writing integers to a sequence of bytes
104 // T = type of integer, B = bytes to write 99 // T = type of integer, B = bytes to write
105 template<typename T, unsigned int B = sizeof(T)> 100 template<typename T, unsigned int B = sizeof(T)>
106 class ByteWriter { 101 class ByteWriter {
107 public: 102 public:
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 data[4] = val >> 32; 224 data[4] = val >> 32;
230 data[5] = val >> 40; 225 data[5] = val >> 40;
231 data[6] = val >> 48; 226 data[6] = val >> 48;
232 data[7] = val >> 56; 227 data[7] = val >> 56;
233 } 228 }
234 }; 229 };
235 230
236 } // namespace webrtc 231 } // namespace webrtc
237 232
238 #endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_BYTE_IO_H_ 233 #endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_BYTE_IO_H_
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