diff --git a/util/misc/uuid.cc b/util/misc/uuid.cc index 15870709..3013d7b2 100644 --- a/util/misc/uuid.cc +++ b/util/misc/uuid.cc @@ -23,13 +23,15 @@ #include #include +#include #include #include +#include "base/containers/span.h" +#include "base/numerics/byte_conversions.h" #include "base/rand_util.h" #include "base/strings/stringprintf.h" #include "base/strings/utf_string_conversions.h" -#include "base/sys_byteorder.h" #include "build/build_config.h" #if BUILDFLAG(IS_APPLE) @@ -53,11 +55,15 @@ void UUID::InitializeToZero() { memset(this, 0, sizeof(*this)); } -void UUID::InitializeFromBytes(const uint8_t* bytes) { - memcpy(this, bytes, sizeof(*this)); - data_1 = base::NetToHost32(data_1); - data_2 = base::NetToHost16(data_2); - data_3 = base::NetToHost16(data_3); +void UUID::InitializeFromBytes(const uint8_t* bytes_ptr) { + // TODO(crbug.com/40284755): This span construction is unsound. The caller + // should provide a span instead of an unbounded pointer. + base::span bytes(bytes_ptr, sizeof(UUID)); + data_1 = base::numerics::U32FromBigEndian(bytes.subspan<0u, 4u>()); + data_2 = base::numerics::U16FromBigEndian(bytes.subspan<4u, 2u>()); + data_3 = base::numerics::U16FromBigEndian(bytes.subspan<6u, 2u>()); + std::ranges::copy(bytes.subspan<8u, 2u>(), data_4); + std::ranges::copy(bytes.subspan<10u, 6u>(), data_5); } bool UUID::InitializeFromString(const base::StringPiece& string) {