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:

committed by
Chromium LUCI CQ

parent
c5cd7b8dfa
commit
9ccb791c33
components
about_ui
chromeos_camera
language_detection
sessions
winhttp
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
#include "components/about_ui/credit_utils.h"
|
#include "components/about_ui/credit_utils.h"
|
||||||
|
|
||||||
#include "base/compiler_specific.h"
|
#include "base/containers/span.h"
|
||||||
#include "base/files/file.h"
|
#include "base/files/file.h"
|
||||||
|
|
||||||
// Must come after all headers that specialize FromJniType() / ToJniType().
|
// Must come after all headers that specialize FromJniType() / ToJniType().
|
||||||
@@ -13,10 +13,8 @@
|
|||||||
namespace about_ui {
|
namespace about_ui {
|
||||||
|
|
||||||
static void JNI_CreditUtils_WriteCreditsHtml(JNIEnv* env, jint fd) {
|
static void JNI_CreditUtils_WriteCreditsHtml(JNIEnv* env, jint fd) {
|
||||||
std::string html_content = GetCredits(false);
|
|
||||||
base::File out_file(fd);
|
base::File out_file(fd);
|
||||||
UNSAFE_TODO(
|
out_file.WriteAtCurrentPos(base::as_byte_span(GetCredits(false)));
|
||||||
out_file.WriteAtCurrentPos(html_content.c_str(), html_content.size()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace about_ui
|
} // namespace about_ui
|
||||||
|
@@ -1,15 +1,19 @@
|
|||||||
// Copyright 2018 The Chromium Authors
|
// Copyright 2018 The Chromium Authors
|
||||||
// Use of this source code is governed by a BSD-style license that can be
|
// Use of this source code is governed by a BSD-style license that can be
|
||||||
// found in the LICENSE file.
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
#include "components/chromeos_camera/jpeg_encode_accelerator.h"
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#include "base/at_exit.h"
|
#include "base/at_exit.h"
|
||||||
#include "base/command_line.h"
|
#include "base/command_line.h"
|
||||||
|
#include "base/containers/span.h"
|
||||||
#include "base/files/file_util.h"
|
#include "base/files/file_util.h"
|
||||||
#include "base/functional/bind.h"
|
#include "base/functional/bind.h"
|
||||||
#include "base/logging.h"
|
#include "base/logging.h"
|
||||||
@@ -27,7 +31,6 @@
|
|||||||
#include "base/time/time.h"
|
#include "base/time/time.h"
|
||||||
#include "build/build_config.h"
|
#include "build/build_config.h"
|
||||||
#include "components/chromeos_camera/gpu_jpeg_encode_accelerator_factory.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/color_plane_layout.h"
|
||||||
#include "media/base/test_data_util.h"
|
#include "media/base/test_data_util.h"
|
||||||
#include "media/gpu/buildflags.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());
|
std::string s = base::StringPrintf("%s: %s\n", key.c_str(), value.c_str());
|
||||||
LOG(INFO) << s;
|
LOG(INFO) << s;
|
||||||
if (log_file_) {
|
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 <memory>
|
||||||
|
|
||||||
#include "base/compiler_specific.h"
|
#include "base/compiler_specific.h"
|
||||||
|
#include "base/containers/span.h"
|
||||||
#include "base/files/file_util.h"
|
#include "base/files/file_util.h"
|
||||||
#include "base/files/scoped_temp_dir.h"
|
#include "base/files/scoped_temp_dir.h"
|
||||||
#include "base/metrics/metrics_hashes.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 file(file_path, (base::File::FLAG_CREATE | base::File::FLAG_READ |
|
||||||
base::File::FLAG_WRITE |
|
base::File::FLAG_WRITE |
|
||||||
base::File::FLAG_CAN_DELETE_ON_CLOSE));
|
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;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -16,6 +16,7 @@
|
|||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
#include "base/containers/span.h"
|
||||||
#include "base/feature_list.h"
|
#include "base/feature_list.h"
|
||||||
#include "base/features.h"
|
#include "base/features.h"
|
||||||
#include "base/files/file.h"
|
#include "base/files/file.h"
|
||||||
@@ -766,8 +767,7 @@ std::unique_ptr<base::File> CommandStorageBackend::OpenAndWriteHeader(
|
|||||||
header.signature = kFileSignature;
|
header.signature = kFileSignature;
|
||||||
header.version =
|
header.version =
|
||||||
IsEncrypted() ? kEncryptedFileVersionWithMarker : kFileVersionWithMarker;
|
IsEncrypted() ? kEncryptedFileVersionWithMarker : kFileVersionWithMarker;
|
||||||
if (file->WriteAtCurrentPos(reinterpret_cast<char*>(&header),
|
if (!file->WriteAtCurrentPosAndCheck(base::byte_span_from_ref(header))) {
|
||||||
sizeof(header)) != sizeof(header)) {
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
return file;
|
return file;
|
||||||
@@ -777,24 +777,22 @@ bool CommandStorageBackend::AppendCommandToFile(
|
|||||||
base::File* file,
|
base::File* file,
|
||||||
const sessions::SessionCommand& command) {
|
const sessions::SessionCommand& command) {
|
||||||
const size_type total_size = command.GetSerializedSize();
|
const size_type total_size = command.GetSerializedSize();
|
||||||
if (file->WriteAtCurrentPos(reinterpret_cast<const char*>(&total_size),
|
if (!file->WriteAtCurrentPosAndCheck(base::byte_span_from_ref(total_size))) {
|
||||||
sizeof(total_size)) != sizeof(total_size)) {
|
|
||||||
DVLOG(1) << "error writing";
|
DVLOG(1) << "error writing";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
id_type command_id = command.id();
|
id_type command_id = command.id();
|
||||||
if (file->WriteAtCurrentPos(reinterpret_cast<char*>(&command_id),
|
if (!file->WriteAtCurrentPosAndCheck(base::byte_span_from_ref(command_id))) {
|
||||||
sizeof(command_id)) != sizeof(command_id)) {
|
|
||||||
DVLOG(1) << "error writing";
|
DVLOG(1) << "error writing";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const size_type content_size = total_size - sizeof(id_type);
|
const size_type content_size = total_size - sizeof(id_type);
|
||||||
if (content_size == 0)
|
if (content_size == 0) {
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
if (file->WriteAtCurrentPos(reinterpret_cast<const char*>(command.contents()),
|
if (!file->WriteAtCurrentPos(
|
||||||
content_size) != content_size) {
|
base::as_byte_span(command.contents_as_string_piece())
|
||||||
|
.first(content_size))) {
|
||||||
DVLOG(1) << "error writing";
|
DVLOG(1) << "error writing";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -835,15 +833,12 @@ bool CommandStorageBackend::AppendEncryptedCommandToFile(
|
|||||||
const size_type command_and_id_size =
|
const size_type command_and_id_size =
|
||||||
static_cast<size_type>(cipher_text.size());
|
static_cast<size_type>(cipher_text.size());
|
||||||
|
|
||||||
int wrote = file->WriteAtCurrentPos(
|
if (!file->WriteAtCurrentPosAndCheck(
|
||||||
reinterpret_cast<const char*>(&command_and_id_size),
|
base::byte_span_from_ref(command_and_id_size))) {
|
||||||
sizeof(command_and_id_size));
|
|
||||||
if (wrote != sizeof(command_and_id_size)) {
|
|
||||||
DVLOG(1) << "error writing";
|
DVLOG(1) << "error writing";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
wrote = file->WriteAtCurrentPos(cipher_text.c_str(), cipher_text.size());
|
if (!file->WriteAtCurrentPosAndCheck(base::as_byte_span(cipher_text))) {
|
||||||
if (wrote != static_cast<int>(cipher_text.size())) {
|
|
||||||
DVLOG(1) << "error writing";
|
DVLOG(1) << "error writing";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@@ -14,7 +14,7 @@
|
|||||||
|
|
||||||
#include "base/check.h"
|
#include "base/check.h"
|
||||||
#include "base/check_op.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.h"
|
||||||
#include "base/files/file_util.h"
|
#include "base/files/file_util.h"
|
||||||
#include "base/functional/bind.h"
|
#include "base/functional/bind.h"
|
||||||
@@ -455,8 +455,7 @@ bool NetworkFetcher::WriteDataToFileBlocking() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (UNSAFE_TODO(file_.WriteAtCurrentPos(&read_buffer_.front(),
|
if (!file_.WriteAtCurrentPosAndCheck(base::as_byte_span(read_buffer_))) {
|
||||||
read_buffer_.size())) == -1) {
|
|
||||||
net_error_ = HRESULTFromLastError();
|
net_error_ = HRESULTFromLastError();
|
||||||
file_.Close();
|
file_.Close();
|
||||||
base::DeleteFile(file_path_);
|
base::DeleteFile(file_path_);
|
||||||
|
Reference in New Issue
Block a user