Commit fdc62f8e authored by Sergey Ulanov's avatar Sergey Ulanov Committed by Commit Bot

Fix Fuchsia implementation of base::RandBytes() to allow empty buffer.

base::RandBytes() may be called with output_length=0 and all
implementations supported this case, except Fuchsia. On Fuchsia the
function was crashing due to a failed CHECK(). This was breaking
MemoryDataSourceTest.EmptySource test.

Bug: 737802
Change-Id: Ib9ec20a82dd6966a146f0c66936e232c061466a5
Reviewed-on: https://chromium-review.googlesource.com/595012
Commit-Queue: Nico Weber <thakis@chromium.org>
Reviewed-by: default avatarNico Weber <thakis@chromium.org>
Reviewed-by: default avatarScott Graham <scottmg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#491086}
parent aeed1e4d
......@@ -21,7 +21,7 @@ uint64_t RandUint64() {
void RandBytes(void* output, size_t output_length) {
size_t remaining = output_length;
unsigned char* cur = reinterpret_cast<unsigned char*>(output);
do {
while (remaining > 0) {
// The syscall has a maximum number of bytes that can be read at once.
size_t read_len =
std::min(remaining, static_cast<size_t>(MX_CPRNG_DRAW_MAX_LEN));
......@@ -33,7 +33,7 @@ void RandBytes(void* output, size_t output_length) {
CHECK(remaining >= actual);
remaining -= actual;
cur += actual;
} while (remaining > 0);
}
}
} // namespace base
......@@ -52,6 +52,11 @@ TEST(RandUtilTest, RandBytes) {
EXPECT_GT(std::unique(buffer, buffer + buffer_size) - buffer, 25);
}
// Verify that calling base::RandBytes with an empty buffer doesn't fail.
TEST(RandUtilTest, RandBytes0) {
base::RandBytes(nullptr, 0);
}
TEST(RandUtilTest, RandBytesAsString) {
std::string random_string = base::RandBytesAsString(1);
EXPECT_EQ(1U, random_string.size());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment