fix: -Wunsafe-buffer-usage warning in ChunkedDataPipeReadableStream (#44211)

* chore: rename v8_value_serializer.cc,h to v8_util.cc,h

* feat: add electron::util::as_byte_span(v8::Local<v8::ArrayBuffer>)

* fix: -Wunsafe-buffer-usage warnings in ChunkedDataPipeReadableStream::ReadInternal()

Xref: 5619253

* refactor: restore node buffer span util

* refactor: remove redundant span wrapper
This commit is contained in:
Charles Kerr 2024-10-14 03:46:24 -05:00 committed by GitHub
parent 8b3d70a2a3
commit 566c72cd46
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 41 additions and 17 deletions

View file

@ -21,7 +21,7 @@
#include "shell/common/gin_converters/value_converter.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/keyboard_util.h"
#include "shell/common/v8_value_serializer.h"
#include "shell/common/v8_util.h"
#include "third_party/blink/public/common/context_menu_data/edit_flags.h"
#include "third_party/blink/public/common/input/web_input_event.h"
#include "third_party/blink/public/common/input/web_keyboard_event.h"

View file

@ -34,6 +34,7 @@
#include "shell/common/gin_converters/value_converter.h"
#include "shell/common/gin_helper/promise.h"
#include "shell/common/node_includes.h"
#include "shell/common/v8_util.h"
namespace gin {
@ -368,10 +369,7 @@ class ChunkedDataPipeReadableStream final
num_bytes = *size_ - bytes_read_;
MojoResult rv = data_pipe_->ReadData(
MOJO_READ_DATA_FLAG_NONE,
base::span(static_cast<uint8_t*>(buf->Buffer()->Data()),
buf->ByteLength())
.subspan(buf->ByteOffset(), num_bytes),
num_bytes);
electron::util::as_byte_span(buf).first(num_bytes), num_bytes);
if (rv == MOJO_RESULT_OK) {
bytes_read_ += num_bytes;
// Not needed for correctness, but this allows the consumer to send the

View file

@ -2,7 +2,7 @@
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/common/v8_value_serializer.h"
#include "shell/common/v8_util.h"
#include <utility>
#include <vector>
@ -240,4 +240,23 @@ v8::Local<v8::Value> DeserializeV8Value(v8::Isolate* isolate,
return V8Deserializer(isolate, data).Deserialize();
}
namespace util {
/**
* SAFETY: There is not yet any v8::ArrayBufferView API that passes the
* UNSAFE_BUFFER_USAGE test, so let's isolate the unsafe API here.
*
* Where possible, Electron should use spans returned here instead of
* |v8::ArrayBufferView::Buffer()->Data()|,
* |v8::ArrayBufferView::ByteOffset()|,
* |v8::ArrayBufferView::ByteLength()|.
*/
base::span<uint8_t> as_byte_span(v8::Local<v8::ArrayBufferView> val) {
uint8_t* data =
static_cast<uint8_t*>(val->Buffer()->Data()) + val->ByteOffset();
const size_t size = val->ByteLength();
return UNSAFE_BUFFERS(base::span{data, size});
}
} // namespace util
} // namespace electron

View file

@ -9,6 +9,7 @@
#include "ui/gfx/image/image_skia_rep.h"
namespace v8 {
class ArrayBufferView;
class Isolate;
template <class T>
class Local;
@ -29,6 +30,12 @@ v8::Local<v8::Value> DeserializeV8Value(v8::Isolate* isolate,
v8::Local<v8::Value> DeserializeV8Value(v8::Isolate* isolate,
base::span<const uint8_t> data);
namespace util {
[[nodiscard]] base::span<uint8_t> as_byte_span(
v8::Local<v8::ArrayBufferView> abv);
} // namespace util
} // namespace electron
#endif // ELECTRON_SHELL_COMMON_V8_VALUE_SERIALIZER_H_