
These are replaced with the bounds-safe byte conversion functions in base::numerics. Most of the sites doing so were trying to parse a byte stream, and we have helpers for that now in base::numerics::U*FromLittleEndian, so make use of those and turn code into using spans. Similarly, constructing a byte stream in little endian from non-byte-sized integers can use base::numerics::U*ToLittleEndian to be explicit. Some code was doing invalid casts from bytes to non-byte-sized integers which cause UB if the pointers are not correctly aligned, so these are replaced with the use of byte spans. There were also casts from structs to arrays of their fields, which is UB, so these are also adjusted. memcpy() is replaced with bounds-checked span::copy_from(). Bug: 40284755 Change-Id: I84594ff8ab4a2037e9d33bb6fe0d8ba60d5d7413 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5370942 Reviewed-by: Mark Mentovai <mark@chromium.org> Reviewed-by: Peter Boström <pbos@chromium.org> Commit-Queue: danakj <danakj@chromium.org> Cr-Commit-Position: refs/heads/main@{#1276469}
76 lines
1.8 KiB
C++
76 lines
1.8 KiB
C++
// Copyright 2016 The Chromium Authors
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#include "base/sys_byteorder.h"
|
|
|
|
#include <stdint.h>
|
|
|
|
#include "build/build_config.h"
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
|
|
|
namespace {
|
|
|
|
const uint16_t k16BitTestData = 0xaabb;
|
|
const uint16_t k16BitSwappedTestData = 0xbbaa;
|
|
const uint32_t k32BitTestData = 0xaabbccdd;
|
|
const uint32_t k32BitSwappedTestData = 0xddccbbaa;
|
|
const uint64_t k64BitTestData = 0xaabbccdd44332211;
|
|
const uint64_t k64BitSwappedTestData = 0x11223344ddccbbaa;
|
|
|
|
} // namespace
|
|
|
|
TEST(ByteOrderTest, NetToHost16) {
|
|
uint16_t h = base::NetToHost16(k16BitTestData);
|
|
#if defined(ARCH_CPU_LITTLE_ENDIAN)
|
|
EXPECT_EQ(k16BitSwappedTestData, h);
|
|
#else
|
|
EXPECT_EQ(k16BitTestData, h);
|
|
#endif
|
|
}
|
|
|
|
TEST(ByteOrderTest, NetToHost32) {
|
|
uint32_t h = base::NetToHost32(k32BitTestData);
|
|
#if defined(ARCH_CPU_LITTLE_ENDIAN)
|
|
EXPECT_EQ(k32BitSwappedTestData, h);
|
|
#else
|
|
EXPECT_EQ(k32BitTestData, h);
|
|
#endif
|
|
}
|
|
|
|
TEST(ByteOrderTest, NetToHost64) {
|
|
uint64_t h = base::NetToHost64(k64BitTestData);
|
|
#if defined(ARCH_CPU_LITTLE_ENDIAN)
|
|
EXPECT_EQ(k64BitSwappedTestData, h);
|
|
#else
|
|
EXPECT_EQ(k64BitTestData, h);
|
|
#endif
|
|
}
|
|
|
|
TEST(ByteOrderTest, HostToNet16) {
|
|
uint16_t n = base::HostToNet16(k16BitTestData);
|
|
#if defined(ARCH_CPU_LITTLE_ENDIAN)
|
|
EXPECT_EQ(k16BitSwappedTestData, n);
|
|
#else
|
|
EXPECT_EQ(k16BitTestData, n);
|
|
#endif
|
|
}
|
|
|
|
TEST(ByteOrderTest, HostToNet32) {
|
|
uint32_t n = base::HostToNet32(k32BitTestData);
|
|
#if defined(ARCH_CPU_LITTLE_ENDIAN)
|
|
EXPECT_EQ(k32BitSwappedTestData, n);
|
|
#else
|
|
EXPECT_EQ(k32BitTestData, n);
|
|
#endif
|
|
}
|
|
|
|
TEST(ByteOrderTest, HostToNet64) {
|
|
uint64_t n = base::HostToNet64(k64BitTestData);
|
|
#if defined(ARCH_CPU_LITTLE_ENDIAN)
|
|
EXPECT_EQ(k64BitSwappedTestData, n);
|
|
#else
|
|
EXPECT_EQ(k64BitTestData, n);
|
|
#endif
|
|
}
|