0

Use array reference to guarantee correctness

An array reference allows the creation of a function that will accept
arrays of the desired size. This allows a static_assert with teeth, and
the removal of a magic number. No behavior or correctness change happens
but this makes the code correct by construction.

Change-Id: I6146cb74da391d7970950f705be0a4c82b7d61e8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1815258
Commit-Queue: Scott Violet <sky@chromium.org>
Auto-Submit: Bruce Dawson <brucedawson@chromium.org>
Reviewed-by: Scott Violet <sky@chromium.org>
Cr-Commit-Position: refs/heads/master@{#698528}
This commit is contained in:
Bruce Dawson
2019-09-20 17:21:00 +00:00
committed by Commit Bot
parent 46fb4f2c84
commit f875c2fbce

@ -63,11 +63,10 @@ namespace {
// Fills the given salt structure with some quasi-random values
// It is not necessary to generate a cryptographically strong random string,
// only that it be reasonably different for different users.
void GenerateSalt(uint8_t salt[LINK_SALT_LENGTH]) {
static_assert(LINK_SALT_LENGTH == 8,
"This code assumes the length of the salt");
void GenerateSalt(uint8_t (&salt)[LINK_SALT_LENGTH]) {
uint64_t randval = base::RandUint64();
memcpy(salt, &randval, 8);
static_assert(sizeof(salt) == sizeof(randval), "Salt size mismatch");
memcpy(salt, &randval, sizeof(salt));
}
// Opens file on a background thread to not block UI thread.