Skip to content

Commit

Permalink
Remove #pragma allow_unsafe_buffers in the rest of headless
Browse files Browse the repository at this point in the history
Bug: 40285825
Change-Id: Ib739b69c46affadcf9f19d73eae42251468b2167
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5760537
Reviewed-by: Arthur Sonzogni <[email protected]>
Commit-Queue: Andrey Kosyakov <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1337365}
  • Loading branch information
caseq authored and Chromium LUCI CQ committed Aug 5, 2024
1 parent 2c6e328 commit 9464c0b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cstdint>
Expand Down Expand Up @@ -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<const uint8_t*>(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;
Expand Down
21 changes: 9 additions & 12 deletions components/headless/test/capture_std_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <fcntl.h>
Expand All @@ -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);
Expand Down Expand Up @@ -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<char, kChunkSize> 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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion components/headless/test/capture_std_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class CaptureStdStream {
raw_ptr<FILE> stream_;

int fileno_ = -1;
int pipes_[2] = {-1, -1};
std::array<int, 2> pipes_ = {-1, -1};
bool capturing_ = false;

// TODO(https://github.com/llvm/llvm-project/issues/61334): Explicit
Expand Down
15 changes: 3 additions & 12 deletions headless/test/headless_printtopdf_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <memory>
#include <optional>
#include <string>
Expand Down Expand Up @@ -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<const uint8_t*>(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);
Expand Down Expand Up @@ -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<const uint8_t*>(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));
Expand Down Expand Up @@ -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<const uint8_t*>(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);
Expand Down

0 comments on commit 9464c0b

Please sign in to comment.