Skip to content

Commit

Permalink
buffer,n-api: fix double ArrayBuffer::Detach() during cleanup
Browse files Browse the repository at this point in the history
These calls could fail if the `ArrayBuffer` had already been explicitly
detached at some point in the past.

The necessary test changes already came with 4f523c2 and could
be ported back to v12.x with a backport of this PR.

Fixes: #33022
Refs: #30551

PR-URL: #33039
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Gerhard Stöbich <[email protected]>
Reviewed-By: David Carlier <[email protected]>
Reviewed-By: Juan José Arboleda <[email protected]>
  • Loading branch information
addaleax authored and targos committed Apr 27, 2020
1 parent af392a1 commit 39a3cc1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/js_native_api_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,12 @@ class ArrayBufferReference final : public Reference {
inline void Finalize(bool is_env_teardown) override {
if (is_env_teardown) {
v8::HandleScope handle_scope(_env->isolate);
v8::Local<v8::Value> ab = Get();
CHECK(!ab.IsEmpty());
CHECK(ab->IsArrayBuffer());
ab.As<v8::ArrayBuffer>()->Detach();
v8::Local<v8::Value> obj = Get();
CHECK(!obj.IsEmpty());
CHECK(obj->IsArrayBuffer());
v8::Local<v8::ArrayBuffer> ab = obj.As<v8::ArrayBuffer>();
if (ab->IsDetachable())
ab->Detach();
}

Reference::Finalize(is_env_teardown);
Expand Down
3 changes: 2 additions & 1 deletion src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ void CallbackInfo::CleanupHook(void* data) {
HandleScope handle_scope(self->env_->isolate());
Local<ArrayBuffer> ab = self->persistent_.Get(self->env_->isolate());
CHECK(!ab.IsEmpty());
ab->Detach();
if (ab->IsDetachable())
ab->Detach();
}

self->WeakCallback(self->env_->isolate());
Expand Down

0 comments on commit 39a3cc1

Please sign in to comment.