Skip to content

Commit

Permalink
Merge pull request #623 from kunalspathak/heap_corruption
Browse files Browse the repository at this point in the history
Fix heap corruption issue on windows
  • Loading branch information
jacobrosenthal committed Nov 9, 2015
2 parents 102cfdf + 25d6c18 commit e9b8440
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/serialport_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,16 @@ void DisposeWatchPortCallbacks(WatchPortBaton* data) {
delete data->disconnectedCallback;
}

// FinalizerCallback will prevent WatchPortBaton::buffer from getting
// collected by gc while finalizing v8::ArrayBuffer. The buffer will
// get cleaned up through this callback.
static void FinalizerCallback(char* data, void* hint) {
uv_work_t* req = reinterpret_cast<uv_work_t*>(hint);
WatchPortBaton* wpb = static_cast<WatchPortBaton*>(req->data);
delete wpb;
delete req;
}

void EIO_AfterWatchPort(uv_work_t* req) {
Nan::HandleScope scope;

Expand All @@ -300,9 +310,11 @@ void EIO_AfterWatchPort(uv_work_t* req) {
goto cleanup;
}

bool skipCleanup = false;
if(data->bytesRead > 0) {
v8::Local<v8::Value> argv[1];
argv[0] = Nan::NewBuffer(data->buffer, data->bytesRead).ToLocalChecked();
argv[0] = Nan::NewBuffer(data->buffer, data->bytesRead, FinalizerCallback, req).ToLocalChecked();
skipCleanup = true;
data->dataCallback->Call(1, argv);
} else if(data->errorCode > 0) {
if(data->errorCode == ERROR_INVALID_HANDLE && IsClosingHandle((int)data->fd)) {
Expand All @@ -318,8 +330,10 @@ void EIO_AfterWatchPort(uv_work_t* req) {
AfterOpenSuccess((int)data->fd, data->dataCallback, data->disconnectedCallback, data->errorCallback);

cleanup:
delete data;
delete req;
if (!skipCleanup) {
delete data;
delete req;
}
}

void AfterOpenSuccess(int fd, Nan::Callback* dataCallback, Nan::Callback* disconnectedCallback, Nan::Callback* errorCallback) {
Expand Down

0 comments on commit e9b8440

Please sign in to comment.