diff --git a/components/headless/command_handler/headless_command_handler.cc b/components/headless/command_handler/headless_command_handler.cc index 140f86ae20c2b2..a15b468d9040e0 100644 --- a/components/headless/command_handler/headless_command_handler.cc +++ b/components/headless/command_handler/headless_command_handler.cc @@ -2,11 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(crbug.com/40285824): Remove this and convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "components/headless/command_handler/headless_command_handler.h" #include @@ -265,8 +260,7 @@ bool GetCommandDictAndOutputPaths(base::Value::Dict* commands, } bool WriteFileTask(base::FilePath file_path, std::string file_data) { - auto file_span = base::make_span( - reinterpret_cast(file_data.data()), file_data.size()); + auto file_span = base::as_byte_span(file_data); if (!base::WriteFile(file_path, file_span)) { PLOG(ERROR) << "Failed to write file " << file_path; return false; diff --git a/components/headless/test/capture_std_stream.cc b/components/headless/test/capture_std_stream.cc index 976a13cc2e9d69..1ab38358c6fd54 100644 --- a/components/headless/test/capture_std_stream.cc +++ b/components/headless/test/capture_std_stream.cc @@ -2,11 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(crbug.com/40285824): Remove this and convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "components/headless/test/capture_std_stream.h" #include @@ -30,9 +25,9 @@ static constexpr char kPipeEnd = '\xff'; CaptureStdStream::CaptureStdStream(FILE* stream) : stream_(stream) { #if BUILDFLAG(IS_WIN) - CHECK_EQ(_pipe(pipes_, 4096, O_BINARY), 0); + CHECK_EQ(_pipe(pipes_.data(), 4096, O_BINARY), 0); #else - CHECK_EQ(pipe(pipes_), 0); + CHECK_EQ(pipe(pipes_.data()), 0); #endif fileno_ = dup(fileno(stream_)); CHECK_NE(fileno_, -1); @@ -76,13 +71,15 @@ std::string CaptureStdStream::TakeCapturedData() { std::string captured_data; for (;;) { constexpr size_t kChunkSize = 256; - char buffer[kChunkSize]; - int bytes_read = read(pipes_[kReadPipe], buffer, kChunkSize); + std::array buffer; + int bytes_read = read(pipes_[kReadPipe], buffer.data(), kChunkSize); CHECK_GT(bytes_read, 0); - if (buffer[bytes_read - 1] != kPipeEnd) { - captured_data.append(buffer, bytes_read); + std::string_view data(buffer.data(), bytes_read); + if (data.back() != kPipeEnd) { + captured_data.append(data); } else { - captured_data.append(buffer, bytes_read - 1); + data.remove_suffix(1); + captured_data.append(data); break; } } diff --git a/components/headless/test/capture_std_stream.h b/components/headless/test/capture_std_stream.h index c69b150e043dd9..27b43c30a478fb 100644 --- a/components/headless/test/capture_std_stream.h +++ b/components/headless/test/capture_std_stream.h @@ -28,7 +28,7 @@ class CaptureStdStream { raw_ptr stream_; int fileno_ = -1; - int pipes_[2] = {-1, -1}; + std::array pipes_ = {-1, -1}; bool capturing_ = false; // TODO(https://github.com/llvm/llvm-project/issues/61334): Explicit diff --git a/headless/test/headless_printtopdf_browsertest.cc b/headless/test/headless_printtopdf_browsertest.cc index 69fef0f7b9c70e..874961b6370c77 100644 --- a/headless/test/headless_printtopdf_browsertest.cc +++ b/headless/test/headless_printtopdf_browsertest.cc @@ -2,11 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(crbug.com/351564777): Remove this and convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include #include #include @@ -85,9 +80,7 @@ class HeadlessPDFPagesBrowserTest : public HeadlessDevTooledBrowserTest { ASSERT_TRUE(base::Base64Decode(pdf_data_base64, &pdf_data)); EXPECT_GT(pdf_data.size(), 0U); - auto pdf_span = base::make_span( - reinterpret_cast(pdf_data.data()), pdf_data.size()); - + auto pdf_span = base::as_byte_span(pdf_data); int num_pages; EXPECT_TRUE(chrome_pdf::GetPDFDocInfo(pdf_span, &num_pages, nullptr)); EXPECT_EQ(std::ceil(kDocHeight / kPaperHeight), num_pages); @@ -195,8 +188,7 @@ class HeadlessPDFStreamBrowserTest : public HeadlessDevTooledBrowserTest { ASSERT_TRUE(base::Base64Decode(base64_pdf_data_, &pdf_data)); EXPECT_GT(pdf_data.size(), 0U); - auto pdf_span = base::make_span( - reinterpret_cast(pdf_data.data()), pdf_data.size()); + auto pdf_span = base::as_byte_span(pdf_data); int num_pages; EXPECT_TRUE(chrome_pdf::GetPDFDocInfo(pdf_span, &num_pages, nullptr)); @@ -254,8 +246,7 @@ class HeadlessPDFBrowserTestBase : public HeadlessDevTooledBrowserTest { ASSERT_TRUE(base::Base64Decode(pdf_data_base64, &pdf_data)); ASSERT_GT(pdf_data.size(), 0U); - auto pdf_span = base::make_span( - reinterpret_cast(pdf_data.data()), pdf_data.size()); + auto pdf_span = base::as_byte_span(pdf_data); int num_pages; ASSERT_TRUE(chrome_pdf::GetPDFDocInfo(pdf_span, &num_pages, nullptr)); OnPDFReady(pdf_span, num_pages);