Skip to content
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

src: avoid deferred gc/cleanup for Buffer.from #38337

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 18 additions & 10 deletions src/node_buffer.cc
Expand Up @@ -303,28 +303,36 @@ MaybeLocal<Object> New(Isolate* isolate,
if (!StringBytes::Size(isolate, string, enc).To(&length))
return Local<Object>();
size_t actual = 0;
char* data = nullptr;
std::unique_ptr<BackingStore> store;

if (length > 0) {
data = UncheckedMalloc(length);
store = ArrayBuffer::NewBackingStore(isolate, length);

if (data == nullptr) {
if (UNLIKELY(!store)) {
THROW_ERR_MEMORY_ALLOCATION_FAILED(isolate);
return Local<Object>();
}

actual = StringBytes::Write(isolate, data, length, string, enc);
actual = StringBytes::Write(
isolate,
reinterpret_cast<char*>(store->Data()),
jasnell marked this conversation as resolved.
Show resolved Hide resolved
length,
string,
enc);
CHECK(actual <= length);

if (actual == 0) {
free(data);
data = nullptr;
} else if (actual < length) {
data = node::Realloc(data, actual);
if (LIKELY(actual > 0)) {
if (actual < length)
store = BackingStore::Reallocate(isolate, std::move(store), actual);
Local<ArrayBuffer> buf = ArrayBuffer::New(isolate, std::move(store));
Local<Object> obj;
if (UNLIKELY(!New(isolate, buf, 0, actual).ToLocal(&obj)))
return MaybeLocal<Object>();
return scope.Escape(obj);
}
}

return scope.EscapeMaybe(New(isolate, data, actual));
return scope.EscapeMaybe(New(isolate, 0));
}


Expand Down