diff --git a/test/addons/buffer-free-callback/binding.cc b/test/addons/buffer-free-callback/binding.cc index e5298a0063..7bfd5e061d 100644 --- a/test/addons/buffer-free-callback/binding.cc +++ b/test/addons/buffer-free-callback/binding.cc @@ -13,9 +13,16 @@ static void FreeCallback(char* data, void* hint) { void Alloc(const v8::FunctionCallbackInfo& args) { v8::Isolate* isolate = args.GetIsolate(); alive++; + + uintptr_t alignment = args[1]->IntegerValue(); + uintptr_t offset = args[2]->IntegerValue(); + + uintptr_t static_offset = reinterpret_cast(buf) % alignment; + char* aligned = buf + (alignment - static_offset) + offset; + args.GetReturnValue().Set(node::Buffer::New( isolate, - buf, + aligned, args[0]->IntegerValue(), FreeCallback, nullptr).ToLocalChecked()); diff --git a/test/addons/buffer-free-callback/test.js b/test/addons/buffer-free-callback/test.js index a70931d81e..f9d7ec90f8 100644 --- a/test/addons/buffer-free-callback/test.js +++ b/test/addons/buffer-free-callback/test.js @@ -4,8 +4,8 @@ require('../../common'); var binding = require('./build/Release/binding'); -function check(size) { - var buf = binding.alloc(size); +function check(size, alignment, offset) { + var buf = binding.alloc(size, alignment, offset); var slice = buf.slice(size >>> 1); buf = null; @@ -16,7 +16,20 @@ function check(size) { gc(); } -check(64); +check(64, 1, 0); + +// Buffers can have weird sizes. +check(97, 1, 0); + +// Buffers can be unaligned +check(64, 8, 0); +check(64, 16, 0); +check(64, 8, 1); +check(64, 16, 1); +check(97, 8, 1); +check(97, 16, 1); +check(97, 8, 3); +check(97, 16, 3); // Empty ArrayBuffer does not allocate data, worth checking -check(0); +check(0, 1, 0);