mirror of
https://github.com/electron/electron.git
synced 2026-04-10 03:01:51 -04:00
refactor: spanify image utils (#44127)
* refactor: electron::util::AddImageSkiaRepFromJPEG() takes a span arg
* refactor: electron::util::AddImageSkiaRepFromPNG() takes a span arg
* refactor: electron::util::AddImageSkiaRepFromBuffer() takes a span arg
* feat: add Node-buffer-to-base-span helper function
* refactor: electron::api::NativeImage::CreateFromPNG() now takes a span param
* refactor: electron::api::NativeImage::CreateFromJPEG() now takes a span param
* refactor: use base::as_byte_span()
* fix: -Wunsafe-buffer-usage warning in NativeImage::CreateFromNamedImage()
Warning fixed by this commit:
../../electron/shell/common/api/electron_api_native_image_mac.mm:131:11: error: function introduces unsafe buffer manipulation [-Werror,-Wunsafe-buffer-usage]
131 | {reinterpret_cast<const uint8_t*>((char*)[png_data bytes]),
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
132 | [png_data length]});
| ~~~~~~~~~~~~~~~~~~
../../electron/shell/common/api/electron_api_native_image_mac.mm:131:11: note: See //docs/unsafe_buffers.md for help.
* chore: add // SAFETY comment for Node-buffer-to-span func
* chore: add // SAFETY comment for NSData-to-span func
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
#include "shell/common/node_util.h"
|
||||
|
||||
#include "base/compiler_specific.h"
|
||||
#include "base/logging.h"
|
||||
#include "gin/converter.h"
|
||||
#include "gin/dictionary.h"
|
||||
@@ -65,4 +66,14 @@ void EmitWarning(v8::Isolate* isolate,
|
||||
emit_warning.Run(warning_msg, warning_type, "");
|
||||
}
|
||||
|
||||
// SAFETY: There is no node::Buffer API that passes the UNSAFE_BUFFER_USAGE
|
||||
// test, so let's isolate the unsafe API use into this function. Instead of
|
||||
// calling `Buffer::Data()` and `Buffer::Length()` directly, the rest of our
|
||||
// code should prefer to use spans returned by this function.
|
||||
base::span<uint8_t> as_byte_span(v8::Local<v8::Value> node_buffer) {
|
||||
auto* data = reinterpret_cast<uint8_t*>(node::Buffer::Data(node_buffer));
|
||||
const auto size = node::Buffer::Length(node_buffer);
|
||||
return UNSAFE_BUFFERS(base::span{data, size});
|
||||
}
|
||||
|
||||
} // namespace electron::util
|
||||
|
||||
Reference in New Issue
Block a user