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

Side by Side Diff: webrtc/base/helpers.cc

Issue 2119003002: Don't silently ignore RNG failures when creating strings / numbers. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Feedback from Matt. Created 4 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 | « webrtc/base/helpers.h ('k') | 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 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2004 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 10 matching lines...) Expand all
21 #if defined(WEBRTC_WIN) 21 #if defined(WEBRTC_WIN)
22 #define WIN32_LEAN_AND_MEAN 22 #define WIN32_LEAN_AND_MEAN
23 #include <windows.h> 23 #include <windows.h>
24 #include <ntsecapi.h> 24 #include <ntsecapi.h>
25 #endif // WEBRTC_WIN 25 #endif // WEBRTC_WIN
26 #endif // else 26 #endif // else
27 #endif // FEATURE_ENABLED_SSL 27 #endif // FEATURE_ENABLED_SSL
28 28
29 #include "webrtc/base/base64.h" 29 #include "webrtc/base/base64.h"
30 #include "webrtc/base/basictypes.h" 30 #include "webrtc/base/basictypes.h"
31 #include "webrtc/base/checks.h"
31 #include "webrtc/base/logging.h" 32 #include "webrtc/base/logging.h"
32 #include "webrtc/base/timeutils.h" 33 #include "webrtc/base/timeutils.h"
33 34
34 // Protect against max macro inclusion. 35 // Protect against max macro inclusion.
35 #undef max 36 #undef max
36 37
37 namespace rtc { 38 namespace rtc {
38 39
39 // Base class for RNG implementations. 40 // Base class for RNG implementations.
40 class RandomGenerator { 41 class RandomGenerator {
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 bool InitRandom(const char* seed, size_t len) { 209 bool InitRandom(const char* seed, size_t len) {
209 if (!Rng().Init(seed, len)) { 210 if (!Rng().Init(seed, len)) {
210 LOG(LS_ERROR) << "Failed to init random generator!"; 211 LOG(LS_ERROR) << "Failed to init random generator!";
211 return false; 212 return false;
212 } 213 }
213 return true; 214 return true;
214 } 215 }
215 216
216 std::string CreateRandomString(size_t len) { 217 std::string CreateRandomString(size_t len) {
217 std::string str; 218 std::string str;
218 CreateRandomString(len, &str); 219 RTC_CHECK(CreateRandomString(len, &str));
219 return str; 220 return str;
220 } 221 }
221 222
222 bool CreateRandomString(size_t len, 223 bool CreateRandomString(size_t len,
223 const char* table, int table_size, 224 const char* table, int table_size,
224 std::string* str) { 225 std::string* str) {
225 str->clear(); 226 str->clear();
226 std::unique_ptr<uint8_t[]> bytes(new uint8_t[len]); 227 std::unique_ptr<uint8_t[]> bytes(new uint8_t[len]);
227 if (!Rng().Generate(bytes.get(), len)) { 228 if (!Rng().Generate(bytes.get(), len)) {
228 LOG(LS_ERROR) << "Failed to generate random string!"; 229 LOG(LS_ERROR) << "Failed to generate random string!";
(...skipping 15 matching lines...) Expand all
244 return CreateRandomString(len, table.c_str(), 245 return CreateRandomString(len, table.c_str(),
245 static_cast<int>(table.size()), str); 246 static_cast<int>(table.size()), str);
246 } 247 }
247 248
248 // Version 4 UUID is of the form: 249 // Version 4 UUID is of the form:
249 // xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx 250 // xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
250 // Where 'x' is a hex digit, and 'y' is 8, 9, a or b. 251 // Where 'x' is a hex digit, and 'y' is 8, 9, a or b.
251 std::string CreateRandomUuid() { 252 std::string CreateRandomUuid() {
252 std::string str; 253 std::string str;
253 std::unique_ptr<uint8_t[]> bytes(new uint8_t[31]); 254 std::unique_ptr<uint8_t[]> bytes(new uint8_t[31]);
254 if (!Rng().Generate(bytes.get(), 31)) { 255 RTC_CHECK(Rng().Generate(bytes.get(), 31));
255 LOG(LS_ERROR) << "Failed to generate random string!";
256 return str;
257 }
258 str.reserve(36); 256 str.reserve(36);
259 for (size_t i = 0; i < 8; ++i) { 257 for (size_t i = 0; i < 8; ++i) {
260 str.push_back(kHex[bytes[i] % 16]); 258 str.push_back(kHex[bytes[i] % 16]);
261 } 259 }
262 str.push_back('-'); 260 str.push_back('-');
263 for (size_t i = 8; i < 12; ++i) { 261 for (size_t i = 8; i < 12; ++i) {
264 str.push_back(kHex[bytes[i] % 16]); 262 str.push_back(kHex[bytes[i] % 16]);
265 } 263 }
266 str.push_back('-'); 264 str.push_back('-');
267 str.push_back('4'); 265 str.push_back('4');
268 for (size_t i = 12; i < 15; ++i) { 266 for (size_t i = 12; i < 15; ++i) {
269 str.push_back(kHex[bytes[i] % 16]); 267 str.push_back(kHex[bytes[i] % 16]);
270 } 268 }
271 str.push_back('-'); 269 str.push_back('-');
272 str.push_back(kUuidDigit17[bytes[15] % 4]); 270 str.push_back(kUuidDigit17[bytes[15] % 4]);
273 for (size_t i = 16; i < 19; ++i) { 271 for (size_t i = 16; i < 19; ++i) {
274 str.push_back(kHex[bytes[i] % 16]); 272 str.push_back(kHex[bytes[i] % 16]);
275 } 273 }
276 str.push_back('-'); 274 str.push_back('-');
277 for (size_t i = 19; i < 31; ++i) { 275 for (size_t i = 19; i < 31; ++i) {
278 str.push_back(kHex[bytes[i] % 16]); 276 str.push_back(kHex[bytes[i] % 16]);
279 } 277 }
280 return str; 278 return str;
281 } 279 }
282 280
283 uint32_t CreateRandomId() { 281 uint32_t CreateRandomId() {
284 uint32_t id; 282 uint32_t id;
285 if (!Rng().Generate(&id, sizeof(id))) { 283 RTC_CHECK(Rng().Generate(&id, sizeof(id)));
286 LOG(LS_ERROR) << "Failed to generate random id!";
287 }
288 return id; 284 return id;
289 } 285 }
290 286
291 uint64_t CreateRandomId64() { 287 uint64_t CreateRandomId64() {
292 return static_cast<uint64_t>(CreateRandomId()) << 32 | CreateRandomId(); 288 return static_cast<uint64_t>(CreateRandomId()) << 32 | CreateRandomId();
293 } 289 }
294 290
295 uint32_t CreateRandomNonZeroId() { 291 uint32_t CreateRandomNonZeroId() {
296 uint32_t id; 292 uint32_t id;
297 do { 293 do {
298 id = CreateRandomId(); 294 id = CreateRandomId();
299 } while (id == 0); 295 } while (id == 0);
300 return id; 296 return id;
301 } 297 }
302 298
303 double CreateRandomDouble() { 299 double CreateRandomDouble() {
304 return CreateRandomId() / (std::numeric_limits<uint32_t>::max() + 300 return CreateRandomId() / (std::numeric_limits<uint32_t>::max() +
305 std::numeric_limits<double>::epsilon()); 301 std::numeric_limits<double>::epsilon());
306 } 302 }
307 303
308 } // namespace rtc 304 } // namespace rtc
OLDNEW
« no previous file with comments | « webrtc/base/helpers.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698