Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/ffi/data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ void ToArrayBuffer(const FunctionCallbackInfo<Value>& args) {
if (copy) {
std::unique_ptr<BackingStore> store =
ArrayBuffer::NewBackingStore(isolate, len);
memcpy(store->Data(), reinterpret_cast<void*>(ptr), len);
if (len > 0) memcpy(store->Data(), reinterpret_cast<void*>(ptr), len);
ab = ArrayBuffer::New(isolate, std::move(store));
} else {
std::unique_ptr<BackingStore> store = ArrayBuffer::NewBackingStore(
Expand Down Expand Up @@ -740,7 +740,9 @@ void ExportBytes(const FunctionCallbackInfo<Value>& args) {
return;
}

std::memcpy(reinterpret_cast<void*>(ptr), view.data(), view.length());
if (view.length() > 0) {
std::memcpy(reinterpret_cast<void*>(ptr), view.data(), view.length());
}
}

void GetRawPointer(const FunctionCallbackInfo<Value>& args) {
Expand Down
8 changes: 8 additions & 0 deletions test/ffi/test-ffi-memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,14 @@ test('ffi toString returns null for a null pointer', () => {
assert.strictEqual(ffi.toString(0n), null);
});

test('ffi accepts zero-length copies through a null pointer', () => {
assert.strictEqual(ffi.toBuffer(0n, 0).length, 0);
assert.strictEqual(ffi.toArrayBuffer(0n, 0).byteLength, 0);
assert.strictEqual(ffi.exportBuffer(Buffer.alloc(0), 0n, 0), undefined);
assert.strictEqual(ffi.exportArrayBuffer(new ArrayBuffer(0), 0n, 0), undefined);
assert.strictEqual(ffi.exportArrayBufferView(new Uint8Array(0), 0n, 0), undefined);
});

test('ffi validates memory access arguments', () => {
withAllocations(common.mustCall((alloc) => {
const ptr = alloc(8);
Expand Down
Loading