Skip to content

Commit b3cbd13

Browse files
indutnyjasnell
authored andcommitted
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: #3080 (comment) Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trevor Norris <[email protected]> PR-URL: #3329
1 parent b8ca4ee commit b3cbd13

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

src/node_buffer.cc

+8-5
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,14 @@ void CallbackInfo::WeakCallback(
148148

149149

150150
void CallbackInfo::WeakCallback(Isolate* isolate, Local<Object> object) {
151-
SPREAD_ARG(object, obj);
152-
CHECK_EQ(obj_offset, 0);
153-
CHECK_EQ(obj_c.ByteLength(), obj_length);
154-
155-
obj->Buffer()->Neuter();
151+
CHECK(object->IsArrayBuffer());
152+
Local<ArrayBuffer> buf = object.As<ArrayBuffer>();
153+
ArrayBuffer::Contents obj_c = buf->GetContents();
154+
char* const obj_data = static_cast<char*>(obj_c.Data());
155+
if (buf->ByteLength() != 0)
156+
CHECK_NE(obj_data, nullptr);
157+
158+
buf->Neuter();
156159
callback_(obj_data, hint_);
157160
int64_t change_in_bytes = -static_cast<int64_t>(sizeof(*this));
158161
isolate->AdjustAmountOfExternalAllocatedMemory(change_in_bytes);

test/addons/buffer-free-callback/binding.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ void Alloc(const v8::FunctionCallbackInfo<v8::Value>& args) {
1616
args.GetReturnValue().Set(node::Buffer::New(
1717
isolate,
1818
buf,
19-
sizeof(buf),
19+
args[0]->IntegerValue(),
2020
FreeCallback,
2121
nullptr).ToLocalChecked());
2222
}

test/addons/buffer-free-callback/test.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,20 @@
44
require('../../common');
55
var assert = require('assert');
66
var binding = require('./build/Release/binding');
7-
var buf = binding.alloc();
8-
var slice = buf.slice(32);
9-
buf = null;
10-
binding.check(slice);
7+
8+
function check(size) {
9+
var buf = binding.alloc(size);
10+
var slice = buf.slice(size >>> 1);
11+
12+
buf = null;
13+
binding.check(slice);
14+
slice = null;
15+
gc();
16+
gc();
17+
gc();
18+
}
19+
20+
check(64);
21+
22+
// Empty ArrayBuffer does not allocate data, worth checking
23+
check(0);

0 commit comments

Comments
 (0)