From 931118c9d7ec81bd8bc6ebadfe08aeb056283544 Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Mon, 12 Oct 2015 16:08:13 -0400 Subject: [PATCH] buffer: fix assertion error in WeakCallback `CallbackInfo` is now bound to `ArrayBuffer` instance, not `Uint8Array`, therefore `SPREAD_ARG` will abort with: Assertion failed: ((object)->IsUint8Array()) Make changes necessary to migrate it to `ArrayBuffer`. See: https://github.com/nodejs/node/pull/3080#issuecomment-147502167 Reviewed-By: James M Snell Reviewed-By: Trevor Norris PR-URL: https://github.com/nodejs/node/pull/3329 --- src/node_buffer.cc | 13 ++++++++----- test/addons/buffer-free-callback/binding.cc | 2 +- test/addons/buffer-free-callback/test.js | 21 +++++++++++++++++---- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 4df7a9f1b4d065..a472d0c95e9225 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -148,11 +148,14 @@ void CallbackInfo::WeakCallback( void CallbackInfo::WeakCallback(Isolate* isolate, Local object) { - SPREAD_ARG(object, obj); - CHECK_EQ(obj_offset, 0); - CHECK_EQ(obj_c.ByteLength(), obj_length); - - obj->Buffer()->Neuter(); + CHECK(object->IsArrayBuffer()); + Local buf = object.As(); + ArrayBuffer::Contents obj_c = buf->GetContents(); + char* const obj_data = static_cast(obj_c.Data()); + if (buf->ByteLength() != 0) + CHECK_NE(obj_data, nullptr); + + buf->Neuter(); callback_(obj_data, hint_); int64_t change_in_bytes = -static_cast(sizeof(*this)); isolate->AdjustAmountOfExternalAllocatedMemory(change_in_bytes); diff --git a/test/addons/buffer-free-callback/binding.cc b/test/addons/buffer-free-callback/binding.cc index a1e7773e95103d..e5298a0063e5fa 100644 --- a/test/addons/buffer-free-callback/binding.cc +++ b/test/addons/buffer-free-callback/binding.cc @@ -16,7 +16,7 @@ void Alloc(const v8::FunctionCallbackInfo& args) { args.GetReturnValue().Set(node::Buffer::New( isolate, buf, - sizeof(buf), + 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 b95f171de2467e..6ee328d5222049 100644 --- a/test/addons/buffer-free-callback/test.js +++ b/test/addons/buffer-free-callback/test.js @@ -4,7 +4,20 @@ require('../../common'); var assert = require('assert'); var binding = require('./build/Release/binding'); -var buf = binding.alloc(); -var slice = buf.slice(32); -buf = null; -binding.check(slice); + +function check(size) { + var buf = binding.alloc(size); + var slice = buf.slice(size >>> 1); + + buf = null; + binding.check(slice); + slice = null; + gc(); + gc(); + gc(); +} + +check(64); + +// Empty ArrayBuffer does not allocate data, worth checking +check(0);