From ae3e9adc1bb19ed4db9ea4bf95bb436df91f687b Mon Sep 17 00:00:00 2001 From: James Price Date: Mon, 27 Feb 2023 21:21:02 +0000 Subject: [PATCH] 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 Kokoro: Kokoro Commit-Queue: James Price --- src/dawn/node/binding/Converter.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/dawn/node/binding/Converter.cpp b/src/dawn/node/binding/Converter.cpp index e645c44dc1..94c91d62e8 100644 --- a/src/dawn/node/binding/Converter.cpp +++ b/src/dawn/node/binding/Converter.cpp @@ -153,15 +153,20 @@ bool Converter::Convert(wgpu::ImageCopyBuffer& out, const interop::GPUImageCopyB bool Converter::Convert(BufferSource& out, interop::BufferSource in) { out = {}; if (auto* view = std::get_if(&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(arr.Data()) + v.ByteOffset(); + out.size = v.ByteLength(); out.bytesPerElement = v.ElementSize(); + return true; }, *view); - return true; } if (auto* arr = std::get_if(&in)) { out.data = arr->Data();