0

Avoid unsafe forms for base::File::WriteAtCurrentPos()

Use the span based forms instead. Adjust callers to accommodate
an optional<size_t> return.

This CL was uploaded by git cl split.

R=jzw@chromium.org, khorimoto@chromium.org, skuhne@chromium.org, waffles@chromium.org, wtlee@chromium.org

Bug: 42271176
Change-Id: I7b1c5869e8660b017e036ce407c93b02a64c4878
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5819272
Reviewed-by: Wei Lee <wtlee@chromium.org>
Auto-Submit: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Joshua Pawlicki <waffles@chromium.org>
Reviewed-by: Kyle Horimoto <khorimoto@chromium.org>
Reviewed-by: Stefan Kuhne <skuhne@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: John Wu <jzw@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1358864}
This commit is contained in:
Tom Sepez
2024-09-23 16:52:15 +00:00
committed by Chromium LUCI CQ
parent c5cd7b8dfa
commit 9ccb791c33
5 changed files with 25 additions and 28 deletions

@ -4,7 +4,7 @@
#include "components/about_ui/credit_utils.h"
#include "base/compiler_specific.h"
#include "base/containers/span.h"
#include "base/files/file.h"
// Must come after all headers that specialize FromJniType() / ToJniType().
@ -13,10 +13,8 @@
namespace about_ui {
static void JNI_CreditUtils_WriteCreditsHtml(JNIEnv* env, jint fd) {
std::string html_content = GetCredits(false);
base::File out_file(fd);
UNSAFE_TODO(
out_file.WriteAtCurrentPos(html_content.c_str(), html_content.size()));
out_file.WriteAtCurrentPos(base::as_byte_span(GetCredits(false)));
}
} // namespace about_ui

@ -1,15 +1,19 @@
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/chromeos_camera/jpeg_encode_accelerator.h"
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <sys/mman.h>
#include <memory>
#include "base/at_exit.h"
#include "base/command_line.h"
#include "base/containers/span.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/logging.h"
@ -27,7 +31,6 @@
#include "base/time/time.h"
#include "build/build_config.h"
#include "components/chromeos_camera/gpu_jpeg_encode_accelerator_factory.h"
#include "components/chromeos_camera/jpeg_encode_accelerator.h"
#include "media/base/color_plane_layout.h"
#include "media/base/test_data_util.h"
#include "media/gpu/buildflags.h"
@ -219,7 +222,7 @@ void JpegEncodeAcceleratorTestEnvironment::LogToFile(const std::string& key,
std::string s = base::StringPrintf("%s: %s\n", key.c_str(), value.c_str());
LOG(INFO) << s;
if (log_file_) {
log_file_->WriteAtCurrentPos(s.data(), static_cast<int>(s.length()));
log_file_->WriteAtCurrentPos(base::as_byte_span(s));
}
}

@ -7,6 +7,7 @@
#include <memory>
#include "base/compiler_specific.h"
#include "base/containers/span.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/metrics/metrics_hashes.h"
@ -30,7 +31,8 @@ base::File CreateInvalidModelFile() {
base::File file(file_path, (base::File::FLAG_CREATE | base::File::FLAG_READ |
base::File::FLAG_WRITE |
base::File::FLAG_CAN_DELETE_ON_CLOSE));
EXPECT_TRUE(UNSAFE_TODO(file.WriteAtCurrentPos("12345", 5)));
EXPECT_TRUE(
file.WriteAtCurrentPosAndCheck(base::byte_span_from_cstring("12345")));
return file;
}

@ -16,6 +16,7 @@
#include <string_view>
#include <utility>
#include "base/containers/span.h"
#include "base/feature_list.h"
#include "base/features.h"
#include "base/files/file.h"
@ -766,8 +767,7 @@ std::unique_ptr<base::File> CommandStorageBackend::OpenAndWriteHeader(
header.signature = kFileSignature;
header.version =
IsEncrypted() ? kEncryptedFileVersionWithMarker : kFileVersionWithMarker;
if (file->WriteAtCurrentPos(reinterpret_cast<char*>(&header),
sizeof(header)) != sizeof(header)) {
if (!file->WriteAtCurrentPosAndCheck(base::byte_span_from_ref(header))) {
return nullptr;
}
return file;
@ -777,24 +777,22 @@ bool CommandStorageBackend::AppendCommandToFile(
base::File* file,
const sessions::SessionCommand& command) {
const size_type total_size = command.GetSerializedSize();
if (file->WriteAtCurrentPos(reinterpret_cast<const char*>(&total_size),
sizeof(total_size)) != sizeof(total_size)) {
if (!file->WriteAtCurrentPosAndCheck(base::byte_span_from_ref(total_size))) {
DVLOG(1) << "error writing";
return false;
}
id_type command_id = command.id();
if (file->WriteAtCurrentPos(reinterpret_cast<char*>(&command_id),
sizeof(command_id)) != sizeof(command_id)) {
if (!file->WriteAtCurrentPosAndCheck(base::byte_span_from_ref(command_id))) {
DVLOG(1) << "error writing";
return false;
}
const size_type content_size = total_size - sizeof(id_type);
if (content_size == 0)
if (content_size == 0) {
return true;
if (file->WriteAtCurrentPos(reinterpret_cast<const char*>(command.contents()),
content_size) != content_size) {
}
if (!file->WriteAtCurrentPos(
base::as_byte_span(command.contents_as_string_piece())
.first(content_size))) {
DVLOG(1) << "error writing";
return false;
}
@ -835,15 +833,12 @@ bool CommandStorageBackend::AppendEncryptedCommandToFile(
const size_type command_and_id_size =
static_cast<size_type>(cipher_text.size());
int wrote = file->WriteAtCurrentPos(
reinterpret_cast<const char*>(&command_and_id_size),
sizeof(command_and_id_size));
if (wrote != sizeof(command_and_id_size)) {
if (!file->WriteAtCurrentPosAndCheck(
base::byte_span_from_ref(command_and_id_size))) {
DVLOG(1) << "error writing";
return false;
}
wrote = file->WriteAtCurrentPos(cipher_text.c_str(), cipher_text.size());
if (wrote != static_cast<int>(cipher_text.size())) {
if (!file->WriteAtCurrentPosAndCheck(base::as_byte_span(cipher_text))) {
DVLOG(1) << "error writing";
return false;
}

@ -14,7 +14,7 @@
#include "base/check.h"
#include "base/check_op.h"
#include "base/compiler_specific.h"
#include "base/containers/span.h"
#include "base/files/file.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
@ -455,8 +455,7 @@ bool NetworkFetcher::WriteDataToFileBlocking() {
}
}
if (UNSAFE_TODO(file_.WriteAtCurrentPos(&read_buffer_.front(),
read_buffer_.size())) == -1) {
if (!file_.WriteAtCurrentPosAndCheck(base::as_byte_span(read_buffer_))) {
net_error_ = HRESULTFromLastError();
file_.Close();
base::DeleteFile(file_path_);