Skip to content

buffer: fix assertion error in WeakCallback #3329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 6 additions & 4 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,13 @@ void CallbackInfo::WeakCallback(


void CallbackInfo::WeakCallback(Isolate* isolate, Local<Object> object) {
SPREAD_ARG(object, obj);
CHECK_EQ(obj_offset, 0);
CHECK_EQ(obj_c.ByteLength(), obj_length);
CHECK(object->IsArrayBuffer());
Local<ArrayBuffer> buf = object.As<ArrayBuffer>();
ArrayBuffer::Contents obj_c = buf->GetContents();
char* const obj_data = static_cast<char*>(obj_c.Data());
CHECK_NE(obj_data, nullptr);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TooTallNate has mentioned how some use a zero-length buffer to keep track of things in their modules. A zero-length buffer would be nullptr. Instead should be like:

if (obj_c.Length() > 0) CHECK_NE(obj_data, nullptr);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might want to add a test for this case as well.


obj->Buffer()->Neuter();
buf->Neuter();
callback_(obj_data, hint_);
int64_t change_in_bytes = -static_cast<int64_t>(sizeof(*this));
isolate->AdjustAmountOfExternalAllocatedMemory(change_in_bytes);
Expand Down
4 changes: 4 additions & 0 deletions test/addons/buffer-free-callback/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ var buf = binding.alloc();
var slice = buf.slice(32);
buf = null;
binding.check(slice);
slice = null;
gc();
gc();
gc();