Skip to content

Commit

Permalink
Convert some "unsafe" make_span() calls to span_from_ref()
Browse files Browse the repository at this point in the history
This will facilitate removing unsafe_buffer usage in the future.

Fully automated change.

Change-Id: I0d5f0369fe002a61d6da83db807612cf6a51c064
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5758178
Owners-Override: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1336912}
  • Loading branch information
tsepez authored and Chromium LUCI CQ committed Aug 3, 2024
1 parent 426e4b5 commit b075e19
Show file tree
Hide file tree
Showing 38 changed files with 131 additions and 131 deletions.
8 changes: 4 additions & 4 deletions base/files/file_descriptor_watcher_posix_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class FileDescriptorWatcherTest
void WriteByte() {
constexpr char kByte = '!';
ASSERT_TRUE(WriteFileDescriptor(write_file_descriptor(),
as_bytes(make_span(&kByte, 1u))));
as_bytes(span_from_ref(kByte))));
}

void ReadByte() {
Expand All @@ -184,13 +184,13 @@ class FileDescriptorWatcherTest
EXPECT_TRUE(thread_checker_.CalledOnValidThread());

char buffer;
ASSERT_TRUE(ReadFromFD(read_file_descriptor(), make_span(&buffer, 1u)));
ASSERT_TRUE(ReadFromFD(read_file_descriptor(), span_from_ref(buffer)));
}

void WriteByte2() {
constexpr char kByte = '!';
ASSERT_TRUE(WriteFileDescriptor(write_file_descriptor2(),
as_bytes(make_span(&kByte, 1u))));
as_bytes(span_from_ref(kByte))));
}

void ReadByte2() {
Expand All @@ -199,7 +199,7 @@ class FileDescriptorWatcherTest
EXPECT_TRUE(thread_checker_.CalledOnValidThread());

char buffer;
ASSERT_TRUE(ReadFromFD(read_file_descriptor2(), make_span(&buffer, 1u)));
ASSERT_TRUE(ReadFromFD(read_file_descriptor2(), span_from_ref(buffer)));
}

void CloseWriteFd2() {
Expand Down
2 changes: 1 addition & 1 deletion base/i18n/streaming_utf8_validator_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ class StreamingUtf8ValidatorSingleSequenceTest : public ::testing::Test {
std::string_view sequence = *it;
StreamingUtf8Validator::State state = VALID_ENDPOINT;
for (const auto& cit : sequence) {
state = validator.AddBytes(base::as_bytes(base::make_span(&cit, 1u)));
state = validator.AddBytes(base::as_bytes(base::span_from_ref(cit)));
}
EXPECT_EQ(expected, state) << "Failed for \"" << sequence << "\"";
}
Expand Down
10 changes: 5 additions & 5 deletions base/sync_socket_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ void SendReceivePeek(SyncSocket* socket_a, SyncSocket* socket_b) {
// Verify |socket_a| can send to |socket_a| and |socket_a| can Receive from
// |socket_a|.
ASSERT_EQ(sizeof(kSending),
socket_a->Send(as_bytes(make_span(&kSending, 1u))));
socket_a->Send(as_bytes(span_from_ref(kSending))));
ASSERT_EQ(sizeof(kSending), socket_b->Peek());
ASSERT_EQ(sizeof(kSending),
socket_b->Receive(as_writable_bytes(make_span(&received, 1u))));
socket_b->Receive(as_writable_bytes(span_from_ref(received))));
ASSERT_EQ(kSending, received);

ASSERT_EQ(0u, socket_a->Peek());
Expand All @@ -96,10 +96,10 @@ void SendReceivePeek(SyncSocket* socket_a, SyncSocket* socket_b) {
// Now verify the reverse.
received = 0;
ASSERT_EQ(sizeof(kSending),
socket_b->Send(as_bytes(make_span(&kSending, 1u))));
socket_b->Send(as_bytes(span_from_ref(kSending))));
ASSERT_EQ(sizeof(kSending), socket_a->Peek());
ASSERT_EQ(sizeof(kSending),
socket_a->Receive(as_writable_bytes(make_span(&received, 1u))));
socket_a->Receive(as_writable_bytes(span_from_ref(received))));
ASSERT_EQ(kSending, received);

ASSERT_EQ(0u, socket_a->Peek());
Expand Down Expand Up @@ -182,7 +182,7 @@ TEST_F(CancelableSyncSocketTest, ShutdownCancelsReceiveWithTimeout) {
TEST_F(CancelableSyncSocketTest, ReceiveAfterShutdown) {
socket_a_.Shutdown();
int data = 0;
EXPECT_EQ(0u, socket_a_.Receive(as_writable_bytes(make_span(&data, 1u))));
EXPECT_EQ(0u, socket_a_.Receive(as_writable_bytes(span_from_ref(data))));
}

TEST_F(CancelableSyncSocketTest, ReceiveWithTimeoutAfterShutdown) {
Expand Down
2 changes: 1 addition & 1 deletion base/task/thread_pool/thread_pool_impl_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ TEST_P(ThreadPoolImplTest, FileDescriptorWatcherNoOpsAfterShutdown) {
thread_pool_->Shutdown();

constexpr char kByte = '!';
ASSERT_TRUE(WriteFileDescriptor(pipes[1], as_bytes(make_span(&kByte, 1u))));
ASSERT_TRUE(WriteFileDescriptor(pipes[1], as_bytes(span_from_ref(kByte))));

// Give a chance for the file watcher to fire before closing the handles.
PlatformThread::Sleep(TestTimeouts::tiny_timeout());
Expand Down
44 changes: 22 additions & 22 deletions base/trace_event/traced_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ const bool kStackTypeArray = true;
#endif

inline void WriteKeyNameAsRawPtr(Pickle& pickle, const char* ptr) {
pickle.WriteBytes(as_bytes(make_span(&kTypeCStr, 1u)));
pickle.WriteBytes(as_bytes(span_from_ref(kTypeCStr)));
pickle.WriteUInt64(static_cast<uint64_t>(reinterpret_cast<uintptr_t>(ptr)));
}

inline void WriteKeyNameWithCopy(Pickle& pickle, std::string_view str) {
pickle.WriteBytes(as_bytes(make_span(&kTypeString, 1u)));
pickle.WriteBytes(as_bytes(span_from_ref(kTypeString)));
pickle.WriteString(str);
}

Expand Down Expand Up @@ -106,50 +106,50 @@ class PickleWriter final : public TracedValue::Writer {
bool IsProtoWriter() const override { return false; }

void SetInteger(const char* name, int value) override {
pickle_.WriteBytes(as_bytes(make_span(&kTypeInt, 1u)));
pickle_.WriteBytes(as_bytes(span_from_ref(kTypeInt)));
pickle_.WriteInt(value);
WriteKeyNameAsRawPtr(pickle_, name);
}

void SetIntegerWithCopiedName(std::string_view name, int value) override {
pickle_.WriteBytes(as_bytes(make_span(&kTypeInt, 1u)));
pickle_.WriteBytes(as_bytes(span_from_ref(kTypeInt)));
pickle_.WriteInt(value);
WriteKeyNameWithCopy(pickle_, name);
}

void SetDouble(const char* name, double value) override {
pickle_.WriteBytes(as_bytes(make_span(&kTypeDouble, 1u)));
pickle_.WriteBytes(as_bytes(span_from_ref(kTypeDouble)));
pickle_.WriteDouble(value);
WriteKeyNameAsRawPtr(pickle_, name);
}

void SetDoubleWithCopiedName(std::string_view name, double value) override {
pickle_.WriteBytes(as_bytes(make_span(&kTypeDouble, 1u)));
pickle_.WriteBytes(as_bytes(span_from_ref(kTypeDouble)));
pickle_.WriteDouble(value);
WriteKeyNameWithCopy(pickle_, name);
}

void SetBoolean(const char* name, bool value) override {
pickle_.WriteBytes(as_bytes(make_span(&kTypeBool, 1u)));
pickle_.WriteBytes(as_bytes(span_from_ref(kTypeBool)));
pickle_.WriteBool(value);
WriteKeyNameAsRawPtr(pickle_, name);
}

void SetBooleanWithCopiedName(std::string_view name, bool value) override {
pickle_.WriteBytes(as_bytes(make_span(&kTypeBool, 1u)));
pickle_.WriteBytes(as_bytes(span_from_ref(kTypeBool)));
pickle_.WriteBool(value);
WriteKeyNameWithCopy(pickle_, name);
}

void SetString(const char* name, std::string_view value) override {
pickle_.WriteBytes(as_bytes(make_span(&kTypeString, 1u)));
pickle_.WriteBytes(as_bytes(span_from_ref(kTypeString)));
pickle_.WriteString(value);
WriteKeyNameAsRawPtr(pickle_, name);
}

void SetStringWithCopiedName(std::string_view name,
std::string_view value) override {
pickle_.WriteBytes(as_bytes(make_span(&kTypeString, 1u)));
pickle_.WriteBytes(as_bytes(span_from_ref(kTypeString)));
pickle_.WriteString(value);
WriteKeyNameWithCopy(pickle_, name);
}
Expand All @@ -173,57 +173,57 @@ class PickleWriter final : public TracedValue::Writer {
}

void BeginArray() override {
pickle_.WriteBytes(as_bytes(make_span(&kTypeStartArray, 1u)));
pickle_.WriteBytes(as_bytes(span_from_ref(kTypeStartArray)));
}

void BeginDictionary() override {
pickle_.WriteBytes(as_bytes(make_span(&kTypeStartDict, 1u)));
pickle_.WriteBytes(as_bytes(span_from_ref(kTypeStartDict)));
}

void BeginDictionary(const char* name) override {
pickle_.WriteBytes(as_bytes(make_span(&kTypeStartDict, 1u)));
pickle_.WriteBytes(as_bytes(span_from_ref(kTypeStartDict)));
WriteKeyNameAsRawPtr(pickle_, name);
}

void BeginDictionaryWithCopiedName(std::string_view name) override {
pickle_.WriteBytes(as_bytes(make_span(&kTypeStartDict, 1u)));
pickle_.WriteBytes(as_bytes(span_from_ref(kTypeStartDict)));
WriteKeyNameWithCopy(pickle_, name);
}

void BeginArray(const char* name) override {
pickle_.WriteBytes(as_bytes(make_span(&kTypeStartArray, 1u)));
pickle_.WriteBytes(as_bytes(span_from_ref(kTypeStartArray)));
WriteKeyNameAsRawPtr(pickle_, name);
}

void BeginArrayWithCopiedName(std::string_view name) override {
pickle_.WriteBytes(as_bytes(make_span(&kTypeStartArray, 1u)));
pickle_.WriteBytes(as_bytes(span_from_ref(kTypeStartArray)));
WriteKeyNameWithCopy(pickle_, name);
}

void EndDictionary() override {
pickle_.WriteBytes(as_bytes(make_span(&kTypeEndDict, 1u)));
pickle_.WriteBytes(as_bytes(span_from_ref(kTypeEndDict)));
}
void EndArray() override {
pickle_.WriteBytes(as_bytes(make_span(&kTypeEndArray, 1u)));
pickle_.WriteBytes(as_bytes(span_from_ref(kTypeEndArray)));
}

void AppendInteger(int value) override {
pickle_.WriteBytes(as_bytes(make_span(&kTypeInt, 1u)));
pickle_.WriteBytes(as_bytes(span_from_ref(kTypeInt)));
pickle_.WriteInt(value);
}

void AppendDouble(double value) override {
pickle_.WriteBytes(as_bytes(make_span(&kTypeDouble, 1u)));
pickle_.WriteBytes(as_bytes(span_from_ref(kTypeDouble)));
pickle_.WriteDouble(value);
}

void AppendBoolean(bool value) override {
pickle_.WriteBytes(as_bytes(make_span(&kTypeBool, 1u)));
pickle_.WriteBytes(as_bytes(span_from_ref(kTypeBool)));
pickle_.WriteBool(value);
}

void AppendString(std::string_view value) override {
pickle_.WriteBytes(as_bytes(make_span(&kTypeString, 1u)));
pickle_.WriteBytes(as_bytes(span_from_ref(kTypeString)));
pickle_.WriteString(value);
}

Expand Down
2 changes: 1 addition & 1 deletion base/win/scoped_handle_verifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace internal {

struct HandleHash {
size_t operator()(const HANDLE& handle) const {
return base::FastHash(as_bytes(make_span(&handle, 1u)));
return base::FastHash(as_bytes(span_from_ref(handle)));
}
};

Expand Down
8 changes: 4 additions & 4 deletions chrome/browser/ash/policy/reporting/install_event_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ InstallEventLog<T, C>::InstallEventLog(const base::FilePath& file_name)

int64_t version;
if (!file.ReadAtCurrentPosAndCheck(
base::as_writable_bytes(base::make_span(&version, 1u)))) {
base::as_writable_bytes(base::span_from_ref(version)))) {
LOG(WARNING) << "Corrupted install log.";
return;
}
Expand All @@ -106,7 +106,7 @@ InstallEventLog<T, C>::InstallEventLog(const base::FilePath& file_name)

ssize_t entries;
if (!file.ReadAtCurrentPosAndCheck(
base::as_writable_bytes(base::make_span(&entries, 1u)))) {
base::as_writable_bytes(base::span_from_ref(entries)))) {
LOG(WARNING) << "Corrupted install log.";
return;
}
Expand Down Expand Up @@ -169,14 +169,14 @@ void InstallEventLog<T, C>::Store() {
}

if (!file.WriteAtCurrentPosAndCheck(
base::as_bytes(base::make_span(&kLogFileVersion, 1u)))) {
base::as_bytes(base::span_from_ref(kLogFileVersion)))) {
LOG(WARNING) << "Unable to store install log.";
return;
}

ssize_t entries = logs_.size();
if (!file.WriteAtCurrentPosAndCheck(
base::as_bytes(base::make_span(&entries, 1u)))) {
base::as_bytes(base::span_from_ref(entries)))) {
LOG(WARNING) << "Unable to store install log.";
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ bool AddPathToRPath(const base::FilePath& executable_path,

mach_header_64 header;
if (!executable_file.ReadAtCurrentPosAndCheck(
base::as_writable_bytes(base::make_span(&header, 1u))) ||
base::as_writable_bytes(base::span_from_ref(header))) ||
header.magic != MH_MAGIC_64 || header.filetype != MH_EXECUTE) {
LOG(ERROR) << "File at " << executable_path
<< " is not a valid Mach-O executable";
Expand Down Expand Up @@ -184,7 +184,7 @@ bool AddPathToRPath(const base::FilePath& executable_path,

// Write the updated header and commands back to the file.
if (!executable_file.WriteAndCheck(
0, base::as_bytes(base::make_span(&header, 1u))) ||
0, base::as_bytes(base::span_from_ref(header))) ||
!executable_file.WriteAndCheck(sizeof header,
base::make_span(commands))) {
LOG(ERROR) << "Failed to write updated load commands to "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ bool SaveIconWithCheckSum(const base::FilePath& icon_file,
// Passing digest as one element in a span of digest fields, therefore the 1u,
// and then having as_bytes converting it to a new span of uint8_t's.
return base::WriteFile(cheksum_file,
base::as_bytes(base::make_span(&digest, 1u)));
base::as_bytes(base::span_from_ref(digest)));
}

// Returns true if |icon_file| is missing or different from |image|.
Expand Down
2 changes: 1 addition & 1 deletion chrome/renderer/cart/commerce_hint_agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ void CommerceHintAgent::ExtractCartWithUpdatedScript(
GetProductExtractionScript(product_id_json, cart_extraction_script));

main_frame->RequestExecuteScript(
ISOLATED_WORLD_ID_CHROME_INTERNAL, base::make_span(&source, 1u),
ISOLATED_WORLD_ID_CHROME_INTERNAL, base::span_from_ref(source),
blink::mojom::UserActivationOption::kDoNotActivate,
blink::mojom::EvaluationTiming::kAsynchronous,
blink::mojom::LoadEventBlockingOption::kDoNotBlock,
Expand Down
2 changes: 1 addition & 1 deletion chrome/renderer/wallet/boarding_pass_extractor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void BoardingPassExtractor::ExtractBoardingPassWithScript(
blink::WebScriptSource(blink::WebString::FromUTF8(script));

main_frame->RequestExecuteScript(
ISOLATED_WORLD_ID_CHROME_INTERNAL, base::make_span(&source, 1u),
ISOLATED_WORLD_ID_CHROME_INTERNAL, base::span_from_ref(source),
blink::mojom::UserActivationOption::kDoNotActivate,
blink::mojom::EvaluationTiming::kAsynchronous,
blink::mojom::LoadEventBlockingOption::kDoNotBlock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ class ScopedAlsaMixerEventTest : public ::testing::Test {

void ReadByte() {
char buffer;
ASSERT_TRUE(base::ReadFromFD(pipe_fds_[0], base::make_span(&buffer, 1u)));
ASSERT_TRUE(base::ReadFromFD(pipe_fds_[0], base::span_from_ref(buffer)));
}

void WriteByte() {
constexpr char kByte = '!';
ASSERT_TRUE(base::WriteFileDescriptor(
pipe_fds_[1], as_bytes(base::make_span(&kByte, 1u))));
pipe_fds_[1], as_bytes(base::span_from_ref(kByte))));
}

base::test::TaskEnvironment task_environment_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ void PostResponse(base::OnceCallback<void(const TProto&)> callback,
std::string ReadPassword(int password_fd) {
std::string password;
char c;
while (base::ReadFromFD(password_fd, base::make_span(&c, 1u))) {
while (base::ReadFromFD(password_fd, base::span_from_ref(c))) {
password.push_back(c);
}
return password;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ base::ScopedFD CreatePasswordPipe(const std::string& data) {
const size_t data_size = data.size();

base::WriteFileDescriptor(pipe_write_end.get(),
base::as_bytes(base::make_span(&data_size, 1u)));
base::as_bytes(base::span_from_ref(data_size)));
base::WriteFileDescriptor(pipe_write_end.get(), data);

return pipe_read_end;
Expand Down
4 changes: 2 additions & 2 deletions chromeos/printing/uri_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ bool Uri::Pim::ParseString(const Iter& begin,
// Try to parse UTF-8 character.
base::StreamingUtf8Validator utf_parser;
base::StreamingUtf8Validator::State state =
utf_parser.AddBytes(base::as_bytes(base::make_span(&c, 1u)));
utf_parser.AddBytes(base::as_bytes(base::span_from_ref(c)));
if (state != base::StreamingUtf8Validator::State::VALID_MIDPOINT) {
parser_error_.status = ParserStatus::kDisallowedASCIICharacter;
return false;
Expand All @@ -127,7 +127,7 @@ bool Uri::Pim::ParseString(const Iter& begin,
parser_error_.status = ParserStatus::kInvalidPercentEncoding;
return false;
}
state = utf_parser.AddBytes(base::as_bytes(base::make_span(&c, 1u)));
state = utf_parser.AddBytes(base::as_bytes(base::span_from_ref(c)));
if (state == base::StreamingUtf8Validator::State::INVALID) {
parser_error_.status = ParserStatus::kInvalidUTF8Character;
return false;
Expand Down
2 changes: 1 addition & 1 deletion components/metrics/serialization/serialization_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ bool SerializationUtils::WriteMetricToFile(const MetricSample& sample,
uint32_t encoded_size = base::checked_cast<uint32_t>(size);
if (!base::WriteFileDescriptor(
file_descriptor.get(),
base::as_bytes(base::make_span(&encoded_size, 1u)))) {
base::as_bytes(base::span_from_ref(encoded_size)))) {
DPLOG(ERROR) << "error writing message length: " << filename;
std::ignore = flock(file_descriptor.get(), LOCK_UN);
return false;
Expand Down
2 changes: 1 addition & 1 deletion components/power_metrics/energy_metrics_provider_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ EnergyMetricsProviderLinux::CaptureMetrics() {
uint64_t absolute_energy;
if (!base::ReadFromFD(
event.fd.get(),
base::as_writable_chars(base::make_span(&absolute_energy, 1u)))) {
base::as_writable_chars(base::span_from_ref(absolute_energy)))) {
LOG(ERROR) << "Failed to read absolute energy of " << event.metric_type;
continue;
}
Expand Down
Loading

0 comments on commit b075e19

Please sign in to comment.