dawn/node: Fix array buffer view conversion
A Napi::TypedArray may have a non-zero offset on its underlying
ArrayBuffer backing buffer. Also use the length of the typed array,
not the backing buffer.
Change-Id: Icaa310ef8f87393b4d7582ba0a0afb2b87318664
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/121820
Reviewed-by: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: James Price <jrprice@google.com>
diff --git a/src/dawn/node/binding/Converter.cpp b/src/dawn/node/binding/Converter.cpp
index e645c44..94c91d6 100644
--- a/src/dawn/node/binding/Converter.cpp
+++ b/src/dawn/node/binding/Converter.cpp
@@ -153,15 +153,20 @@
bool Converter::Convert(BufferSource& out, interop::BufferSource in) {
out = {};
if (auto* view = std::get_if<interop::ArrayBufferView>(&in)) {
- std::visit(
+ return std::visit(
[&](auto&& v) {
auto arr = v.ArrayBuffer();
- out.data = arr.Data();
- out.size = arr.ByteLength();
+ if (v.ByteOffset() + v.ByteLength() > arr.ByteLength()) {
+ Napi::Error::New(env, "offset + length exceeds underlying buffer size")
+ .ThrowAsJavaScriptException();
+ return false;
+ }
+ out.data = static_cast<uint8_t*>(arr.Data()) + v.ByteOffset();
+ out.size = v.ByteLength();
out.bytesPerElement = v.ElementSize();
+ return true;
},
*view);
- return true;
}
if (auto* arr = std::get_if<interop::ArrayBuffer>(&in)) {
out.data = arr->Data();