Don't silently ignore RNG failures when creating strings / numbers.

RTC_CHECK in functions that return the random value.

BUG=webrtc:6072

Review-Url: https://codereview.webrtc.org/2119003002
Cr-Commit-Position: refs/heads/master@{#13682}
This commit is contained in:
jbauch 2016-08-08 16:33:06 -07:00 committed by Commit bot
parent d3b953db30
commit 912f46a348
2 changed files with 4 additions and 9 deletions

View File

@ -28,6 +28,7 @@
#include "webrtc/base/base64.h"
#include "webrtc/base/basictypes.h"
#include "webrtc/base/checks.h"
#include "webrtc/base/logging.h"
#include "webrtc/base/timeutils.h"
@ -202,7 +203,7 @@ bool InitRandom(const char* seed, size_t len) {
std::string CreateRandomString(size_t len) {
std::string str;
CreateRandomString(len, &str);
RTC_CHECK(CreateRandomString(len, &str));
return str;
}
@ -245,10 +246,7 @@ bool CreateRandomData(size_t length, std::string* data) {
std::string CreateRandomUuid() {
std::string str;
std::unique_ptr<uint8_t[]> bytes(new uint8_t[31]);
if (!Rng().Generate(bytes.get(), 31)) {
LOG(LS_ERROR) << "Failed to generate random string!";
return str;
}
RTC_CHECK(Rng().Generate(bytes.get(), 31));
str.reserve(36);
for (size_t i = 0; i < 8; ++i) {
str.push_back(kHex[bytes[i] % 16]);
@ -276,9 +274,7 @@ std::string CreateRandomUuid() {
uint32_t CreateRandomId() {
uint32_t id;
if (!Rng().Generate(&id, sizeof(id))) {
LOG(LS_ERROR) << "Failed to generate random id!";
}
RTC_CHECK(Rng().Generate(&id, sizeof(id)));
return id;
}

View File

@ -25,7 +25,6 @@ bool InitRandom(const char* seed, size_t len);
// Generates a (cryptographically) random string of the given length.
// We generate base64 values so that they will be printable.
// WARNING: could silently fail. Use the version below instead.
std::string CreateRandomString(size_t length);
// Generates a (cryptographically) random string of the given length.