Prevent undefined behavior when some Node Buffer objects are destroyed

If node::Buffer::New() is used to wrap an existing chunk of memory
without providing a custom callback to release that memory then Node
will just use `free()`. In a couple of places Node buffer objects were
constructed from chunks of memory that were allocated with `new[]`, but
a custom callback to release that memory was omitted, this resulted in
undefined behavior when those buffers were destroyed because `free()`
was used to release memory allocated with `new[]`.

To avoid undefined behavior the aforementioned buffer objects are now
constructed with a custom callback that safely releases the underlying
chunk of memory.
This commit is contained in:
Vadim Macagon 2016-10-02 23:38:39 +07:00
parent ff6a8fac2a
commit 7c5d3296e7
2 changed files with 14 additions and 5 deletions

View file

@ -21,6 +21,10 @@ namespace atom {
namespace {
void FreeNodeBufferData(char* data, void* hint) {
delete[] data;
}
void RunCallbackInUI(
const AtomBlobReader::CompletionCallback& callback,
char* blob_data,
@ -32,7 +36,8 @@ void RunCallbackInUI(
v8::HandleScope handle_scope(isolate);
if (blob_data) {
v8::Local<v8::Value> buffer = node::Buffer::New(isolate,
blob_data, static_cast<size_t>(size)).ToLocalChecked();
blob_data, static_cast<size_t>(size), &FreeNodeBufferData, nullptr)
.ToLocalChecked();
callback.Run(buffer);
} else {
callback.Run(v8::Null(isolate));