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}
diff --git a/webrtc/base/helpers.cc b/webrtc/base/helpers.cc
index be71fee..9674eb9 100644
--- a/webrtc/base/helpers.cc
+++ b/webrtc/base/helpers.cc
@@ -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 @@
std::string CreateRandomString(size_t len) {
std::string str;
- CreateRandomString(len, &str);
+ RTC_CHECK(CreateRandomString(len, &str));
return str;
}
@@ -245,10 +246,7 @@
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 @@
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;
}
diff --git a/webrtc/base/helpers.h b/webrtc/base/helpers.h
index c75dba5..2e72dfb 100644
--- a/webrtc/base/helpers.h
+++ b/webrtc/base/helpers.h
@@ -25,7 +25,6 @@
// 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.