Skip to content

Commit 1af08e1

Browse files
addaleaxBethGriggs
authored andcommitted
buffer,n-api: fix double ArrayBuffer::Detach() during cleanup
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 <jasnell@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de> Reviewed-By: David Carlier <devnexen@gmail.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
1 parent 31c797c commit 1af08e1

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

src/js_native_api_v8.cc

+6-4
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,12 @@ class ArrayBufferReference final : public Reference {
392392
inline void Finalize(bool is_env_teardown) override {
393393
if (is_env_teardown) {
394394
v8::HandleScope handle_scope(_env->isolate);
395-
v8::Local<v8::Value> ab = Get();
396-
CHECK(!ab.IsEmpty());
397-
CHECK(ab->IsArrayBuffer());
398-
ab.As<v8::ArrayBuffer>()->Detach();
395+
v8::Local<v8::Value> obj = Get();
396+
CHECK(!obj.IsEmpty());
397+
CHECK(obj->IsArrayBuffer());
398+
v8::Local<v8::ArrayBuffer> ab = obj.As<v8::ArrayBuffer>();
399+
if (ab->IsDetachable())
400+
ab->Detach();
399401
}
400402

401403
Reference::Finalize(is_env_teardown);

src/node_buffer.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ void CallbackInfo::CleanupHook(void* data) {
152152
HandleScope handle_scope(self->env_->isolate());
153153
Local<ArrayBuffer> ab = self->persistent_.Get(self->env_->isolate());
154154
CHECK(!ab.IsEmpty());
155-
ab->Detach();
155+
if (ab->IsDetachable())
156+
ab->Detach();
156157
}
157158

158159
self->WeakCallback(self->env_->isolate());

0 commit comments

Comments
 (0)