From 1c0214a4785d425dc83f9612169fad7f70a28d41 Mon Sep 17 00:00:00 2001 From: Vladimir Mutafov Date: Fri, 18 Nov 2022 21:30:33 +0200 Subject: [PATCH] fix: memcpy array data for non-direct java bytebuffers (#1740) --- .../runtime/src/main/cpp/ArrayBufferHelper.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/test-app/runtime/src/main/cpp/ArrayBufferHelper.cpp b/test-app/runtime/src/main/cpp/ArrayBufferHelper.cpp index f86ffc198..88ad2c245 100644 --- a/test-app/runtime/src/main/cpp/ArrayBufferHelper.cpp +++ b/test-app/runtime/src/main/cpp/ArrayBufferHelper.cpp @@ -114,16 +114,21 @@ void ArrayBufferHelper::CreateFromCallbackImpl(const FunctionCallbackInfo jbyteArray byteArray = env.NewByteArray(bufferRemainingSize); env.CallObjectMethod(obj, m_getMethodID, byteArray, 0, bufferRemainingSize); - auto buffer = env.GetByteArrayElements(byteArray, 0); + auto byteArrayElements = env.GetByteArrayElements(byteArray, 0); + void* data[bufferRemainingSize]; + memcpy(data, byteArrayElements, bufferRemainingSize); - std::shared_ptr store = ArrayBuffer::NewBackingStore(buffer, bufferRemainingSize, - [](void* data, size_t length, void* deleter_data) { - free(data); - }, - buffer); + std::shared_ptr store = ArrayBuffer::NewBackingStore( + byteArrayElements, + bufferRemainingSize, + [](void *data, size_t length, void *deleter_data) { + free(data); + }, + nullptr); arrayBuffer = ArrayBuffer::New(isolate, store); + env.ReleaseByteArrayElements(byteArray, byteArrayElements, 0); } auto ctx = isolate->GetCurrentContext();