From b07acb38086c3ad16817743391b7520d27032b2a Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Mon, 9 Jan 2012 20:42:11 +0100 Subject: [PATCH] child_process: fix segfault after failed spawn The process handle is uninitialized when uv_spawn() fails so don't export the handle to JS land when that happens. Attempts to close the uninitialized handle resulted in segmentation faults and memory corruption. Fixes #2481. --- src/process_wrap.cc | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/process_wrap.cc b/src/process_wrap.cc index 7a6a8518a2..6600a044c8 100644 --- a/src/process_wrap.cc +++ b/src/process_wrap.cc @@ -177,10 +177,14 @@ class ProcessWrap : public HandleWrap { int r = uv_spawn(uv_default_loop(), &wrap->process_, options); - wrap->SetHandle((uv_handle_t*)&wrap->process_); - assert(wrap->process_.data == wrap); - - wrap->object_->Set(String::New("pid"), Integer::New(wrap->process_.pid)); + if (r) { + SetErrno(uv_last_error(uv_default_loop())); + } + else { + wrap->SetHandle((uv_handle_t*)&wrap->process_); + assert(wrap->process_.data == wrap); + wrap->object_->Set(String::New("pid"), Integer::New(wrap->process_.pid)); + } if (options.args) { for (int i = 0; options.args[i]; i++) free(options.args[i]); @@ -195,8 +199,6 @@ class ProcessWrap : public HandleWrap { delete [] options.env; } - if (r) SetErrno(uv_last_error(uv_default_loop())); - return scope.Close(Integer::New(r)); }