From 95caa1f0bd1b3dcb8e7bc6c3ee6ff5955e96a3f7 Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Fri, 20 Sep 2024 08:47:49 -0500 Subject: [PATCH] fix: -Wunsafe-buffer-usage warnings in Clipboard::WriteBuffer() (#43813) * fix: -Wunsafe-buffer-usage warnings in Clipboard::WriteBuffer() Co-authored-by: Charles Kerr * chore: add a DCHECK to confirm the BigBuffer is full Co-authored-by: Charles Kerr --------- Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Charles Kerr --- shell/common/api/electron_api_clipboard.cc | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/shell/common/api/electron_api_clipboard.cc b/shell/common/api/electron_api_clipboard.cc index f8c2a84edd3e..32be2805969a 100644 --- a/shell/common/api/electron_api_clipboard.cc +++ b/shell/common/api/electron_api_clipboard.cc @@ -111,12 +111,16 @@ void Clipboard::WriteBuffer(const std::string& format, return; } + CHECK(buffer->IsArrayBufferView()); + v8::Local buffer_view = buffer.As(); + const size_t n_bytes = buffer_view->ByteLength(); + mojo_base::BigBuffer big_buffer{n_bytes}; + [[maybe_unused]] const size_t n_got = + buffer_view->CopyContents(big_buffer.data(), n_bytes); + DCHECK_EQ(n_got, n_bytes); + ui::ScopedClipboardWriter writer(GetClipboardBuffer(args)); - base::span payload_span( - reinterpret_cast(node::Buffer::Data(buffer)), - node::Buffer::Length(buffer)); - writer.WriteUnsafeRawData(base::UTF8ToUTF16(format), - mojo_base::BigBuffer(payload_span)); + writer.WriteUnsafeRawData(base::UTF8ToUTF16(format), std::move(big_buffer)); } void Clipboard::Write(const gin_helper::Dictionary& data,